%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Dhanushka Amarasinghe
% EE 416
% Project No.4
% Due-11/13/01
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;

%1;

%FIR1   FIR filter design using the window method.
    %B = FIR1(N,Wn) designs an N'th order lowpass FIR digital filter
    %and returns the filter coefficients in length N+1 vector B.
    %The cut-off frequency Wn must be between 0 < Wn < 1.0, with 1.0 
    %corresponding to half the sample rate.  The filter B is real and
    %has linear phase, i.e., even symmetric coefficients obeying B(k) =
    %B(N+2-k), k = 1,2,...,N+1.
    
 %FIR2   FIR filter design using the window method - arbitrary filter shape.
    %B = FIR2(N,F,M) designs an N'th order FIR digital filter with the
    %frequency response specified by vectors F and M, and returns the
    %filter coefficients in length N+1 vector B.  Vectors F and M specify
    %the frequency and magnitude breakpoints for the filter such that
    %PLOT(F,M) would show a plot of the desired frequency response.
    %The frequencies in F must be between 0.0 < F < 1.0, with 1.0
    %corresponding to half the sample rate. They must be in increasing
    %order and start with 0.0 and end with 1.0.   



w1s=0.5*pi;                % 1st band stop
w1p=0.3*pi;                % 1st band pass
w2s=0.6*pi;                % 2nd band stop
w2p=0.8*pi;                % 2nd band pass
As=80;
width1=w1s-w1p;            % Distance between band pass and band stop
width2=w2p-w2s;            % Distance between band pass and band stop
w1c=(w1p+w1s)/2;           % 1st cutoff frequency
w2c=(w2p+w2s)/2;           % 2nd cutoff frequency 
%kaiser window
delta_f=min(width1/(2*pi),width2/(2*pi));
M=ceil((As-7.95)/(14.36*delta_f)+1);
beta=0.110*(As-8.7);			% Value of beta		
hd=fir1(500,[0.5 0.6],kaiser(501,7.843));
%hd=ideal_lp(w1c,M)-ideal_lp(w2c,M);
w_kai=kaiser(M,beta);
h=hd.*w_kai;
[H,W]=freqz(h,1,500);
%mag=20log10(abs(H));

% 3

n=0:449;									%rage of n
x=cos(0.2*pi*n)+sin(0.54*pi*n)+sin(0.56*pi*n)+cos(0.58*pi*n)+cos(0.9*pi*n)
[H,W]=freqz(x,1,256);				% Frequency response
%mag=20*log10(abs(H));
mag=abs(H);
figure(1);
plot(W/pi,mag);						% Fourier Transform
title('Fourier Transform of x');
xlabel('Frequency');
ylabel('Amplitude');

y=conv(x,h);
[H1,W1]=freqz(y,1,256);
mag1=abs(H1);
figure(2);
plot(W1/pi,mag1);

% 4: The presence of the ripples whose amplitude does 
% not change with the filter length is called Gibbs' phenomenom. 