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

30 ビュー (過去 30 日間)
Cai Chin
Cai Chin 2020 年 11 月 9 日
コメント済み: Cai Chin 2020 年 11 月 14 日
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 件のコメント
Mathieu NOE
Mathieu NOE 2020 年 11 月 10 日
hello
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

サインインしてコメントする。

採用された回答

Uday Pradhan
Uday Pradhan 2020 年 11 月 12 日
編集済み: Uday Pradhan 2020 年 11 月 12 日
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;
end
end
I hope this helps!
  5 件のコメント
Uday Pradhan
Uday Pradhan 2020 年 11 月 13 日
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 2020 年 11 月 14 日
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!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by