# Average of discrete periodic signal

I am working on a project which, at some point, relies on the computation of the average of discrete signals, which typically look like this:

The project generates hundreds of such signals, and:

• the signals are generated "on-the-fly", meaning I can monitor the signal while it's being produced, and stop it once I have obtained an accurate estimate of the average value of the periodic part (the sooner being the better)
• the duration of the transient part is unknown
• the amplitudes can vary a lot
• I have no control on the final frequency of the signal
• I have no control on the sampling ratio, however it is assumed to be high enough
• on rare occasions, the periodic part can degenerate into a constant signal

My question is the following: is there a proper way (involving fourier transform, moving averages, moving averages of moving averages...) to automatically determine the average value of the periodic part of each signal ?

I came up with something weird that involves computing a moving average of moving average, then computing the derivative of the latter, and waiting until its amplitude becomes close enough to zero. Although it seems to provide decent results, it really sounds weird, and it requires a large amount of samples to provide a decent estimate of the average.

EDIT

Testing the solution of @Dan Boschen, chaining multiple exponential moving averages works like a charm (here 3 EMA, each with alpha = 0.01):

• Can you clarify what you want the average of? Since the average of the periodic part of the signal would be zero so I assume you actually mean something else— the average frequency? May 16, 2022 at 1:49
• @DanBoschen thank you for answering. I indeed want the average of the periodic part only, which is actually not zero (in the plot it is something close to -0.025). My problem being that I am looking for an automatic way to determine it, given the restrictions I listed above. May 16, 2022 at 6:46

It appears the OP would like to estimate the mean of the signal efficiently. A moving average will provide the best estimate under condition of white noise; assuming that is the case, the CIC (Cascade-Integrator-Comb) filter is an efficient implementation of the moving average. Further the result can be decimated which the CIC provides.

Under other non-white noise conditions, an exponential averager can also be considered. Or specialized nulling filters if there is noise at a very specific frequencies.

Both of these filter implementations are explained in more detail at this link. The exponential averager as given by the following transfer function:

$$H(z) = \frac{(1-\alpha)}{1 - \alpha z^{-1}}$$

Has a 3 dB cutoff in Hz given by:

$$f_c = \frac{f_s}{2\pi} \cos^{-1}\bigg(\frac{-\alpha^2 + 4\alpha -1}{2\alpha}\bigg)$$

Which was determined by setting $$|H(z)|^2 = 1/2$$.

As an efficient decimating structure, the CIC is implemented as follows:

The waveform is first accumulated (the accumulator wraps on overflow), this is then followed by a decimator where the Dth output is selected, and then at this lower output rate ($$f_s/D$$ where $$f_s$$ is the input sample rate), each successive sample is differenced. As detailed in the referenced link above, this is identical to a moving average over D samples! The accumulator is a digital integrator, and the differencing filter is known as a "comb filter", thus it is called a "Cascade Integrator Comb" filter.

The implementation of multiple moving averages is also easily implemented by adding more integrator and combs as shown in the diagram below for a $$CIC^2$$ structure of two moving averages in cascade.

The accumulators are sized so that they only overflow once for each output sample, otherwise the result would be corrupted.

• thank you for your answer. Being a novice to this domain, I am not certain to grasp all the details of what you propose. I understand the idea of the exponential moving average filter. In my case (initial transient part of unknown duration + not knowing the frequency of the periodic part of the signal), is there a specific way to chose the decay factor ? Can I still use a criterion based on the amplitude of the derivative of the filter output to stop my computation ? May 16, 2022 at 9:27
• Does your signal change in amplitude once it starts or can you trust that it maintains the same amplitude each time (even though different each time)? May 16, 2022 at 9:40
• Once it has reached the periodic part, the maximal amplitude does not change anymore May 16, 2022 at 9:42
• Ok then yes, monitoring the derivative is an excellent approach. I'll add the equation for the cutoff frequency for the exponential averager; the settling time is related to the inverse of the cutoff frequency. You can estimate the 10% to 90% settling time as $0.25/BW$ where $BW$ is the bandwidth in Hz. Note for the CIC, the $z^{-1}$ block is just a unit sample delay, and the $D$ with a down arrow just means select every Dth sample- it is quite simple! May 16, 2022 at 9:50
• Just tested to chain multiple EMA with low alpha value (0.01), it seems to work like a charm. So my initial guess of chaining standard moving averages was not that stupid after all ! Is there a preferred strategy regarding the alpha values of each EMA ? May 16, 2022 at 12:48