Sample rate calculation: How can I identify when value changes and calculate the time between points

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

If y is at a fixed sample rate (as it appears to be) you can just use
doc unique
with the index output argument to locate the values and respective times (or if you know the times in advance you can simply create the coarser grid from these times).
Then
doc interp1
will allow you to interpolate from the coarser grid (0:0.025:...) to the finer grid (0:0.05:...)
Thank you for your response.
I have looked into this function and I feel it may be possible that I could lose data if the measurement stays consistent or later returns to an earlier value, because it only looks for unique values it would determine this as not unique (when actually it is). Would you agree this could happen?
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.

Sign in to comment.

Answers (1)

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

Thank you, but this is only an option if you have the signal processing toolbox. I do not have the toolbox.
The second option works without the SPT. Also, you can replace findchangepts by the analogous ischange function
Ah, great. I will investigate these options then. Thank you.

Sign in to comment.

Products

Release

R2016b

Asked:

on 3 Sep 2018

Commented:

on 6 Sep 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!