1
$\begingroup$

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:

enter image description here

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?

$\endgroup$
2
  • $\begingroup$ you're misinterpreting what $N$ is in that formula. $\endgroup$ Commented Apr 29, 2021 at 20:29
  • $\begingroup$ @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 $\endgroup$ Commented Apr 30, 2021 at 15:05

1 Answer 1

3
$\begingroup$

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.

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

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