You are on page 1of 5

Untitled

1/5

Back
This page show simple Digital filter coefficient calculation method
[Hamming ] [FIR Filter] [IIR Filter]
[Hamming ]

1. Filter Design Using Hamming window satisfying below Specification.


wp
ws
tr_width
M

= 0.2*pi;
% Passband Frequency
= 0.3*pi;
% Stopband Frequency
= ws - wp;
% Transition Band Frequency
= ceil(6.6*pi/tr_width) + 1 % M = 67(filter order) , w = 6.6*pi / tr_width
% CEIL(X) rounds the elements of X to the nearest integers

towards infinity.
%
%
%
%
n
wc
hd
M-1

Rectangular(tr_width = 1.8pi/M) , Bartlett(tr_width = 6.1pi/M)


Blackman(tr_width = 11pi/M) , Hanning(tr_width = 6.2pi/M)
Hamming(tr_width = 6.6pi/M)
tr_width => M.

= [ 0 : 1 : M-1 ];
= (ws + wp) / 2
= ideal_lp(wc,M);

% Ideal LPF cutoff frequency


% hd = ideal impulse response between 0 to

subplot(2,2,1); stem(n,hd); title('Ideal Impulse Response');


axis([0 M-1 -0.1 0.3]); xlabel('n'); ylabel('hd(n)')
/****************** Ideal+-Low Pass Filter ****************************/
function hd = ideal_lp(wc,M);
% Ideal LowPass filter computation, [hd] = ideal_lp(wc,M)% hd = ideal impulse
response between 0 to M-1,
% wc = cutoff frequency in radians, M = length of the ideal filter
alpha = (M-1)/2;
n
= [0:1:(M-1)];
m
= n - alpha + eps;
% eps = 2.2204e-016
hd
= sin(wc*m) ./ (pi*m);
/***************************************************************************/
w_ham = (hamming(M))';
% w_ham = windows function
subplot(2,2,2), stem(n,w_ham); title('Hamming Window')
axis([0 M-1 0 1.1]); xlabel('n'); ylabel('w(n)')

C:\pec_works\PEC_Office\PEC_2008\Homepage\Naver_Homepage\doc\matlab.htm
2008-11-13 / 4:44

Untitled

2/5

h = hd .* w_ham; subplot(2,2,3); stem(n,h);


title('Actual Impulse Response')
axis([0 M-1 -0.1 0.3]); xlabel('n'); ylabel('h(n)')
[db,mag,pha,grd,w] = freqz_m(h,[1]);
%[db,mag,pha,grd,w] = freqz_m(hd,[1]);
%[db,mag,pha,grd,w] = freqz_m(w_ham,[1]);
delta_w = 2*pi/1000;
Rp
= -(min(db(1:1:wp/delta_w+1)))
% Actual Passband
Ripple
As
= -round(max(db(ws/delta_w+1:1:501)))
% Min Stopband
attenuation
subplot(2,2,4); plot(w/pi,db);title('Magnitude Response in dB');grid
axis([0 1 -100 10]); xlabel('frequency in pi units'); ylabel('Decibels')
/****************** freqz_m function define
***************************************/
function [db,mag,pha,grd,w] = freqz_m(b,a);
if (a =1) Fir Filter
% [db,mag,pha,grd,w] = freqz_m(b,a);
% db = Relative magnitude in dB computed over 0 to pi radians
% mag = absolute magnitude computed over 0 to pi radians
% pha = Phase response in radians over 0 to pi radians
% grd = Group delay over 0 to pi radians
%w
= 501 frequency samples between 0 to pi radians
%b
= numerator polynomial of H(z) (for FIR: b=h)
%a
= denominator polynomial of H(z) (for FIR: a=[1])
[H,w]
H
mag
db
pha
% pha
grd
% grd

= freqz(b,a,1000,'whole');
= (H(1:1:501))'; w = (w(1:1:501))';
= abs(H);
= 20*log10((mag+eps)/max(mag));
= angle(H);
= unwrap(angle(H));
= grpdelay(b,a,w);
= di

ff(pha);
% grd = [grd(1) grd];
% grd = [0 grd(1:1:500); grd; grd(2:1:501) 0];
% grd = median(grd)*500/pi;
/*************************************************************************************/
% filter length M = 67 , 52dB,

Pass Band Ripple = 0.0394

[Hamming] [IIR Filter] , [FIR Filter]

EX2 )

Fir method - LPF

C:\pec_works\PEC_Office\PEC_2008\Homepage\Naver_Homepage\doc\matlab.htm
2008-11-13 / 4:44

Untitled
clf
t
= 0 : 0.001 : 1;
= 1000
y
= sin(2*pi*30*t)+sin(2*pi*80*t)+cos(2*pi*160*t);
subplot(2,1,1), plot(y,'g'); title('time domain');

3/5

% N = (f) = 1/0.001

YY
= fft(y);
%
plot(abs(YY))
ylen
= length(YY);
fs
= 1000;
f1
= 1 : 1 : ylen/2 ;
ff
= ((f1*fs)/(ylen));
magYY
= abs(YY);
figure(1);
ylen2
= round(ylen/2-0.5);
subplot(2,1,2), plot(ff(1:ylen2), magYY(1:ylen2),'k'); title('frequency
domain');
hold on;
fn
= fs/2;
% fs =1000 ;
f1n
= 55/fn;
% fn = 500 , wc = 0.11, lowpass filter
cutoff frequency (30 + 80)/2
for k
= 1 : 5 : 100
[B]
= fir1(k,f1n);
% LPF , k = 0 ~ 100 ,f1n = 0.1100 ;
B:
filter coefficient
[H,wT]
= freqz(B,1,100); % B / 1 , N point
T
= 0.001;
herzt
= wT/(2*pi*T);
figure(1);
subplot(2,1,1), plot(y,'b'); title('time domain , input data display ');
subplot(2,1,2), plot(ff(1:ylen2),magYY(1:ylen2),'k'); title('frequency domain,
fft(frequency analysis');
hold on;
s
= int2str(k);
subplot(2,1,2), plot(herzt,abs(H)* 600,'r');title('in fft plot,Lowpass
fillter'),xlabel('Frequence(Hz)');
ylabel('Amplitude'); text( 250 + k * 8,500,s ); grid; axis([0 1000 -1 650]);
%hold off;
hold on;
pause;
figure(2)
clf;
result = filter(B,1,y);
% y = input data , B = filter coefficient ,
% result = frequency domain filter shape.
plot(result,'c');
plot( result(300:500) );
result
end

C:\pec_works\PEC_Office\PEC_2008\Homepage\Naver_Homepage\doc\matlab.htm
2008-11-13 / 4:44

Untitled
[Hamming] [FIR Filter] ,

4/5
[IIR Filter]

3. IIR Digital Filter


Define of formal - LPF ,HPF Example of butter / cheby1,2 / ellips
t
= 0:0.001:1-0.001;
y
= sin(2*pi*30*t)+sin(2*pi*80*t)+cos(2*pi*160*t);
YY
= fft(y);
ylen = length(YY);
fs
= 1000;
f1
= 1:1:ylen;
ff
= ((f1*fs)/(ylen));
magYY = abs(YY);
hold on;
for k = 1 : 2 : 17
% input frequency ; 30Hz, 80Hz, 160Hz
% [B,A] = butter(k,0.12);
% [B,A]= BUTTER(N,W) LPF ,Wn = 0.12 (W = 0 ~
0.5) ,BFP ( w1 < W < w2 )
% [B,A] = BUTTER(k,0.24,'high') % [B,A] = BUTTER(N,Wn,'high') - highpass
filter, 0.24 = 120Hz / (1000Hz/2)
% butter filter = 11 order saturation start , tr_width is width(disadvantage)
% [B,A] = CHEBY1(k,0.5,0.12)
% [B,A] = CHEBY1(N,R,Wn) N order, passband
ripple R(0.5),Wn(cut off frequency,0 ~ 1)
% [B,A] = cheby1(k,0.5,0.24,'high'); % Chebyshev type I Rp = 0.5 dB(starting
point)
% cheby1 filter = 7 order saturation start , tr_width is not width
% [B,A] = cheby2(k,20,0.12);
% Chebyshev type 2 Rp = 20 dB(starting point)
% [B,A] = cheby2(k,20,0.24,'high');
% Chebyshev type 2 Rp = 20 dB(starting
point)
% cheby2 5 order saturation start, stop band ripple is high
% [B,A] = ELLIP(k, 3, 30 ,0.12) % [B,A] = ELLIP(N,Rp,Rs,Wn),[B,A] =
ELLIP(N,Rp,Rs,Wn,'high') or 'stop' or 'W'
[B,A] = ELLIP(k,3, 30,0.24,'high')
% Ellip 3 order saturation start , Just ripple control is difficult in manual haddle.
[H,wT]= freqz(B,A,100);
T
= 0.001;
herzt = wT/(2*pi*T);
figure(1);
subplot(2,1,1),plot(y,'g'),title('time domain');
subplot(2,1,2),plot(ff(1:ylen/2),magYY(1:ylen/2),'k'),title('frequenc
y domain');
hold on;
subplot(2,1,2),plot(herzt,abs(H)*700,'r'); title('Low fillter'),grid; pause

C:\pec_works\PEC_Office\PEC_2008\Homepage\Naver_Homepage\doc\matlab.htm
2008-11-13 / 4:44

Untitled

5/5

s = int2str(k) ;text(250 + k * 8,700,s) ; grid ; axis([0 1000 -1 650]) ;


figure(2) ;
subplot(2,1,1); result = filter(B,A,y); plot(result,'c');
subplot(2,1,2) ;plot(result(300:500));
figure(3) ;
zplane(B,A) ;title('Pole-Zero Plot');
%A = [ 1 1/3], B = [ 1 -1/2] , zplane(Z(B),P(A))
% H(e) = B(zeros) / A(poles)
% Filter(B(Numberator- ZEROS),A(denominator - POLES))
% [H,W] = FREQZ(B(Numberator -zeros),A(denominator - Poles),N)
end

C:\pec_works\PEC_Office\PEC_2008\Homepage\Naver_Homepage\doc\matlab.htm
2008-11-13 / 4:44

You might also like