Vous êtes sur la page 1sur 3

function

% 1. plot the frequency response in figure 1 the following window functions (41-
point
% window)
% 1.1 hanning window
n=41;
n=[-20:20]+eps;
wc=pi/4;
h=sin(wc*n)./(pi*n);
h1=hanning(n).*h';
n1=0:n-1;
[h1,w]=freqz(h1,1);
figure(1);
% 1.2 hamming window
h2=hamming(n).*h';
n2=0:n-1;
[h2,w]=freqz(h2,1);
% 1.3 bartlett window
h3=bartlett(n).*h';
n3=0:n-1;
[h3,w]=freqz(h3,1);
% 1.4 blackman window
h4=blackman(n).*h';
n4=0:n-1;
[h4,w]=freqz(h4,1);
% 1.5 boxcar window
h5=boxcar(n).*h';
n5=0:n-1;
[h5,w]=freqz(h5,1);
plot(w/pi,20*log10([h1 h2 h3 h4 h5]));grid on
legend('hanning','hamming','bartlett','blackman','boxcar');

% 2. design a 40-tapped fir filter whose cutoff frequency is 4000 hz and a


sampling rate
% of 22050 hz using the following window functions: hanning, hamming, bartlett,
% boxcar and blackman. plot the frequency response in figure 2 and the impulse
% response in figure 3.
wc=0.3628*pi;
wn=0.3628;
b11=fir1(n-1,wn,hanning(n));
b12=fir1(n-1,wn,hamming(n));
b13=fir1(n-1,wn,bartlett(n));
b14=fir1(n-1,wn,boxcar(n));
b15=fir1(n-1,wn,blackman(n));
[b11,w]=freqz(b11,1);
[b12,w]=freqz(b12,1);
[b13,w]=freqz(b13,1);
[b14,w]=freqz(b14,1);
[b15,w]=freqz(b15,1);
figure(2);plot(w/pi,20*log10([b11 b12 b13 b14 b15]));grid on;
legend('hanning','hamming','bartlett','boxcar','blackman');
n=40;
n=0:n-1;
n1=[-(n-1)/2:(n-1)/2]+eps;
b16=(sin(wc*n1)./(pi*n1)).*[hanning(n)]';
b17=(sin(wc*n1)./(pi*n1)).*[hamming(n)]';
b18=(sin(wc*n1)./(pi*n1)).*[bartlett(n)]';
b19=(sin(wc*n1)./(pi*n1)).*[boxcar(n)]';
b20=(sin(wc*n1)./(pi*n1)).*[blackman(n)]';
figure(3);subplot(231); stem(n,b16); title('hanning');
figure(3);subplot(232); stem(n,b17); title('hamming');
figure(3);subplot(233); stem(n,b18); title('bartlett');
figure(3);subplot(234); stem(n,b19); title('boxcar');
figure(3);subplot(235); stem(n,b20); title('blackman');
% 3. using the kaiser window, design the following fir filters that will satisfy
the
% following specifications whose specifications are given below. plot the
corresponding
% frequency responses (figure 5,6,7,8)
% 3.1 lowpass filter
% passband frequency : 2500hz
% stopband frequency : 3000hz
% sampling frequency: 8000hz
% 0.01 1 ? = and 0.001 2 ? =
[m1,wn1,beta1,typ1]=kaiserord([2500 3000],[1 0],[0.01 0.001], 8000);
b1=fir1(m1,wn1,typ1,kaiser(m1+1,beta1));
[b1,w]=freqz(b1,1);
figure(5); plot(w/pi,20*log10(abs(b1)));grid on; title('lowpass filter');
% 3.2 highpass filter
% passband frequency : 2500hz
% stopband frequency : 3000hz
% sampling frequency: 8000hz
% 0.01 1 ? = and 0.001 2 ? =
[m2,wn2,beta2,typ2]=kaiserord([2500 3000],[0 1],[0.01 0.001], 8000);
b2=fir1(m2,wn2,typ2,kaiser(m2+1,beta2));
[b2,w]=freqz(b2,1);
figure(6); plot(w/pi,20*log10(abs(b2)));grid on; title('highpass filter');
% 3.3 bandpass filter
% passband frequencies : 1500hz, 2500 hz
% stopband frequency : 500hz, 3000hz
% sampling frequency: 8000 hz
% 0.01 1 ? = and 0.001 2 ? =
[m3,wn3,beta3,typ3]=kaiserord([500 1500 2500 3000],[0 1 0],[0.001 0.01 0.001],
8000);
b3=fir1(m3,wn3,typ3,kaiser(m3+1,beta3));
[b3,w]=freqz(b3,1);
figure(7); plot(w/pi,20*log10(abs(b3)));grid on; title('bandpass filter');
% 3.4 bandstop filter
% passband frequencies : 500hz, 3000 hz
% stopband frequency : 1500hz, 2500hz
% sampling frequency: 8000 hz
% 0.01 1 ? = and 0.001 2 ? ='
[m4,wn4,beta4,typ4]=kaiserord([500 1500 2500 3000],[1 0 1],[0.01 0.001 0.01],
8000);
b4=fir1(m4,wn4,typ4,kaiser(m4+1,beta4));
[b4,w]=freqz(b4,1);
figure(8); plot(w/pi,20*log10(abs(b4)));grid on; title('bandstop filter');
% 4. instead of the kaiser window, use the parks mcclellan algorithm to the design
the
% fir filters given in item number 3. plot the corresponding frequency responses
% (figure 9,10,11,12).
[n5,fo5,ao5,w] = remezord([2500 3000],[1 0],[0.01 0.001], 8000);
b5=remez(n5+4,fo5,ao5,w);
[b5,w]=freqz(b5,1);
figure(9);plot(w/pi,20*log10(abs(b5))); grid on; title('lowpass filter');
[n6,fo6,ao6,w6] = remezord([2500 3000],[0 1],[0.01 0.001], 8000);
b6=remez(n6+4,fo6,ao6,w6);
[b6,w6]=freqz(b6,1);
figure(10);plot(w6/pi,20*log10(abs(b6))); grid on; title('highpass filter');
[n7,fo7,ao7,w] = remezord([500 1500 2500 3000],[0 1 0],[0.001 0.01 0.001], 8000);
b7=remez(n7+4,fo7,ao7,w);
[b7,w]=freqz(b7,1);
figure(11);plot(w/pi,20*log10(abs(b7))); grid on; title('bandpass filter');
[n8,fo8,ao8,w] = remezord([500 1500 2500 3000],[1 0 1],[0.01 0.001 0.01], 8000);
b8=remez(n8+3,fo8,ao8,w);
[b8,w]=freqz(b8,1);
figure(12);plot(w/pi,20*log10(abs(b8))); grid on; title('bandstop filter');
% 5. quantize the filter coefficients by rounding it off to 8-bit representation.
use the filter
% designed in number 3.3 and 4.3. observe the effects of quantization by plotting
their
% frequency responses figure (13)
b9=fix(b3*128)/128;
[b9,w]=freqz(b9,1);
b10=fix(b7*128)/128;
[b10,w]=freqz(b10,1);
figure(13);plot(w/pi,20*log10(abs([b9 b10]))); grid on;
legend('8-bit kaiser','8-bit parks');

Vous aimerez peut-être aussi