Sunday, 6 September 2015

filters - Exponential moving average cut-off frequency


I am trying to implement low pass filter from this example.


What is the cut-off frequency for this type of filter? Is it Fs1α2πα, where Fs is sampling frequency?



Answer




The discrete recurrence relation given on the linked page is


y[n]=(1α)y[n1]+αx[n]


with x[n] being the input samples and y[n] being the output samples. So taking the Z-transform


Y(z)=(1α)z1Y(z)+αX(z)


H(z)=Y(z)X(z)=α1(1α)z1


To find the -3 dB corner of the filter, in terms of the normalized digital radian frequency, Ω in the range [0,π], one solves


|H(z=ejΩ3dB)|2=12=|α1(1α)ejΩ3dB|2


=α2|1(1α)cos(Ω3dB)j(1α)sin(Ω3dB)|2


which results in this equation


[1(1α)cos(Ω3dB)]2+[(1α)sin(Ω3dB)]2=2α2



After some more algebra and trigonometry, I come up with


Ω3dB=cos1[1α22(1α)]


and since


f3dB=Ω3dBπFs2


I come up with


f3dB=Fs2πcos1[1α22(1α)]


Here's some Octave/MatLab code so you can verify the result on a plot; just change your Fs and alpha as required:


Fs = 10000.0
alpha = 0.01


b = [alpha];
a = [1 -(1-alpha)];

[H, W] = freqz(b, a, 1024, Fs);

f3db = Fs/(2*pi)*acos(1-(alpha^2)/(2*(1-alpha)))

plot(W, 20*log10(abs(H)), [f3db, f3db], [-40, 0]);
grid on


Update in response to comment:


The graph is a plot of the filter's magnitude response in dB (on the y-axis) vs. input frequency in Hz (on the x-axis). The vertical line designates the -3 dB corner frequency.


Since you want to specify your -3 dB corner frequency to find the value for α, let's start with this equation from above:


[1(1α)cos(Ω3dB)]2+[(1α)sin(Ω3dB)]2=2α2


and after some algebra and trigonometry, one can get a quadratic equation in α


α2+2(1cos(Ω3dB))α2(1cos(Ω3dB))=0


which has solutions


α=cos(Ω3db)1±cos2(Ω3dB)4cos(Ω3dB)+3


of which only the + answer from the ± can yield a positive answer for α.


Using the above solution for α and the relation



Ω3dB=πFs/2f3dB


one can find α given f3dB and Fs.


Here is an updated Octave script:


Fs = 40000
f3db = 1

format long;
omega3db = f3db * pi/(Fs/2)

alpha = cos(omega3db) - 1 + sqrt(cos(omega3db).^2 - 4*cos(omega3db) + 3)


b = [alpha];
a = [1 -(1-alpha)];

[H, W] = freqz(b, a, 32768, Fs);

figure(1);
plot(W, 20*log10(abs(H)), [f3db, f3db], [-40, 0]);
xlabel('Frequency (Hz)');
ylabel('Magnitude Response (dB)');

title('EWMA Filter Frequency Response');
grid on;


W2 = [0:75] * pi/(Fs/2); % 0 to 75 Hz
H2 = freqz(b, a, W2);
W2 = W2 / (pi/(Fs/2));

figure(2);
plot(W2, 20*log10(abs(H2)), [f3db, f3db], [-20, 0]);

xlabel('Frequency (Hz)');
ylabel('Magnitude Response (dB)');
title('EWMA Filter Frequency Response near DC');
grid on;

No comments:

Post a Comment

readings - Appending 内 to a company name is read ない or うち?

For example, if I say マイクロソフト内のパートナーシップは強いです, is the 内 here read as うち or ない? Answer 「内」 in the form: 「Proper Noun + 内」 is always read 「ない...