% Kevin Brenon
% 26785079
% EE 416
% 11/13/01

% MATLAB Project 4: Kaiser Window


% 1. Explain the different types of FIR filters clearly 
%    citing the difference between each.

% TYPE I 
% A type I filter has a symmetric impulse response of:

% h(n) = h[M-n],    0 <= n <= M

% where M is an even integer.  The delay M/2 is an integer.
% The frequency response is:

% H(e^jw) = e^(-jwM/2)*the sum of a[k]cos(wk) from k=0 to M/2

% where

% a[0] = h[M/2],
% a[k] = 2h[(M/2)-k],   k=1,2,...,M/2.

% and Beta is equal to either 0 or pi.

% TYPE II
% A type II filter has the same impulse response as a
% type I, except M is an odd integer.  The delay M/2
% is an integer plus one-half and Beta is the same as
% type I.  The frequency response is:

% H(e^jw) = e^(-jwM/2)*the sum of b[k]cos[w(k-0.5)] 
%           from k=1 to (M+1)/2

% where

% b[k] = 2h[(M+1)/2-k], k=1,2,...,(M+1)/2

% TYPE III
% A type III filter has a asymmetric impulse response of:

% h[n] = -h[M-n],   0<=n<=M

% with M an even integer, the delay M/2 an integer, and 
% Beta equal to either pi/2 or 3pi/2.  The frequency
% response is:

% H(e^jw) = je^(-jwM/2)*the sum of c[k]sin(wk) from k=1 to M/2

% where

% c[k] = 2h[(M/2)-k], k=1,2,...,M/2

% TYPE IV
% A type IV filter has the same impulse response as a type
% III, except M is an odd integer and the delay M/2 is an
% integer plus one-half and the same Beta.  The frequency
% response is:

% H(e^jw) = je^(-jwM/2)*the sum of d[k]cos[w(k-0.5)] 
%           from k=1 to (M+1)/2

% where

% d[k] = 2h[(M+1)/2-k], k=1,2,...,(M+1)/2


% 2. Implement the filter h(n) using a Kaiser Window and 
%    verify the results.

w1p = 0.3*pi;       % cutoff frequencies
w1s = 0.5*pi;
w2p = 0.6*pi;
w2s = 0.8*pi;
Rp = 1;             % Passband ripple
As = 80;            % Stopband attenuation
w1 = w1s-w1p;
w2 = w2s-w2p;
w1c = (w1p+w1s)/2;                  % filter cutoffs
w2c = (w2p+w2s)/2;
deltaf = min(w1/(2*pi),w2/(2*pi));
M = round(((As-7.95)/(14.36*deltaf))+1);
beta = 0.1102*(As-8.7);
wk = kaiser(M+1,beta);                  % kaiser window
lp = fir1(M,w1c/pi);                    % lowpass filter
hp = fir1(M,w2c/pi);                    % highpass filter
hd = hp-lp;
h = hd.*wk';                            % filter h(n)
figure(1);
freqz(h)
title('2. FIR Type I Filter h(n) using a Kaiser Window');

% the function fir1 by itself is used to verify the results
hv = fir1(M,[0.3 0.5 0.6 0.8]);
figure(2);
freqz(hv)
title('2. Verification of FIR Type I Filter h(n)');
% The filter designed using the Kaiser window is a nice bandpass
% filter with a flat center of the passband but 


% 3. Plot X(e^jw) and Y(e^jw). Explain results.

n = [0:51];
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);
[X,nx] = fouriert(x,n);
XdB = 20*log10(abs(X));
figure(3);
plot(nx,XdB),grid on
title('3. The Plot of X(e^(jw))');
ylabel('Magnitude of X(e^(jw)) (dB)');
xlabel('n');
y = h.*x;
[Y,ny] = fouriert(y,n);
YdB = 20*log10(abs(Y));
figure(4);
plot(ny,YdB),grid on
title('3. The Plot of Y(e^(jw))');
ylabel('Magnitude of Y(e^(jw)) (dB)');
xlabel('n');
% Y(e^jw) seems to be somewhat of a lowpass filter, derived from the bandpass
% filter h(n).

% 4. Explain Gibbs phenomenon.

% Gibbs Phenomenon is the ripples that occur at the cutoff 
% frequencies when trying to design ideal filters.  It is a 
% product of fourier series in which oscillations increase newar the point of discontinuities.