How to calculate an exponentially weighted moving average on a conditional basis?
74 次查看(过去 30 天)
显示 更早的评论
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
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
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
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!