%Ridwan A Kabir
%EE 416
%Project 4;
%Date-11/13/2001

%************************************************************************************
%Part 1;
%  1.FIR1:
%   It 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.By default, the filter is scaled so the center of the first pass band 
%   has magnitude exactly one after windowing.

%  2.FIR2:
%   This isfilter 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. 
 
%   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.
 
%   By default FIR2 uses a Hamming window

%************************************************************************************
%Part2:

clear all;
w1s=0.5*pi;
w1p=0.3*pi;
w2s=0.6*pi;
w2p=0.8*pi;

As= 80;

width1=w1s-w1p;
width2=w2p-w2s;
w1c=(w1p+w1s)/2;
w2c=(w2p+w2s)/2;
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);
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=20*log10(abs(H));

%************************************************************************************
%Part3:


n=0:449;
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);
mag=abs(H);
%figure(1);
%plot(W/pi,mag);
title('Fourier Transform of x');
xlabel('frequency');
ylabel('amplitude');

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

%************************************************************************************
%Part 4;
%Expolanation of Gibb's Phenomenon:

%The sinusoidal components of the signal that occur at multiples of the fundamental
%frequency are called harmonics. In general, for well-behaved (continuous) periodic 
%signals, a sufficiently large number of harmonics can be used to approximate the signal 
%reasonably well. For periodic signals with discontinuities, however, such as a periodic 
%square wave, even a large number of harmonics will not be sufficient to reproduce the 
%square wave exactly. This effect is known as Gibbs phenomenon and it manifests itself in
%the form of ripples of increasing frequency and closer to the transitions of the square signal. 
