x0=wavread('sample.wav')      % Reading the given sample.wav file
x1=x0'                        % Changing to column matrix    
x2=x1(1:256);                 % Selecting only 1st 256 points
x3=[x2,zeros(1,384)];         % Zero padding to increase resolution     
X1=fft(x2);                   % Fourier Transform of the function before zero padding
X2=fft(x3);                   % Fourier Transform of the function after zero padding
X1mag=abs(X1);                % Magnitude of the non zero padded F.T.
length(X1mag)

X1arg=phase(X1);              % Phase of the non zero padded F.T.
X2mag=abs(X2);                % Magnitude of the zero padded F.T.
X2arg=phase(X2);              % Phase of the zero padded F.T.
length(X2mag)
inc=pi/128;
w=-pi:inc:pi-inc;
l=w/pi;
length(l)

inc2=pi/320;
w2=-pi:inc2:pi-inc2;
l2=w2/pi;
length(l2)

figure(1);
subplot(4,1,1);
plot(l,fftshift(X1mag));
title('Magnitude without zero padding');
xlabel('0 to 128 of N');
ylabel('Function of X1mag');

subplot(4,1,2)
plot(l,fftshift(X1arg));
title('Phase without zero padding');
xlabel('0 to 128 of N');
ylabel('Function of X1arg');

subplot(4,1,3)
plot(l2,fftshift(X2mag));
title('Magnitude with zero padding');
xlabel('0 to 128 of N');
ylabel('Function of X2mag');

subplot(4,1,4)
plot(l2,fftshift(X2arg));
title('Phase with zero padding');
xlabel('0 to 128 of N');
ylabel('Function of X1arg');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Part 4
% By comparing part 3 with 1 and 2 is apparent that the density of the resoulution has 
% increased.I believe thisis due to zero padding. The pase of the plots has become more 
% stable and linear.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Part 5
figure(2);
subplot(3,1,1);
plot(x0);
title('Input Signal x0');
xlabel('t');
ylabel('Function of x0');

subplot(3,1,2);
Wn=[0.2 0.4];
D= FIR1(24,Wn);
plot(D);

subplot(3,1,3);
Y=filter(D,1,x0);
plot(t,Y);

figure(3);
subplot(3,1,1);
plot(x0);
title('Input Signal x0');
xlabel('t');
ylabel('Function of x0');

subplot(3,1,2);
Wn=[0.2 0.4];
[B,A] = BUTTER(45,Wn);
plot(A,B);

subplot(3,1,3);
Y=filter(D,1,x0);
plot(t,Y);

figure(4);
subplot(3,1,1);
plot(x0);
title('Input Signal x0');
xlabel('t');
ylabel('Function of x0');
Wn=[0.2 0.4];
[B,A] = CHEBY1(35,0.5,Wn);
plot(A,B);
subplot(3,1,3);
Y=filter(D,1,x0);
plot(t,Y);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Part 6
% In mp3 encoder the sound is sampled into 44,100 times per second. This is
% a form of compression or zero padding. This helps to reduce number of bites 
% in a song without hurting the quality of the music.

