MATLAB: Downsampling by Averaging in Blocks

averagingdigital signal processing

Hello, 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 MATLAB downsample function seems to downsample by removing the data points. http://www.mathworks.com/help/signal/ref/downsample.html
Thanks.

Best Answer

y=[1,2,3,4,5,6,7,8,9,10,11];
out = nanmean(reshape([y(:); nan(mod(-numel(y),4),1)],4,[]));