Need betterment for the Moving average filter

Hello, I made a moving average filter that has five sub windows say P1,p2,p3,p4,p5 where p1 has the current 500 values of the signal and p2:previous 500 values ,p3:500 values prior to p2,p4:500 values prior to p3,p5:500 values prior to p4 as shown in the below code and also it replaces the current 500 values of the signal with the average of the 2500 values i.e,p1(current values)+p2+p3+p4+p5.
clc;
clear all;
nm=load('signal1.mat');
nm1=nm.nomotion(4000:40000,:);
p1=zeros(500,1);
p2=p1;
p3=p2;
p4=p3;
p5=p4;
for i=1:500:length(nm1)-500
if (i<500)
p5=nm1(i:i+499,:);
out1(i:i+499,:)=p5;
elseif (i<1000)
p4=nm1(i:i+499,:);
out1(i:i+499,:)=p4;
elseif(i<1500)
p3=nm1(i:i+499,:);
out1(i:i+499,:)=p3;
elseif(i<2000)
p2=nm1(i:i+499,:);
out1(i:i+499,:)=p2;
else
p1=nm1(i:i+499,:);
for k=1:1:length(p1)
out(k,1)=(p1(k)+p2(k)+p3(k)+p4(k)+p5(k))/5;
end;
p5=p4;
p4=p3;
p3=p2;
p2=out;
out1(i:i+499,:)=out;
end;
end;
plot(out1);hold on;plot(nm1,'-r');
The result after averaging is shown below with red showing averaged signal and blue showing input signal.But I think the averaging is not done properly.
can someone explain how to make a better moving average as mentioned above and the file is attached.

Answers (1)

You could use conv().
blurredSignal = conv(signal, ones(WindowWidth)/WindowWidth, 'same');

5 Comments

I am confused with using conv for averaging the four singnals(p1,p2,p3,p4,p5 because in realtime I will be getting 500 values into matlab through serial port for which I am trying to save the last 2000 values(last four signals)which should be avergaed with current values and replace them with average values.
Can I know how to use this conv for the above implementation.
conv does a moving average, in other words an average within a single signal. If you want to average the last 500 elements of 4 separate signals you need to do
averagedSignal = (p1(end-499:end)+...
p2(end-499:end)+...
p3(end-499:end)+...
p4(end-499:end)+...)/4;
Yes, I used this
averagedSignal(1:500,:) = (p1(1:500,:)+p2(1:500,:)+p3(1:500,:)+p4(1:500,:)+p5(1:500,:))/5;
p5=p4;
p4=p3;
p3=p2;
p2=averagedSignal;
out1(i:i+499,:)=averagedSignal;
The result is
Problem: First I assumed that every pulse will be of length 500 and averaging the last 2000 samples + present 500 will give out the averaged pulse but I am wrong as each pulse signal is not contianing the exact 500 samples(or values) as shown inthe below image
Can you suggest a method that will identify/detect each pulse on some parameters and save those values of a pulse into P1,the next detected pulse values into another p2 and son on upto p4 such that p1,p2,p3,p4 contains the last values corresponding to the last 4 pulses.
Can you suggest me method for implementing the above mentioned or any other fro the reuired.
No, probably not, unless I spent more time trying to figure out what's going on with your code than I can spend. Maybe someone else will. Sorry.

This question is closed.

Products

Asked:

on 28 Dec 2013

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!