Azzera filtri
Azzera filtri

How to calculate an exponentially weighted moving average on a conditional basis?

93 visualizzazioni (ultimi 30 giorni)
Hi, I am using MATLAB R2020a on a MacOS. I have a signal 'cycle_periods' consisting of the cycle periods of an ECG signal on which I would like to perform an exponentially weighted mean, such that older values are less weighted than newer ones. However, I would like this to be done on an element-by-element basis such that a given element is only included in the overall weighted mean if the weighted mean with the current sample does not exceed 1.5 times or go below 0.5 times the weighted mean without the element.
I have used the dsp.MovingAverage function as shown below to calculate the weighted mean, but I am really unsure as to how to manipulate the function to include my conditions.
% Exponentially weighted moving mean for stable cycle periods
movavgExp = dsp.MovingAverage('Method', 'Exponential weighting', 'ForgettingFactor', 0.1);
mean_cycle_period_exp = movavgExp(cycle_periods);
I would very much appreciate any help regarding this matter, thanks in advance.
  1 Commento
Mathieu NOE
Mathieu NOE il 10 Nov 2020
your special case is not covered in the standard matlab package
you will have to create your own , but that doesn't seems a "mission impossible"
all the best

Accedi per commentare.

Risposta accettata

Uday Pradhan
Uday Pradhan il 12 Nov 2020
Modificato: Uday Pradhan il 12 Nov 2020
Hi Cai,
In order to accomodate your custom conditions, you may think about creating a small script to mimic the algorithm used by the dsp.MovingAverage system object (exponential wieghing method). A very detailed explanation about the same is given in this link. I have created a dummy code for reference:
%Let t be a 1-D array of data points
lambda = 0.1; %forgetting factor
prevWeightFactor = 0; %initialise weight factor and moving average
prevAvg = 0;
for i = 1 : length(t)
presentWeightFactor = lambda * prevWeightFactor + 1;
presentAvg = (1 - (1/presentWeightFactor)) * prevAvg + (1/presentWeightFactor) * t(i);
if (presentAvg < 0.5 * prevAvg) || (presentAvg > 1.5 * prevAvg)
presentAvg = prevAvg; %ignore this input, you might want to skip this step for the first sample
else %accept this input in the moving average
prevWeightFactor = presentWeightFactor;
prevAvg = presentAvg;
I hope this helps!
  5 Commenti
Uday Pradhan
Uday Pradhan il 13 Nov 2020
Hello Cai,
I ran your code with the data you have provided and the implementation seems to be correct. I cannot find anything wrong in that regard. Looking at indices, say idx, at which the values are acepted I can see most of these correspond to the positions where cycle_periods(idx) is 0.0040, hence this value repeats often in x. I believe the results align with the condtions you want to establish on the input data:
lowerBound = 0.5*(x(j - 1));
upperBound = 1.5*(x(j - 1));
Our first element is 0.0040, so that makes the upper and lower bounds : 0.0020 and 0.0060. Values whose weighted average falls in this range are only accepted. These bounds do not change for a few indices hence we see 0.0040 repeatedly getting accepted.
Cai Chin
Cai Chin il 14 Nov 2020
Hi Uday, this makes so much sense now, I have removed the erroneously small values and the result is now what I wanted. Thank you very much for all your help!

Accedi per commentare.

Più risposte (0)


Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by