% 1. There are many types of FIR Filter used in DSP today. There is the windowing method used
% in the previous project. There is the Parks-McClellan method and the XXX method. With the 
% windowing method, you cannot improve the attenduation of window by increasing the order of the
% filter. 

beta2 = 0.1102*(80-8.7);
n2 = ((80-8)/(2.285*0.2*pi))+1;
fcuts = [0.3*pi 0.5*pi 0.6*pi 0.8*pi];
mags = [1 0 1];
devs = [0.01 0.05 0.01];        %Deviations from ideal
[n,Wn,beta,ftype] = kaiserord(fcuts,mags,devs,2*pi);
n2 = round(n2)+1;
hh = fir1(n2,Wn,ftype,kaiser(n2+1,beta2),'noscale');
[H,f] = freqz(hh,1,500);

% Matlab 6.1 code
% s.xunits = 'rad/sample';
% s.yunits = 'dB';
% s.plot = 'mag';

t = 0:499;
xn = cos(.2*pi*t) + sin(.54*pi*t) + sin(.56*pi*t) + cos(.58*pi*t) + cos(.9*pi*t);  
[Xn,Wx] = freqz(xn,1);

yn = conv(hh, xn);
[Yn,Wy] = freqz(yn,1);

%<<<<<<<<<<<< Test Signal >>>>>>>>>>
n = 0:499;
xx = sin(0.2*n*pi) + sin(0.5*n*pi) + sin(0.7*n*pi);
XX = fftshift(fft(xx));
YY = XX.*H';

%<<<<<<<<<<<< Plots >>>>>>>>>>>>>>>
figure(1);      %Plot of window function
plot(f/pi, 20*log10(abs(H)));
title('Kaiser Window Bandstop FIR Filter');
xlabel('Frequency - Normalized (x\pi rad/sample)');
ylabel('Magnitude (dB)');
grid;

figure(2);      %Plot of filtered test signal
subplot(2,2,1)
plot(Wx/pi, abs(Xn));
title('Test signal');
xlabel('Frequency - Normalized (x\pi rad/sample)');
ylabel('Magnitude');
grid;
subplot(2,2,2)
plot(Wy/pi, abs(Yn));
title('Test signal filtered with Kaiser Windowing');
xlabel('Frequency - Normalized (x\pi rad/sample)');
ylabel('Magnitude');
grid;
subplot(2,2,3)
plot(Wy/pi, 20*log10(abs(Xn)));
title('');
xlabel('Frequency - Normalized (x\pi rad/sample)');
ylabel('Magnitude');
axis([0 1 -100 70])
grid;
subplot(2,2,4)
plot(Wy/pi, 20*log10(abs(Yn)));
title('');
xlabel('Frequency - Normalized (x\pi rad/sample)');
ylabel('Magnitude');
axis([0 1 -100 70])
grid;

% The results of the filtering process are illistrated in figure 2. Note that the the three
% central frequencies have been attenuated as the filter responce indicates. Looking at figure
% 2, you can see that the central 3 frequency are down more then -80dB from the outer two 
% signals. When analysing the effectiveness of this filter, the design goals have been overshot.
% The noise floor is at approximatly 15dB but our filter attenuates the signals in the stop-
% band to approximatly -40dB. A redesign of the filter would reduce the amount of attenuation to
% reduce complexity and delay of filter.

% The gibbs phenomenon is caused by the discontunuities of truncating and infinite sequence. This
% truncation causes a ripple in the frequency responce of the filter. Unfortunatly, increaseing
% the filter order will not have any effect on the amplitude of the ripple. This phenomenon is 
% very prevelent when using a rectangular window. If less ripple is desired, a different windowing 
% function should be chosen.