MATLAB: Doing averages in blocks for downsampling time series data

averageblock averagingdigital signal processingMATLAB

One way to downsample is to do averaging in blocks. I had asked a question here "I would like to downsample data simulated at 200 Hz to 50 Hz by averaging blocks of 4 and plot it versus time. For example, if y=[1,2,3,4,5,6,7,8,9,10,11,……], I would like to do averages of (1,2,3,4), then (5,6,7,8), and so and plot it versus t=[0:1/50:60], so that the downsampled data appears to be sampled at 50 Hz." The answer was
y=[1,2,3,4,5,6,7,8,9,10,11];
out = nanmean(reshape([y(:); nan(mod(-numel(y),4),1)],4,[]));
Is there simpler syntax or smaller steps to achieve the same goal. It works but I was trying to decipher the code for self-teaching purposes.
Thank you.

Best Answer

A regular timetable and resample?
>> tt = timetable(rand(12,1),'SampleRate',200)
tt =
12×1 timetable
Time Var1
_________ ________
0 sec 0.72187
0.005 sec 0.49658
0.01 sec 0.053694
0.015 sec 0.44163
0.02 sec 0.51917
0.025 sec 0.77194
0.03 sec 0.065356
0.035 sec 0.44279
0.04 sec 0.97718
0.045 sec 0.46774
0.05 sec 0.32906
0.055 sec 0.44593
>> ttAvg = retime(tt,'regular','mean','SampleRate',50)
ttAvg =
3×1 timetable
Time Var1
________ _______
0 sec 0.42845
0.02 sec 0.44982
0.04 sec 0.55498
then
plot(ttAvg.Time,ttAvg.Var1)