How to calculate an exponentially weighted moving mean and specify the time constant over which weights are applied?

9 views (last 30 days)
Hi, I am using MATLAB R2020a with MacOS. I am trying to find the exponentially weighted moving mean of the cycle period of an ECG signal, and have used the dsp.MovingAverage function from the DSP signal processing toolbox, and called the commands shown. However, I also want to specify the number of elements in a vector over which the weights are applied, with less weight being applied to 'older' elements. When I call the 'WindowLength' command in the dsp.MovingAverage function, it gives a warning:
movavgexp = dsp.MovingAverage(10, 'Method', 'Exponential weighting', 'ForgettingFactor', 0.1);
Warning: The WindowLength property is not relevant in this configuration of the System
object.
Is there another method I can use to specify the 'time constant' and calculate the exponentially weighted moving mean? Thanks in advance!

Answers (1)

Uday Pradhan
Uday Pradhan on 12 Nov 2020
Edited: Uday Pradhan on 12 Nov 2020
Hi Cai,
The warning is shown because the syntax used is not proper. At present, there are two algorithms supported by the dsp.MovingAverage system object, one is the sliding window algorithm and the other is exponential weighing, as an example the correct syntax for specifying these are respectively:
movavgWindow = dsp.MovingAverage(10);
movavgExp = dsp.MovingAverage('Method','Exponential weighting',...
'ForgettingFactor',0.9);
In your code, you seem tohave mixed the two, hence the warning.
In my understanding you would like to specify a certain fixed number of elements, let's say k, over which you want to calculate the moving average but with exponential weighing algorithm. Now I am assuming that, you would want to partition your data into k independent portions i.e. the average taken over t(i+1), t(i + 2) ,..., t(i+k) is independent of the average taken over t(i+k+1), t(i+k+2) , ... , t(i + 2k), where t is a 1 - D vector of data points.
In that case, you can use the "reset" function of the dsp.MovingAverage system object. Note that:
reset(movavgexp) % Resets the System object to the initial values for that object.
As an example:
for i = 1:length(t) %loop over the data points
y = movavgexp(t(i));
if mod(i,k) == 0
%now you can reset the object to calculate avg independent of the last data points
%y contains the moving average over last k points, so you may want to store it
reset(movavgexp);
end
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!