Create Moving Average filter WITHOUT filter() function

Hello,
I can't seem to plot a desired output using a Hanning Moving Average equation. My goal is to create a moving average filter with a 3-point moving average with a created signal x. However, I don't notice a difference between the unfiltered signal and the filtered signal. May someone give advice on how to enter a moving average equation and it's coefficients without the use of the filter() function? Or is the 3-point moving average so small that there isn't a difference?
Hanning Moving Average filter:
y[n] = 1/4(x[n] + 2x[n-1] + x[n-2]).
fs = 200; %sampling frequency
Ts = 1/200; %sampling time
t = 0:Ts:1;
x = sin(2*pi*2*t) + sin(2*pi*10*t) + sin(2*pi*90*t) %signal
plot(t,x); %unfiltered signal
title('Signal with 2, 10, and 90 Hz');
xlabel('time (s)');
%Difference equation of Hanning Moving Average filter:
n = 3
yn = (.25*x(n)) + (.5*x*(n-1)) + (.25*(n-2)) %Attempt at creating the moving average equation.
plot(t,yn)
Thank You

5 comentarios

Jonas
Jonas el 6 de Mayo de 2021
Editada: Jonas el 6 de Mayo de 2021
the second two expressions are at the moment just mutiplying with n-1 or n-2, i think you want this as index e.g. x(n-1)
edit: the x is missing completly in the last part
should be
1/4*(x(n) + 2*x(n-1) + x(n-2))
Thanks for the response. However, when I plotted this equation, nothing appeared.
Jonas
Jonas el 6 de Mayo de 2021
you have to calculate the value for every n, you have to create a y(n) array with the given formula. at the moment yn is only a single value.
Terry Carney
Terry Carney el 6 de Mayo de 2021
Editada: Terry Carney el 6 de Mayo de 2021
May you give me an example?
What I'm thinking of is:
y[1] = 1/4*(.25x + .25(-x))
y[2] = 1/4*(.5x + .5x)
y[3] = 1/4*(.75x + 1x +.25x)
Jonas
Jonas el 6 de Mayo de 2021
Editada: Jonas el 6 de Mayo de 2021
you could calculate the main part e.g. by
y=1/4*(x(3:end) + 2*x(2:end-1) + x(1:end-2))
but then you jave to think about the first two samples of y because the normal formula will have negative index. maybe you want to assume 0 before the actual x(n) starts. similar to that you have to think about two values beyond x because the formula for y contains up to n-2. this way y will be longer than your original x by two samples

Iniciar sesión para comentar.

 Respuesta aceptada

hello
try this
fs = 200; %sampling frequency
Ts = 1/200; %sampling time
t = 0:Ts:1;
x = sin(2*pi*2*t) + sin(2*pi*10*t) + sin(2*pi*90*t) %signal
plot(t,x); %unfiltered signal
title('Signal with 2, 10, and 90 Hz');
xlabel('time (s)');
%Difference equation of Hanning Moving Average filter:
yn = zeros(size(t));
%%%%%%% main loop %%%%%%%%%%%%
yn(1) = 0.25*x(1); % first iteration
yn(2) = 0.25*x(2) + 0.5*x(1); % second iteration
ind = 3:length(t); % for n>= 3
yn(ind) = (0.25*x(ind)) + (0.5*x(ind-1)) + (0.25*x(ind-2));
%Attempt at creating the moving average equation.
plot(t,x,'b',t,yn,'r')

2 comentarios

Jonas
Jonas el 6 de Mayo de 2021
providing just the solution is boring :p providing help to find the solution would be the better way ;)
@Jonas, I agree. But I was up for a couple nights trying to solve this. I do thank you for your guidance as well.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 6 de Mayo de 2021

Comentada:

el 6 de Mayo de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by