% Kevin Brenon
% 26785079
% EE 416
% Project 2
% October 25,2001

% 1. Read the sample.wav file.

x0 = wavread('sample.wav');         % The function wavread was used to read
                                    % the sampled data in the sample.wav file.
                                    
% 2. Zero pad the given wave file with a practical number of zeros.

x1 = x0(1:256);                     % This takes the first 256 points of x0,
                                    % which gives a good portion of the wave.
x2 = wextend('ar','zpd',x1,128);    % This function zero-pads x1.
                                    % 'ar' stands for 'add rows'
                                    % 'zpd' stands for 'zero-padding'
                                    % x1 is the wave to be extended
                                    % 128 is the amount of zeros added
            
% 3. Find the DFT magnitudes and phases of 1 and 2.

% This can be accomplished by using the function fouriert, which is included
% with this project.  The function automatically makes the zero frequency 
% component the center of the plot.  The function requires a time input, so 
% that will be set up first.

t1 = 1:256;                         % This is the same length as x1
t2 = 1:512;                         % This is the same length as x2(padded x1)

[X1,f1] = fouriert(x1,t1);          % the fourier transform of x1 is taken
[X2,f2] = fouriert(x2,t2);          % the fourier transform of x2 is taken

figure(1);
subplot(2,1,1);
plot(f1,abs(X1));                   % The magnitude of X1 is plotted
title('The Fourier Transform of the First 256 Points of sample.wav');
ylabel('Magnitude');
subplot(2,1,2);
plot(f1,angle(X1));                 % The phase of X1 is plotted
ylabel('Phase');
xlabel('f1');

figure(2);
subplot(2,1,1);
plot(f2,abs(X2));                   % The magnitude of X2 is plotted
title('The Fourier Transform of the Zero-Padded sample.wav');
ylabel('Magnitude');
subplot(2,1,2);
plot(f2,angle(X2));                 % The phase of X2 is plotted
ylabel('Phase');
xlabel('f2');

% 4. Compare 3 of 1 and 2 and comment of spectral/density resolution.

% By looking at both graphs side by side, it is clearly visible that
% the magnitude graphs are very similar.  However, the graph of the 
% fourier transform of the wave that was zero-padded (X2) has a higher
% resolution than that of the fourier transform of simply the first 256
% points of sample.wav (X1).  There is more detail on X2, with more 
% sublte changes in magnitude.  Also, the phase graphs are different.
% X2 appears to have a more complex phase than X1.

% 5. Use at least three different filter commands in Matlab to implement
%    filters to filter the signal(sample.wav).

% The first filter I will use is the Bessel filter.  I will be using it
% in a bandpass configuration.

[b1,a1] = besself(1,[0.0001 0.5]);  % This creates a bessel filter of
figure(3);                          % the order of 1, with cut-off 
freqs(b1,a1);                       % frequencies at 0.0001 and 0.5
title('Bessel Filter (n=1,Wn=0.0001:0.5)');

yb1 = filter(b1,a1,x0);             % This filters the original wave
figure(4);                          % through the bessel filter.
subplot(2,1,1);
plot(yb1)
title('Comparison of Sample.wav Filtered Through a Bessel Filter and the Original Wave in the Time Domain');
ylabel('Filtered Wave');
axis([0 400 -0.4 0.4]);
subplot(2,1,2);
plot(x0)
ylabel('Original Wave');

t0 = 0:372;                         % The Fourier Transforms are
[X0,f0] = fouriert(x0,t0);          % taken to observe the changes
[YB1,fb1] = fouriert(yb1,t0);       % in the frequency domain.
figure(5);
subplot(2,1,1);
plot(fb1,abs(YB1))
title('Comparison of Sample.wav Filtered Through a Bessel Filter and the Original Wave in the Frequency Domain');
ylabel('Filtered Wave');
subplot(2,1,2);
plot(f0,abs(X0));
ylabel('Original Wave');

% The second filter I will use is a Butterworth filter.  It will also
% be configured for bandpass use.

[b2,a2] = butter(1,[0.01 0.5]);     % This creates a butterworth filter
figure(6);                          % of the order of 1, with cut-off 
freqs(b2,a2);                       % frequencies at 0.01 and 0.5
title('Butterworth Filter (n=1,Wn=0.01:0.5)');

yb2 = filter(b2,a2,x0);             % This filters the original wave
figure(7);                          % through the butterworth filter.
subplot(2,1,1);
plot(yb2)
title('Comparison of Sample.wav Filtered Through a Butterworth Filter and the Original Wave in the Time Domain');
ylabel('Filtered Wave');
subplot(2,1,2);
plot(x0)
ylabel('Original Wave');
          
[YB2,fb2] = fouriert(yb2,t0);       % The Fourier Transform is taken
figure(8);
subplot(2,1,1);
plot(fb2,abs(YB2))
title('Comparison of Sample.wav Filtered Through a Butterworth Filter and the Original Wave in the Frequency Domain');
ylabel('Filtered Wave');
subplot(2,1,2);
plot(f0,abs(X0));
ylabel('Original Wave');

% The last filter I will use is a Chebyshev Type 1 filter, which is a 
% passband ripple type.  Again, it will be configured as bandpass.

[b3,a3] = cheby1(1,3,[0.01 0.5]);   % This creates a chebyshev type 1
figure(9);                          % filter of the order of 1, with 
freqs(b3,a3);                       % 3dB p-p ripple in the passband
                                    % and cut-off frequencies at 0.01 
                                    % and 0.5
title('Chebyshev Type I Filter (n=1,Rp=3,Wn=0.01:0.5)');

ych = filter(b3,a3,x0);             % This filters the original wave
figure(10);                         % through the chebyshev filter.
subplot(2,1,1);
plot(ych)
title('Comparison of Sample.wav Filtered Through a Chebyshev Filter and the Original Wave in the Time Domain');
ylabel('Filtered Wave');
subplot(2,1,2);
plot(x0)
ylabel('Original Wave');
          
[YCH,fb3] = fouriert(ych,t0);       % The Fourier Transform is taken
figure(11);
subplot(2,1,1);
plot(fb3,abs(YCH))
title('Comparison of Sample.wav Filtered Through a Chebyshev Filter and the Original Wave in the Frequency Domain');
ylabel('Filtered Wave');
subplot(2,1,2);
plot(f0,abs(X0));
ylabel('Original Wave');

% The Bessel Filter seemed to be the worst out of the three for the   
% application.  The gradual cut-offs did not suit the large low
% frequency components of the wave.  Both the Butterworth and the
% Chebyshev gave fairly good results after filtering.  Both kept the 
% basic form of the wave, and allowed a higher low frequency cut-off.
% All the filters were on the order of 1 to preserve the amplitudes
% of the original wave as much as possible.

% 6. Discuss the working of mp3 encoders based on this project.

% It is easy to see that mp3 encoders are basically bandpass filters
% that filter out any unnecessary frequencies from the sound.  Our
% ears can only pick up sounds from about 20 to 20,000 Hz, and many
% speakers (especially computer speakers) do not even cover this 
% range.  Therefore, the encoders simply filter out all of these un-
% needed frequencies to effectively compress the size of the file
% to a more workable amount.  Wave files (.wav) take up large amounts
% of space because all the very high, and very low frequencies are
% included.  Mp3's compress this down to facilitate such things as
% transfer over the internet.  
