# Adjustment of window length with sliding/moving RMS method for frequency drifts

I'm using a sliding RMS mechanism to compute RMS of a signal, i.e. with a window size of N, I add the squared value of the new sample $x[n]$ to the running total while deducting the squared value of the $x[n - N]$ sample and then performing a square root to get the RMS value.

This method works fine when a cycle's worth data in the signal under question lines up exactly with the length of the window. For ex,

Let the window length $N = 512$, frequency of the signal $f=100 \ \mathrm{Hz}$ and sampling frequency $f_s= 51200 \ \mathrm{Hz}$.

With the above settings, every set of 512 samples would have a cycle's worth data of the signal and the computation works fine.

Now, the issue begins when the frequency of the signal deviates from $100 \ \mathrm{Hz}$ giving more or less number of samples in the same 512 samples window which throws off the accuracy of the sliding RMS algorithm.

This happens because the division by 512 to get the RMS, is no longer valid due to the fact that this particular cycle of the signal is getting complete before the end of the 512 samples window. However, the running squared total would already have the contributions from the previous samples which were divided by 512 while the samples of this cycle would require a different window length.

Is there a way to compensate for the addition/lack of samples due to the frequency drifts while using this technique?

• do you have knowledge about the "instantaneous" frequency of your signal or should the algorithm work for any signal form? – Maximilian Matthé Feb 23 '17 at 15:27
• The algorithm is strictly for sinusoids & not for any arbitrary signals.If by "instantaneous", you mean frequency at every sample, then no. I get the frequency only every half or full cycle. It's possible for me to 1st get the frequency of a cycle(and subsequently the window length) & then proceed to use the algorithm on all it's samples. The same is repeated for each cycle. – John Smith Feb 23 '17 at 15:40
• you need to make your window width much wider than any anticipated period of your signal. then the sliding r.m.s. won't be much different than the '"perfect" which will be an integer multiple of the signal period. – robert bristow-johnson Feb 24 '17 at 3:09
• @robertbristow-johnson Increasing the window width to an extent which is much wider than the period poses an issue wherein if the magnitude of the signal were to change, it would require much more time/ many more samples for the RMS to reflect this change which makes it seem that the change in magnitude is "slow" even if the change happened in just one cycle. – John Smith Feb 24 '17 at 3:44
• sure, it makes the envelope slower. this depends on the discipline one is working in, but in audio and computer music, the envelopes have a bandwidth that is, at least, a couple of octaves less than the fundamental frequency of the waveform. at least after the attack portion of a note. i s'pose what one can do is run contemporaneously a pitch detector on the input and adjust the width of the smoothing window to be have 2 periods of length and have complementary properties (two adjacent windows overlap and add to 1). – robert bristow-johnson Feb 24 '17 at 3:55

• another thing you can do, after the DC-blocking filter, is run the signal through a Hilbert transform (a causal filter of known delay) and delay the original by the same delay, so you have a Hilbert pair (a.k.a. "Analytic Signal"). one will be a sine wave the other a cosine wave and you can square both and add the squares and square-root. that will give you $\sqrt{2}$ times the RMS of your sine. and you can update it sample-by-sample. – robert bristow-johnson Feb 24 '17 at 18:52