# Is there a Python method to calculate moving RMS of a signal? How to calculate the moving RMS using the discrete signal formula? [closed]

I'm trying to calculate a moving RMS of an acceleration signal.

One of the answers in the post, shows the formula for calculating moving RMS for discrete signal.

The formula they suggest:

I have a few questions on this formula. Since $$i$$ would be, for most part of the summation, less than $$N$$(no of samples), therefore k would be negative and the negative index does not make sense to me. That's why I'm having trouble understanding this formula.

Also, is there a python method to find moving RMS of a signal like MATLAB has dsp.MovingRMS?

• you're misinterpreting what $N$ is in that formula. Apr 29, 2021 at 20:29
• @MarcusMüller The referenced answer discussing the equation uses a following equation $T = Nd\tau$ where they say $d\tau$ is the sampling time. So my understanding is that if $T$ being the total measurement time, wouldn't that mean $N$ would be the number of samples? I'm just trying to understand this Apr 30, 2021 at 15:05

There are quite a few solutions, you can recognize that you have the square root of the rolling mean of the squared magnitude of the signal. Using pandas rolling mean this could be written as follows.

import pandas as pd;

def rolling_rms(x, N):
return (pd.DataFrame(abs(x)**2).rolling(N).mean()) **0.5


Each entry in the result contains the RMS of N contiguous samples, where i < N the result is filled with nan.

If you care more for efficiency you can do something like this

import numpy as np;
def rolling_rms(x, N):
xc = np.cumsum(abs(x)**2);
return np.sqrt((xc[N:] - xc[:-N]) / N)


this particular implementation skips the first valid entry, so if you want it, you can insert a zero at the beginning of x.

• you are squaring the values so there is no need of abs(x), just x**2
– Spas
Apr 16 at 18:08
• @Spas, what about (1+1j)**2 ?
– Bob
Apr 17 at 14:47