How do I calculate the RMS of every 5 values?

Hello, I have a vector of 66000 values and I would like to know the root mean square (RMS) of every 5 values. Do I need a loop for this? Or is there some easier way to solve this?
Thanks in advance! Shawn

 Accepted Answer

If you mean you want the RMS of the first 5 values, followed by the RMS of the next 5 values, etc., then assuming your vector length is a multiple of 5 simply reshape it into columns of 5 elements:
rms = sqrt(mean(reshape(yourvector, 5, []) .^ 2));
If you mean you want a moving rms (rms of elements 1-5, rms of elements 2-6, etc.), then use movmean:
movrms = sqrt(movmean(yourvector .^ 2, 5));

5 Comments

I meant the first one. But thanks this works great!
Hello Guillaume,
I have a Question similar to the one above.
Q.Calculate moving root meaning square with every 100 data points or so-called time window. All the moving time windows are not overlapped for simplification. Also, a new time vector needs to be calculated to synchronize the moving RMS data.(EMG)
Can I Use ,
RMS = movingRMS(LowFilt_EMG,100);
rmsTime = newTime(Time,100);
Actually I tried using these codes. but it was showing the error. Can you please help me with this?
Thank you very much Guillaume.
Have a nice year ¿??¿
Really Guillaume I don't know how to thank you properly.
You should be in the Avengers cause you're my little hero.
plot(love);
awesome piece of code, Guillaume !!! have a great year
reduced my code execution from 1 sec to 0.025 sec !!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!