Many spectroscopic detectors used to study light absorbance as a function of time. These detectors cab sample data at 160 to 250 Hz. There is an option of choosing lower sampling rates such 80, 40, 20, 10 Hz to decrease noise. The manual uses an interesting term called "data bunching" when lower sampling rates are chosen. I asked the engineers and he clarified that it is akin to block averaging. The data is still being sampled at 160 Hz, however digitally it is being downsampled to 80, 40 Hz etc.

Just for the sake of simulating data bunching 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."

Is there a simple syntax for Matlab for that?

Thank you.


1 Answer 1


Averaging by N but keeping the same sample rate is equivalent to filtering input "y" with an FIR filter "B". Where B = 1/N * ones(1,N);

yFiltered = filter(B,1,y);

Now you simply need to downsample the data by N. I'm not sure exactly how the engineer implemented the original algorith, but I would do this.

yDownsampled = yFiltered(N:N:end);

Basically start at index "N" and keep 1 out of N samples until the end.

Your starting index can be anything between 1 and N, you can toy around with it.

You'd need to do the same with your time vector.

  • $\begingroup$ Unfortunately, all these big companies hide what they are doing. I am doing it for the sake of learning. Some one in Mathwork answered it as the following: y=[1,2,3,4,5,6,7,8,9,10,11]; out = nanmean(reshape([y(:); nan(mod(-numel(y),4),1)],4,[])); This gives me the desired result of downsampling if I run it on a simulated data set. However the syntax is too complicated. I was trying to break it down into steps. Your suggestion is quite different or equivalent. $\endgroup$
    – ACR
    Commented Apr 14, 2019 at 21:31
  • $\begingroup$ Yeah in my opinion , you should focus on having Matlab code that is easy to understand Unless you need performance then it's ok to use more complex code. $\endgroup$
    – Ben
    Commented Apr 14, 2019 at 21:57
  • $\begingroup$ Does your code and this one out = nanmean(reshape([y(:); nan(mod(-numel(y),4),1)],4,[])); give the same output or not? Thanks. $\endgroup$
    – ACR
    Commented Apr 14, 2019 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.