% Jonathon Panek   
% EE 416
% Project 4
% Due 11/13/01
% ========================================================================
% Four types of FIR filters:
%
%   1. Type 1 -  Odd Length, Odd Symmetry  
%   2. Type 2 -  Odd Length, Even Symmetry 
%   3. Type 3 -  Even Length, Odd Symmetry 
%   4. Type 4 -  Even Length, Even Symmetry 
%
% ========================================================================
    clear all;
% Given a Type 1 FIR filter with
    Rp=1;
    A=80;
    w1p=0.3*pi;
    w1s=0.5*pi;
    w2s=0.6*pi;
    w2p=0.8*pi;
    wc1=(w1s+w1p)/2;
    dw1=w1s-w1p;
    wc2=(w2s+w2p)/2;
    dw2=w2p-w2s;
    df=min(dw1/(2*pi),dw2/(2*pi)); 
% Type 1 filter (odd length, odd symmetry) -> h(n) using Kaiser window
    n = 30;
    M=ceil(((A-7.95)/(14.36*df))+1);
    B=0.1102*(A-8.7);
    h=fir1(M,[.4 .7],'stop',kaiser(M+1,B));
    [H,W]=freqz(h,1,2001);
    subplot(3,1,1);
    plot(W/pi,20*log10(abs(H))), grid;
    title('Kaiser Window');
    xlabel('W1/pi');
    ylabel('|H(e^jw)| (dB)');  
% Verify the design
% y(n)=x(n)*h(n)
    n=0:1:499;
    x=cos(.2*pi*n)+sin(.54*pi*n)+sin(.56*pi*n)+cos(.58*pi*n)+cos(.9*pi*n);
    [X,W]=freqz(x,1,512);
    subplot(3,1,2);
    plot(W/pi,abs(X));
    title('Before Filter');
    xlabel('W1/pi');
    ylabel('|X(e^jw)| (dB)'); 
    y=conv(x,h);
    [Y,W2]=freqz(y,1,512);
    subplot(3,1,3);
    plot(W/pi,abs(Y));
    title('After Filter');
    xlabel('W1/pi');
    ylabel('|X(e^jw)H(e^jw)| (dB)'); 
% Gibbs Phenomenon
% =====================================================================
% In a truncated Fourier series, more terms in the periodic expansion
% of a function x(t) results in more ripples in the expansion interval.
% The ripples decrease in size near the center and those on the ends
% become narrower.The narrow lobes occur at the discontinuities of the 
% periodic expansion of the function.This is known as Gibbs phenomenon.