# Analysis of the ratio of two moving average filters

I have created a basic segmentation algorithm for 1-D signals (e.g. audio) based on a ratio of energy averages. The classification rule involves comparing the signal energy measured over a short time window divided by that over a longer window, with a fixed threshold.

More precisely, if $$x[n] > 0$$ is the instantaneous signal power at index $$n$$, then the quantity described above is:

$$y[n] = \frac{\frac{1}{K} \displaystyle\sum_{i = 0}^{K-1} x[n-i]}{\frac{1}{L}\displaystyle\sum_{j = 0}^{L-1} x[n-j]} = \frac{L}{K} \left(1 + \frac{\displaystyle\sum_{j=K}^{L-1} x[n-j]}{\displaystyle\sum_{i=0}^{K-1} x[n-i]}\right)^{-1},$$ for fixed integers $$K$$, $$L$$ with $$L \gg K > 1.$$

I am interested in the behaviour of this nonlinear system, since it seems quite useful in practice. Can anyone suggest a way to analyse it theoretically?

• This is similar to stock market technical analysis where buy and sell signals are determined by the crossovers in moving averages of different length. May 2 at 12:44
• Yes, I did come across this application while googling. May 2 at 14:58

This depends a bit on what you want to get our of the analysis

Step 1: Instantaneous power

You square the signal. That changes the spectrum considerably. In particularly you need to watch out for aliasing. Let's say your signal is a 15kHz sine wave sampled at 48 kHz. If you square this you get one component at DC (0 Hz) and another at 30 kHz. However, the 30 kHz component will alias down to 18 kHz, so your squared spectrum is outright wrong in this case. To be safe, you should either up-sample the signal by a factor of 2 before squaring OR lowpass filter it at a quarter of the sample rate. Many "natural" audio signals don't have a lot of energy at high frequencies, so could also choose to just live with the aliasing error, but that's an application specific decision.

The physical interpretation of instantaneous power is complicated. Let's say your input signal is the voltage at the terminals of a loudspeaker: Squaring it does NOT represent the instantaneous power. The actual instantaneous power is the product of the voltage and the current waveform. Since a loudspeaker is a reactive load, this will be quite different from the squared voltage. For example, it's perfectly normal for the instantaneous power to be negative which the square of a signal can never be. Negative instantaneous power simple means that the load has energy storage and that at times it can feed stored energy back into the source.

Going back to the sine wave example. A squared sine creates a component at DC and one at double the frequency. One possible interpretation: the DC component reflects the actual power consumed, the double frequency component represents power that gets exchanged between the source and the load but not consumed: it just gets shoved back and forth.

Provided that the signal is the "actual" instantaneous power, than the mean of it will represent the average power and the integral will represent the real energy consumed (exactly).

Step 2: Moving average

Moving average is just a low pass filter where the length of the averaging window determines the "cutoff" frequency. Unfortunately it's a rather poor one. It attenuates the "higher" frequency in the spectrum but very unevenly. Personally I would assume that in most cases a simple IIR filter would work much better.

A low pass filter will eliminate the "back and forth components" of the power signal but keep the "actual consumption". The choice of cutoff frequency (or time constant) should be tied to the physical process you want to observe. I.e. could be to time constant of the power supply storage or the thermal time constant of the system.

Step 3: Dividing averages for different time constants

This creates the ratio of "short term power " vs "long term power" where you can define "short" and "long" by the cutoff frequencies of your low pass filter. Whether that's useful or not depends on your application. It's a relative metric: it shows how "spikey" your energy consumption is.

Since real instantaneous power can be negative, it's conceivable that the lowpass filtered version can be negative or zero as well, which can make the division problematic.

• Hi, thanks for your answer. I realise I have used the terms 'energy' and 'instananeous power' a bit loosely in my question. I am considering audio data in this case, and am not too worried about aliasing, since most of the energy will be below fs/4. And I agree that a moving average is a poor man's low pass filter. However, my question arises because I have stumbled upon a technique that seems to work reasonably well, and I was curious about the part involving the ratio of short and long term powers. Is there any theory dealing with the ratio of two FIR (or IIR) filter outputs? May 2 at 15:14

This technique is similar to a classical signal processing in seismic being an intendance of "first break picking". It was proposed in First arrival picking on common-offset trace collections for automatic estimation of static corrections, Françoise Coppens, 1985 (a former colleagues of mine). You may find the name STA/LTA (short-term average/long-term average). Other details at History of First Break Picking.

I am providing this reference for two reasons:

• this may put you on track to other techniques applicable to your signal.
• as your signal is low-pass, you may be interested in their continuous formalism, that may better exhibit some features your are looking for: $$F(\tau) = \frac{\int_{\tau-L}^{\tau}S^2(t)\mathrm{d}t}{\int_{0}^{\tau}S^2(t)\mathrm{d}t}$$

There are many variations like using different windows sizes, transforming the variables, for instance with powers.

$$F(\tau) = \frac{\int_{\tau-L}^{\tau}|S|^p(t)\mathrm{d}t} {\int_{0}^{\tau}|S|^p(t)\mathrm{d}t}$$

I don't have in mind a theoretical analysis, but I can look it up. I would also suggest to invest in the literature on onset detection in audio or music, for instance the overview in:

and on change detection.

• Thank you! This is the kind of thing I was looking for. I was about to post a continuous-time formulation of my equation but you have basically nailed it. I am still interested to see if there is any theoretical material regarding this kind of ratio, but perhaps I can look it up now. May 2 at 23:38
• I have added a reference on onset detection in audio and music May 3 at 9:35
• What a coincidence! I did a research project on beat tracking around four years ago and I actually have a copy of the paper you linked sitting here on my laptop. Of course, this would be a great feature to use for onset detection. Great reference! I'm still interested in discovering any theory behind ratios of filters though. May 3 at 14:07
• Sure. The second author (L. Daudet) is somebody I know well, I would not let this paper unmentioned :) May 3 at 14:41