% m-file file name: parbpf.m % FUnction for designing FIR bandpass filter using Parzen window. % Written by Dr. James S. Kang, Professor % Department of Electrical and Computer Engineering % California State Polytechnic University, Pomona % N = length (order) of the filter (impulse response). % f1bpf = lower passband cutoff frequency. % f2bpf = upper passband cutoff frequency. f1bpf= (N-1)/4 + 1 & k <= 3*(N-1)/4 + 1 w(k) = wa(k); else w(k) = (1/2)*(1-2*abs(k-1-(N-1)/2)/(N-1)); end end for k = 1:N t(k) = k-1; z(k) = 0; if w(k) >= 0 Hi(k) = w(k); Lo(k) = 0; else Hi(k) = 0; Lo(k) = -w(k); end end figure stem(t,w);grid; %errorbar(t,z,Lo,Hi,'w') title('Window Function') xlabel('Time') ylabel('Amplitude') disp('The window function is') disp(w) % % Calculating and plotting the impulse response of the FIR filter. % for k = 1:N h(k) = hds(k)*w(k); end disp('Coefficients of the Impulse Response (Not Normalized)') disp(h); figure stem(h);grid; title('Impulse Response (Not Normalized)') xlabel('Time') ylabel('Amplitude') % % Normalization % qc=2*pi*(f1bpf+f2bpf)/(2*fs); HRealc=h(1); HImagc=0; for k = 2:N HRealc = HRealc + h(k)*cos(qc*(k-1)); HImagc = HImagc - h(k)*sin(qc*(k-1)); end magc = sqrt(HRealc^2+HImagc^2); for k=1:N h(k)=h(k)/magc; end disp('Coefficients of the Impulse Response (Normalized)') disp(h); figure stem(h);grid; title('Impulse Response (Normalized)') xlabel('Time') ylabel('Amplitude') % % Calculating and plotting the frequency response. % M = 400; for n = 1:M+1 f(n) = (n-1)/M; q(n) = pi*f(n); if rem(N,2) == 1 H(n) = h((N-1)/2+1)+2*h(1)*cos(q(n)*(-(N-1)/2)); else H(n) = 2*h(1)*cos(q(n)*(-(N-1)/2)); end if rem(N,2) == 1 endk = (N-1)/2; else endk = N/2; end for k = 2:endk H(n) = H(n) + 2*h(k)*cos(q(n)*(k-1-(N-1)/2)); end H(n) = exp(-j*q(n)*(N-1)/2)*H(n); mag(n) = abs(H(n)); dB(n) = 20*log10(abs(H(n))); if dB(n) < -200 dB(n) = -200; end pha(n) = angle(H(n))/pi; end figure plot(f,mag);grid; title('Magnitude Response') xlabel('Frequency (1 = pi = fsampling/2)') ylabel('Magnitude') figure plot(f,dB);grid; title('Magnitude Response in dB') xlabel('Frequency (1 = pi = fsampling/2)') ylabel('Magnitude in dB') figure plot(f,pha);grid; title('Phase Response') xlabel('Frequency (1 = pi = fsampling/2)') ylabel('Phase in Radians/pi') % % Calculating and plotting poles and zeros. % nozeros = 0; k = 1; while abs(h(k)) < 10^(-10) & k == nozeros + 1 & k < (N-1)/2 nozeros = nozeros + 1; k = k+1; end stop = N - 2*nozeros; for k = 1:stop h1(k) = h(k+nozeros); end dz=ROOTS(h1); for k = 1:stop-1 dp(k) = 0; end for m = 1:201 x(m) = (m-101)/100; ty(m) = sqrt(1-x(m).*x(m)); by(m) = - ty(m); end figure plot(x,ty) grid on hold on plot(x,by) plot(real(dp),imag(dp),'rx') plot(real(dz),imag(dz),'bo') axis equal title('Digital Poles and Zeros') xlabel('Real Part') ylabel('Imaginary Part') disp('Digital Pole Locations') disp(dp) disp('Digital Zero Locations') disp(dz)