%Vedrana Sarajlic
%ID# 2730-3757
%File: project2

%Read the sample.wav file
x=wavread('sample.wav');
%reduce the length to power of 2
y=x(1:256);

%plot the magnitude and phase
f=fft(y);
fftshift(f);

figure(1);

subplot(211);
plot(abs(f))
ylabel('Magnitude before zero-padding') 
xlabel('Plot using plot function')
grid

subplot(212);
plot(angle(f))
ylabel('Phase before zero-padding')
xlabel('Plot using plot function')
grid

%the same plot using stem function
figure(2);
subplot(211);
stem(abs(f))
ylabel('Magnitude before zero-padding')
xlabel('Plot using stem function')
grid

subplot(212);
stem(angle(f))
ylabel('Phase before zero-padding')
xlabel('Plot using stem function')
grid

%plots after zero padding
fp=fft(y,400);
fftshift(fp);

figure(3);
subplot(211);
plot(abs(fp))
ylabel('Magnitude after zero-padding')
xlabel('Plot using plot function')
grid

subplot(212);
plot(angle(fp))
ylabel('Phase after zero-padding')
xlabel('Plot using plot function')
grid

figure(4);
subplot(211);
stem(abs(fp))
ylabel('Magnitude after zero-padding')
xlabel('Plot using stem function')
grid


subplot(212);
stem(angle(fp))
ylabel('Phase after zero-padding')
xlabel('Plot using stem function')
grid

%The zero-padded wave file has a higher resolution than the original one.





%filtering the signal sample.wav
%using fir1

h=fir1(32,0.5);
ha=filter(h,1,y);
Ha=fftshift(fft(ha,500));
figure(5);
subplot(211);
stem(y)
xlabel('Original signal sample.wav')
subplot(212);
stem(ha)
xlabel('Signal after filtering using fir1 function')

figure(6);
subplot(211);
stem(abs(Ha))
xlabel('Magnitude plot of filtered signal (fir1)')
subplot(212);
stem(angle(Ha))
xlabel('Phase plot of filtered signal (fir1)')


%using fir2

%coefficient matrices F and M
F=[0.0 0.2 0.32 1.0];
M=[0.0 0.2 0.5 1.0];
B=fir2(32,F,M);
hb=filter(B,1,y);
Hb=fftshift(fft(hb,500));

figure(7);
subplot(211);
stem(y)
xlabel('Original signal sample.wav')
subplot(212);
stem(hb)
xlabel('Signal after filtering using fir2 function')

figure(8);
subplot(211);
stem(abs(Hb))
xlabel('Magnitude plot of filtered signal (fir2)')

subplot(212);
stem(angle(Hb))
xlabel('Phase plot of filtered signal (fir2)')


%using Butterworth filter
W=[0.32 0.5];
[C,A]=butter(24,W);
hc=filter(C,1,y);
Hc=fftshift(fft(hc,500));

figure(9);
subplot(211);
stem(y)
xlabel('Original signal sample.wav')
subplot(212);
stem(hc)
xlabel('Signal after filtering using butter function')

figure(10);
subplot(211);
stem(abs(Hc))
xlabel('Magnitude plot of filtered signal (butter)')

subplot(212);
stem(angle(Hc))
xlabel('Phase plot of filtered signal (butter)')

%The .wav file gets its relatively high and relatively low frequencies filtered out.
%Its size after filtering is smaller than the original one.
%mp3 encoders work similarly to this:they filter out all the unnecessary high and low frequencies
%of the .wav file and convert it to .mp3 file which is smaller, and therefore more convenient
%for storage.


%reconstruction of .wav file after being passed through a filter
wavwrite(hc,'newwave.wav');
