I have a signal of electromyographical data that I am supposed (scientific papers\' explicit recommendation) to smooth using RMS.
I have the following working code,
It is possible to use convolution to perform the operation you are referring to. I did it a few times for processing EEG signals as well.
import numpy as np
def window_rms(a, window_size):
a2 = np.power(a,2)
window = np.ones(window_size)/float(window_size)
return np.sqrt(np.convolve(a2, window, 'valid'))
Breaking it down, the np.power(a, 2) part makes a new array with the same dimension as a, but where each value is squared. np.ones(window_size)/float(window_size) produces an array or length window_size where each element is 1/window_size. So the convolution effectively produces a new array where each element i is equal to
(a[i]^2 + a[i+1]^2 + … + a[i+window_size]^2)/window_size
which is the RMS value of the array elements within the moving window. It should perform really well this way.
Note, though, that np.power(a, 2) produces a new array of same dimension. If a is really large, I mean sufficiently large that it cannot fit twice in memory, you might need a strategy where each element are modified in place. Also, the 'valid' argument specifies to discard border effects, resulting in a smaller array produced by np.convolve(). You could keep it all by specifying 'same' instead (see documentation).