

%  ======================================================================================

%  										MATLAB    PROJECT  2

%  ======================================================================================


%  This project analyzes the given wave file and application of filters


%  -------------------------------------------

%  ( 1 ) Read the given wave file

%  -------------------------------------------

figure(1);
subplot(2,1,1);						% Set first plot
x=wavread('sample.wav');			% Open the given wave file
i1=2*pi/128;
t1=-pi:i1:pi;							% Set x-axis range
X=x(1:128);								% Consider over 128 points
t=t1(1:128);							% Take 128 points
plot(X);									% Plot the given wave function
title('Plot of the given Wave Signal');	% Define title
xlabel('time');							% Define x-axis
ylabel('amplitude');					% Define y-axis


%  -------------------------------------------

%  ( 2 ) Zero Pad the given wave file

%  -------------------------------------------

subplot(2,1,2);							% Set second plot
i2=2*pi/512;
t2=-pi:i2:pi;								% Set x-axis range
tpad=t2(1:512);							% Take 512 points
X1=[zeros(1,384),X'];					% Zero pad the given wave file
plot(X1);									% Plot the padded wave function 		
title('Plot of the Padded Wave Signal');		% Define title
xlabel('time');							% Define x-axis
ylabel('amplitude');						% Define y-axis


%  -------------------------------------------

%  ( 3 ) Find DFT magnitude and phase of 1 and 2

%  -------------------------------------------

figure(2);					
subplot(2,1,1);					% Set the first plot
Y=fftshift(fft(X));				% Find the Discrete Fourier Transform of original wave file
mag1=20*log10(abs(Y));			% Find the maginitude 
plot(t,mag1);						% Plot the DFT magnitude of original wave file
title('Plot of DFT Magnitude of Original Wave File');		% Set title
xlabel('Normalized Frequency over [-pi,pi]');				% Label x-axis
ylabel('Magnitude in dB');	% LAbel y-axis

subplot(2,1,2);				% Set the second plot
p1=unwrap(angle(Y));			% Find the phase
plot(t,p1);						% Plot the DFT phase of original wave file
title('Plot of DFT Phase of Original Wave File');			% Set title
xlabel('Normalized Frequency over [-pi,pi]');				% Label x-axis
ylabel('Phase in Degree');	% Label y-axis

figure(3);
subplot(2,1,1);				% Set the first plot	
Z=fftshift(fft(X1));			% Find the Discrete Fourier Transform of padded wave file
mag2=20*log10(abs(Z));			% Find the magnitude
plot(tpad,mag2);				% Plot the DFT magnitude of padded wave file
title('Plot of DFT Magnitude of Padded Wave File');		% Set title
xlabel('Normalized Frequency over [-pi,pi]');				% Label x-axis
ylabel('Magnitude in dB');	% LAbel y-axis

subplot(2,1,2);				% Set the second plot
p2=unwrap(angle(Z));			% Find the phase 
plot(tpad,p2);					% Plot the DFT phase of padded wave file
title('Plot of DFT Phase of Padded Wave File');			% Set title
xlabel('Normalized Frequency over [-pi,pi]');			% Label x-axis
ylabel('Phase in Degree');	% Label y-axis



%  ---------------------------------------------------

%  ( 4 ) Comments on Spectral / Density Resolution

%  ---------------------------------------------------

%  With padded signal, the density resolution seems more compact over the same range as the

%  original signal. With more data values, the spectral becomes more defined and compact. The

%  phase of padded signal becomes more linear. 


%  ---------------------------------------------------

%  ( 5 ) Filter Implementation

%  ---------------------------------------------------

figure(4);									% I select first filter using butterworth
subplot(3,1,1);							% Subplot for wav file
plot(t,abs(X));							% Plot the input wav file
title('Plot of Wav File');				% Define title
xlabel('time');							% Label x-axis
ylabel('amplitude');						% Label y-axis							

subplot(3,1,2);							% Subplot for filter function
[B1,A1]=butter(5,0.5);					% Define filter 1
plot(B1,A1);								% Plot filter
title('Plot of Butterworth filter');	% Define title
xlabel('frequency');						% Set x-axis							
ylabel('amplitude');						% Set y-axis

subplot(3,1,3);							% Subplot for output response
y = filter(B1,A1,X);						% Find the filtered output 
plot(t,abs(y));							% Plot output response
title('Plot of output response');	% Define title
xlabel('time');							% Set x-axis
ylabel('amplitude');						% Set y-axis


figure(5);									% I select second filter using Chebychev Type I
subplot(3,1,1);							% Subplot for wav file
plot(t,abs(X));							% Plot the input wav file
title('Plot of Wav File');				% Define title
xlabel('time');							% Label x-axis
ylabel('amplitude');						% Label y-axis							

subplot(3,1,2);							% Subplot for filter function
W=[0.25 0.5];								% Set stopband and passband frequencies
[B2,A2]=cheby1(5,2,W,'stop');			% Define filter 2
plot(B2,A2);								% Plot filter
title('Plot of Chebychey Type 1 filter');			% Define title
xlabel('frequency');						% Set x-axis							
ylabel('amplitude');						% Set y-axis

subplot(3,1,3);								% Subplot for output response
y2 = filter(B2,A2,X);						% Find filtered output
plot(t,abs(y2));								% Plot output response
title('Plot of output response');		% Define title
xlabel('time');								% Set x-axis
ylabel('amplitude');							% Set y-axis


figure(6);									% I select the third filter using Chebychev type 2
subplot(3,1,1);							% Subplot for wav file
plot(t,abs(X));							% Plot the input wav file
title('Plot of Wav File');				% Define title
xlabel('time');							% Label x-axis
ylabel('amplitude');						% Label y-axis							

subplot(3,1,2);							% Subplot for filter function
W=[0.25 0.5];								% Set stopband and passband frequencies
[B3,A3]=cheby2(5,2,W,'stop');			% Define filter 3
plot(B3,A3);								% Plot filter
title('Plot of Chebychev Type 2 filter');			% Define title
xlabel('frequency');						% Set x-axis							
ylabel('amplitude');						% Set y-axis

subplot(3,1,3);								% Subplot for output response
y3 = filter(B3,A3,X);						% Find filtered output
plot(t,abs(y3));								% Plot output response
title('Plot of output response');		% Define title
xlabel('time');								% Set x-axis
ylabel('amplitude');							% Set y-axis


%  ---------------------------------------------------

% ( 6 ) Discussion on MP3 Encoders

%  ---------------------------------------------------


%  MP3 encoders are programs that convert wav file into MP3 format file. MP3 format 

%  is a compression system for music, which reduces the size of a file without 

%  changing the quality of the system. For example, an MP3 player pulls a file

%  from memory, decompress the MP3 decoding, run the decompressed bytes and finally

%  amplify the analog signal to allow user to hear the file. Thus, MP3 encoders 

%  are run based on the application of filters to decompress or compress the file.


% ----------------------------------------------

% NAME    : FOONG CHAN TOONG

% UB NO.  : 2764 - 1508

% CLASS   : EE 416

% PROJECT : MATLAB PROJECT 1

% ----------------------------------------------




