# How calculate moving root mean square python?

Hey guys. I’m in the stage of developing my database. In this sense, I am facing difficulty in understanding the meaning of moving root mean square for each column of my dataframe

``````Xrms = dado['accX'].pow(2).rolling(window).apply(lambda x: np.sqrt(x.mean()))
``````

Plotting the vectors gives:

does this make sense?

1 Like

This should be: given a window of lets say 10 values (moving 1 value to the right at a time),

for that window compute first the square for each value then the mean of the 10 values of the window size, then the square root. Finally place the point in the cartesian graph.

Edited

1 Like

Could you demonstrate how to implement window RMS calculation?

This is the rms formula and I have edited the reply above. Xi here is one value and in the 1/n the n =10 for our window size. The rest is straightforward.

That’s my question. Using this format, I produce a vector with equal values.

Shouldnt the values of xi be changing i.e. the window rolling over with time.

1 Like

Here `n` is window of size `n` and let’s say you have an array [1, 2, 3, 4, 5] and window of size n = 2, then to calculate moving RMS for each window (x_i) or element in the array, you should have to compute the sum of squares of x_i and n-1 right shifted values (1 right shifted value in this case) in the array.

For example,

moving RMS = √( 1/n Σ x_i^2 ), where i = i to i+n-1,

moving RMS for window 1 = np.sqrt((1/2) * np.sum([1^2, 2^2])) = 1.58
moving RMS for window 2 = np.sqrt((1/2) * np.sum([2^2, 3^2])) = 2.54
moving RMS for window 3 = np.sqrt((1/2) * np.sum([3^2, 4^2])) = 3.53
moving RMS for window 4 = np.sqrt((1/2) * np.sum([4^2, 5^2])) = 4.52

moving RMS for window 5 can not be calculated.

1 Like

Modify your code with given below:

``` for i in range(len(dado['accX'])):   rms[i] = (i+window-1 < len(dado['accX'])) * np.sqrt( (1/window) * np.sum( dado['accX'].loc[i:i+window-1]**2 ) ) ```

2 Likes

Coming back here to report on the result of the plot using this code

I’m just a little curious about the behavior at the end. As the last values tend to zero, that means that the sample windows are not contained in the time series, right? so i should reduce the size of the windows

Yes, it is not getting enough windows to compute at the end. Reducing or increasing size of window depends on the number of examples in your dataset, so choose accordingly.

One thing you could do is to remove the term `(i+window-1 < len(dado[‘accX’])) * ` from the formula and it will assume sample windows to 0 that are not contained in the time series.

2 Likes