interpolating data to different sample rate

47 views (last 30 days)
C.G.
C.G. on 14 Feb 2022
Commented: Star Strider on 15 Feb 2022
I have 2 data sets I want to correlate. One is sampled every second, and one is sampled at a much smaller interval (attached). I want to get them both to the same sampling rate by increasing the smaller intervalled one to 1s.
How would I go about this?

Answers (2)

Benjamin Thompson
Benjamin Thompson on 14 Feb 2022
You can use interp1 to interpolate, or resample, one signal onto the same set of time values as another.

Star Strider
Star Strider on 14 Feb 2022
If they are signals, use resample because it includes an anti-aliasing filter, and these appear to be signals.
format longE
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/894275/Raw%20Data.xlsx', 'VariableNamingRule','preserve')
T1 = 499×5 table
Time (s) Linear Acceleration x (m/s^2) Linear Acceleration y (m/s^2) Linear Acceleration z (m/s^2) Absolute acceleration (m/s^2) ____________________ _____________________________ _____________________________ _____________________________ _____________________________ 1.86912208300000e-01 8.93268171700000e-03 8.41090908300000e-03 -1.81244389400000e-01 1.81659199800000e-01 1.96983208300000e-01 8.07946331800000e-03 1.19734714000000e-02 -2.56993314000000e-01 2.57398922300000e-01 2.07023208300000e-01 7.70330462600000e-03 -4.94139058500000e-03 -3.28702397300000e-01 3.28829780100000e-01 2.17063208300000e-01 1.09702536500000e-02 -2.36724525700000e-03 -3.19120565100000e-01 3.19317843800000e-01 2.27134208300000e-01 1.50394041100000e-02 -1.41565110900000e-02 -2.72862072600000e-01 2.73642652300000e-01 2.37174208300000e-01 7.19481613500000e-03 -9.09744529100000e-03 -2.18375963000000e-01 2.18683767300000e-01 2.47215208300000e-01 9.69910550900000e-03 1.32750067900000e-04 -1.58347862400000e-01 1.58644684100000e-01 2.57255208300000e-01 1.99553044900000e-02 7.00284473600000e-03 -1.93227673200000e-01 1.94381551800000e-01 2.67326208300000e-01 2.18355497700000e-02 8.38541887700000e-03 -2.33348928100000e-01 2.34518290800000e-01 2.77366208300000e-01 1.39188583200000e-02 1.63915910000000e-02 -2.64484766700000e-01 2.65357514900000e-01 2.87406208300000e-01 1.97226218500000e-02 1.40000798000000e-02 -3.12020875800000e-01 3.12956883600000e-01 2.97477208300000e-01 1.37514086800000e-02 7.38640381000000e-03 -2.80596184700000e-01 2.81030032400000e-01 3.07517208300000e-01 1.23518045200000e-02 1.00708239700000e-02 -2.15932411600000e-01 2.16519733300000e-01 3.17558208300000e-01 1.03200432700000e-02 7.87407986800000e-03 -1.92114363300000e-01 1.92552416300000e-01 3.27598208300000e-01 2.17892471300000e-02 7.59140353600000e-03 -1.70313019800000e-01 1.71868919200000e-01 3.37669208300000e-01 1.13416249300000e-02 1.17506742100000e-02 -1.91676406900000e-01 1.92370880700000e-01
M1 = table2array(T1);
TimeStats = [mean(diff(M1(:,1))); std(diff(M1(:,1))); 1/mean(diff(M1(:,1)))]
TimeStats = 3×1
8.031238955220885e-03 4.503231238111708e-02 1.245137899115712e+02
[y,t] = resample(M1(:,2:end), M1(:,1), 1.25E+2); % Resample To Uniform 1.25 Hz Sampling Frequency
T1r = array2table([t, y], 'VariableNames',T1.Properties.VariableNames)
T1r = 500×5 table
Time (s) Linear Acceleration x (m/s^2) Linear Acceleration y (m/s^2) Linear Acceleration z (m/s^2) Absolute acceleration (m/s^2) ____________________ _____________________________ _____________________________ _____________________________ _____________________________ 1.86912208300000e-01 8.93268171700000e-03 8.41090908300000e-03 -1.81244389400000e-01 1.81659199800000e-01 1.94912208300000e-01 8.25491911229342e-03 1.12408662407798e-02 -2.41416308454712e-01 2.41823809074154e-01 2.02912208300000e-01 7.85732737329203e-03 1.98460519391782e-03 -2.99340241777460e-01 2.99581547389263e-01 2.10912208300000e-01 8.96875928280637e-03 -3.94429385386534e-03 -3.24990868871135e-01 3.25145325889771e-01 2.18912208300000e-01 1.17173352904071e-02 -4.53171279003712e-03 -3.10627669396247e-01 3.10932040097935e-01 2.26912208300000e-01 1.49497058275931e-02 -1.38966345122097e-02 -2.73881771272922e-01 2.74649492982455e-01 2.34912208300000e-01 8.96219242976595e-03 -1.02372467688225e-02 -2.30651618370040e-01 2.31065938402590e-01 2.42912208300000e-01 8.62590989339179e-03 -3.82278530002618e-03 -1.84072483143133e-01 1.84374011359197e-01 2.50912208300000e-01 1.34757158309878e-02 2.66250504678105e-03 -1.71191553687610e-01 1.71803966957261e-01 2.58912208300000e-01 2.02646646755784e-02 7.23032218130206e-03 -1.99828896451842e-01 2.00985322679059e-01 2.66912208300000e-01 2.17582563983467e-02 8.32858383635121e-03 -2.31699618247096e-01 2.32868344424665e-01 2.74912208300000e-01 1.58538743377590e-02 1.44347039093783e-02 -2.56874473082032e-01 2.57819720483526e-01 2.82912208300000e-01 1.71248017998187e-02 1.50705430801594e-02 -2.90743258838307e-01 2.91650951036474e-01 2.90912208300000e-01 1.76438736249956e-02 1.16976720926284e-02 -3.01081051850382e-01 3.01842243513892e-01 2.98912208300000e-01 1.35513656551394e-02 7.77008338466134e-03 -2.71353902389393e-01 2.71809685865289e-01 3.06912208300000e-01 1.24361432168924e-02 9.90906359183266e-03 -2.19828983584612e-01 2.20407057100349e-01
The data need to be resampled regardless, since the standard deviation of the sampling intervals is not near 0 as it should be, indicating irregulal sampling intervals. This is not appropriate for signal processing applications, all of which assuming regular sampling intervals.
To use these data in any signal processing application — and even for fft — they need to be resampled first to a consistent sampling frequency. I chose 125 Hz because the mean sampling frequency of the original data ia 124.514... Hz so this would likely produce the least disruptive resampling.
Resample the other signals at 125 Hz in order to compare them. Ideally, they also need to have identical sampling times (not simply the same sampling frequency) in order to do this correctly.
.
  2 Comments
C.G.
C.G. on 15 Feb 2022
Thank you for your reponse.
My other dataset has been resampled using retime as it is in a different format as its from different equipment. How would I go about getting them identical for correlation?
Star Strider
Star Strider on 15 Feb 2022
My pleasure!
format longE
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/895055/S%20Raw.xlsx', 'VariableNamingRule','preserve')
T1 = 499×2 table
Var1 Var2 _____________________________ ____________________ {''2022-02-10 18:39:01.000''} 5.12916339756230e-01 {''2022-02-10 18:39:02.000''} 6.04788079457912e-01 {''2022-02-10 18:39:03.000''} 6.96659819159595e-01 {''2022-02-10 18:39:04.000''} 7.00000000000000e-01 {''2022-02-10 18:39:05.000''} 5.44174346120565e-01 {''2022-02-10 18:39:06.000''} 6.37207809050609e-01 {''2022-02-10 18:39:07.000''} 4.59791541906453e-01 {''2022-02-10 18:39:08.000''} 3.51146870578991e-01 {''2022-02-10 18:39:09.000''} 4.41666719473181e-01 {''2022-02-10 18:39:10.000''} 5.64390928432270e-01 {''2022-02-10 18:39:11.000''} 7.45466810577674e-01 {''2022-02-10 18:39:12.000''} 9.64411062348048e-01 {''2022-02-10 18:39:13.000''} 1.39925822374150e+00 {''2022-02-10 18:39:14.000''} 1.30740876133655e+00 {''2022-02-10 18:39:15.000''} 1.21908190407119e+00 {''2022-02-10 18:39:16.000''} 1.20000000000000e+00
T1.Var1 = datetime(T1.Var1, 'InputFormat','''''yyyy-MM-dd HH:mm:ss.SSS''''')
T1 = 499×2 table
Var1 Var2 ____________________ ____________________ 10-Feb-2022 18:39:01 5.12916339756230e-01 10-Feb-2022 18:39:02 6.04788079457912e-01 10-Feb-2022 18:39:03 6.96659819159595e-01 10-Feb-2022 18:39:04 7.00000000000000e-01 10-Feb-2022 18:39:05 5.44174346120565e-01 10-Feb-2022 18:39:06 6.37207809050609e-01 10-Feb-2022 18:39:07 4.59791541906453e-01 10-Feb-2022 18:39:08 3.51146870578991e-01 10-Feb-2022 18:39:09 4.41666719473181e-01 10-Feb-2022 18:39:10 5.64390928432270e-01 10-Feb-2022 18:39:11 7.45466810577674e-01 10-Feb-2022 18:39:12 9.64411062348048e-01 10-Feb-2022 18:39:13 1.39925822374150e+00 10-Feb-2022 18:39:14 1.30740876133655e+00 10-Feb-2022 18:39:15 1.21908190407119e+00 10-Feb-2022 18:39:16 1.20000000000000e+00
TT1 = table2timetable(T1)
TT1 = 499×1 timetable
Var1 Var2 ____________________ ____________________ 10-Feb-2022 18:39:01 5.12916339756230e-01 10-Feb-2022 18:39:02 6.04788079457912e-01 10-Feb-2022 18:39:03 6.96659819159595e-01 10-Feb-2022 18:39:04 7.00000000000000e-01 10-Feb-2022 18:39:05 5.44174346120565e-01 10-Feb-2022 18:39:06 6.37207809050609e-01 10-Feb-2022 18:39:07 4.59791541906453e-01 10-Feb-2022 18:39:08 3.51146870578991e-01 10-Feb-2022 18:39:09 4.41666719473181e-01 10-Feb-2022 18:39:10 5.64390928432270e-01 10-Feb-2022 18:39:11 7.45466810577674e-01 10-Feb-2022 18:39:12 9.64411062348048e-01 10-Feb-2022 18:39:13 1.39925822374150e+00 10-Feb-2022 18:39:14 1.30740876133655e+00 10-Feb-2022 18:39:15 1.21908190407119e+00 10-Feb-2022 18:39:16 1.20000000000000e+00
TotTime = TT1.Var1(end) - TT1.Var1(1)
TotTime = duration
00:08:18
TT1.ssSSS = TT1.Var1;
TT1.ssSSS.Format = 'ss.SSS' % Cumulative Time In s & ms
TT1 = 499×2 timetable
Var1 Var2 ssSSS ____________________ ____________________ ______ 10-Feb-2022 18:39:01 5.12916339756230e-01 01.000 10-Feb-2022 18:39:02 6.04788079457912e-01 02.000 10-Feb-2022 18:39:03 6.96659819159595e-01 03.000 10-Feb-2022 18:39:04 7.00000000000000e-01 04.000 10-Feb-2022 18:39:05 5.44174346120565e-01 05.000 10-Feb-2022 18:39:06 6.37207809050609e-01 06.000 10-Feb-2022 18:39:07 4.59791541906453e-01 07.000 10-Feb-2022 18:39:08 3.51146870578991e-01 08.000 10-Feb-2022 18:39:09 4.41666719473181e-01 09.000 10-Feb-2022 18:39:10 5.64390928432270e-01 10.000 10-Feb-2022 18:39:11 7.45466810577674e-01 11.000 10-Feb-2022 18:39:12 9.64411062348048e-01 12.000 10-Feb-2022 18:39:13 1.39925822374150e+00 13.000 10-Feb-2022 18:39:14 1.30740876133655e+00 14.000 10-Feb-2022 18:39:15 1.21908190407119e+00 15.000 10-Feb-2022 18:39:16 1.20000000000000e+00 16.000
TssSSS = second(TT1.ssSSS);
TimeStats = [mean(diff(TssSSS)); std(diff(TssSSS)); 1/mean(diff(TssSSS))]
TimeStats = 3×1
3.614457831325301e-02 7.550946933513523e+00 2.766666666666667e+01
[y,t] = resample(TT1.Var2, TssSSS, 1.25E+2); % Resample To Uniform 1.25 Hz Sampling Frequency
T1r = array2table([t, y], 'VariableNames',{'Time (ms)','Var2'})
T1r = 7386×2 table
Time (ms) Var2 ____________________ ____________________ 0.00000000000000e+00 5.84632575588755e+01 8.00000000000000e-03 6.09226103576792e+01 1.60000000000000e-02 6.29139951844163e+01 2.40000000000000e-02 6.44317557038368e+01 3.20000000000000e-02 6.54828195436685e+01 4.00000000000000e-02 6.60861145277872e+01 4.80000000000000e-02 6.62715991338979e+01 5.60000000000000e-02 6.60789485631659e+01 6.40000000000000e-02 6.55559499994597e+01 7.20000000000000e-02 6.47566706063528e+01 8.00000000000000e-02 6.37394691873600e+01 8.80000000000000e-02 6.25649269546137e+01 9.60000000000000e-02 6.12937743680878e+01 1.04000000000000e-01 5.99848895005805e+01 1.12000000000000e-01 5.86934389560559e+01 1.20000000000000e-01 5.74665953396047e+01
Now, the previous set and this set are all sampled at the same frequency (125 Hz), so they can all be processed with the same filters or other signal processing procedures.
.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!