The following is a hard coded 3-point weighted symmetric moving average filter:
ECG(:,1) = time; %initialization of the data, rather crude, I have yet to streamline it: the important part is N.
%Attached is the ECG data: The first column is the time vector, and the second column is the voltage vector of the signal.
ECG(:,2) = Amplitude; N = length(ECG); A = ECG(:,2); time = ECG(:,1); % 3 point Weighted Symmetric filter
for i = 2 : (N-1) %Filtering starts at the second point since you cannot gather data from point 1 since there is no previous point.
%We also ignore the last point for similar reasons
W3(i-1)= (A(i-1) + 2*A(i) + A(i+1))/4; %Adds weights of one to the first and last points in the filter, and increases the weight value by 1 as you move
%closer to the center point. Thus a 5 point filter would have weights of 1,2,3,2, and 1, respectively.
%You then have to divide the entire thing by the sum of the coefficients, which in this case is 1+2+1 = 4.
%If this was a five point filter, it would be 1+2+3+2+1 = 9 that the expression would be divided by.
endplot (time (2 : (N-1)), W3, 'g') %Then all that's left is to plot the data, with necessary labels and a title
xlabel('Time (sec)');ylabel('Amplitude (Volts)');title('3 Point Weighted Symmetric Filter');
So my assumptions for how an n-point weighted symmetric moving average filter would function are as follows:
% n point Weighted Symmetric filter: Let's assume n is set equal to 3, so that it is easy to follow from the first code
reach = n - ceil(n/2) %How far out the filter "averages". For instance, a 3 point filter would have 3-ceil(3/2) = 3-3 = 1 neighboring point both before
%and after the point of interest that it would be averaging over.
for i = ceil(n/2) : (N-1)WN(i-reach)= (reach*A(i-reach) + (reach+1)*A(i) + reach*A(i+reach))/((3*reach)+1); %??? Yeah, it gets tricky for me at this point.
endplot (time (ceil(n-2) : (N-1)), W3, 'g') %Sure enough, our final goal is to plot this data
xlabel('Time (sec)');ylabel('Amplitude (Volts)');title('n Point Weighted Symmetric Filter');%}
My final goal is to create a weighted symmetric moving average filter that has a modular number of points over which it can average. The part that really gets me is the weighting itself, and while I'm sure that a nested for loop of some kind would do the trick, I can't see how I would even start something like that.
Thank you for taking the time to inspect my question, any feedback would be greatly appreciated.
Regards,
Luke Pretzie
Best Answer