Sample rate calculation: How can I identify when value changes and calculate the time between points
13 views (last 30 days)
Show older comments
I have a series of data channels each with an x component and a y component. The x component in all cases is time and the y component is the variable being measured. All of the x components in the data has been logged (or exported) at a sample rate of 200Hz, but some of the y components are actually measured at a lower sample rate than this. This results in the graphs looking very digital instead of smooth.
So, to fix this, I'd like to be able to determine when the value of the y component changes and calculate the time between these changes to determine the true sample rate.
Below is an example of the data I have. You can see the X component is time at 200Hz and the Y component just repeats until it updates its value at a much lower rate, highlighted by the #.
LubP_x LubP_y
0 1.023#
0.005 1.023
0.010 1.023
0.015 1.023
0.020 1.023
0.025 1.285#
0.030 1.285
0.035 1.285
0.040 1.285
0.045 1.285
0.050 1.426#
0.055 1.426
0.060 1.426
0.065 1.426
0.070 1.426
0.075 1.734#
0.080 1.734
0.085 1.734
0.090 1.734
0.095 1.734
0.100 1.956#
[...]
I can currently calculate the sample rate as follows: -
LubP_t = mean(diff(LubP_x)); % time step per point
LubP_sr = 1 / LubP_t; % sample rate in Hz
but it simply gives me 200Hz as you would expect. I have seen it is possible to change sample rate of data, but only if you know what the new sample rate needs to be. I need to first calculate the new required sample rate.
I hope the above makes sense.
Many thanks Tom
3 Comments
Adam
on 3 Sep 2018
Yes, by far the better solution would be if you know what the sample rate is of the y data. If you have no information on this then you could estimate it based on the most common run length of values if it is consistent, but it is still no ideal.
e.g. if you have many runs of 5 consecutive identical values and a few of 10 then clearly the 10s are just multiple blocks of 5 which happen to have the same value. There are utilities on File Exchange that can calculate run length and also probably simple combinations of Matlab commands to also achieve the same thing.
Answers (1)
jonas
on 3 Sep 2018
Edited: jonas
on 3 Sep 2018
Not sure if this is a complete answer, but here is how you can find the changes. You can then calculate the time between changes easily by diff(t(locs))
A is the 2-column data set you posted.
t=A(:,1);
y=A(:,2)
[locs]=findchangepts(y,'statistic','mean','minthreshold',1e-5)
plot(t,y,'k',...
t(locs),y(locs),'o')

...or a simpler solution that works in case you have zero noise
tc=t(diff(y)>0)
yc=y(diff(y)>0)
hp=plot(t,y,'k',...
tc,yc,'o')
if you have noise you have to change the zero to a tolerance
4 Comments
See Also
Categories
Find more on Spectral Estimation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!