%% Project 3 - Part 1 - Evan Shenkman

clc
close all
clear all

[nine1, fs] = audioread('audio/91.wav');
nine2 = audioread('audio/94.wav');

nine1 = nine1';
nine2 = nine2';

nine1 = nine1/norm(nine1, Inf);
nine2 = nine2/norm(nine2, Inf);

newfs=8000;

newnine1 = resample(nine1,newfs,fs);
t1=0:1/newfs:(length(newnine1)-1)/newfs;

newnine2 = resample(nine2,newfs,fs);
t2=0:1/newfs:(length(newnine2)-1)/newfs;

fig = figure(1);
set(fig, 'Position', [100 100 1000 800]);

subplot(4,2,1)
plot(t1,newnine1) 
title('Time Waveform of nine1.wav'); 
ylabel('x(t)'); 
xlabel('Time'); 
grid on;
axis tight

subplot(4,2,2)
plot(t2,newnine2) 
title('Time Waveform of nine4.wav'); 
ylabel('x(t)'); 
xlabel('Time'); 
grid on;
axis tight

n = 256;    %window size

subplot(4,2,3)
spectrogram(newnine1,hamming(n/4),round(.9*n/4),[],newfs,'yaxis'); 
axis tight; 
title('Wideband Spectrogram of nine1.wav');

subplot(4,2,4)
spectrogram(newnine2,hamming(n/4),round(.9*n/4),[],newfs,'yaxis'); 
axis tight; 
title('Wideband Spectrogram of nine4.wav');

%Zero Crossings
a = newnine1(1:length(newnine1)-1); % All but the last sample...
b = newnine1(2:length(newnine1));   % All but the first sample...
tt = a.*b;                          % Multiply the vectors of offset samples by eachother
index = find(tt<0);                 % Any index whose value is negative represents a Zero Crossing! (flipping of sign)

aZeroCrossOverTime = zeros(1, length(newnine1));
j = 1;
zeroCount = 0;
for i = 1:length(aZeroCrossOverTime)
    if (j < length(index))
        if (i == index(j))
            zeroCount = zeroCount + 1;
            j = j + 1;
        end
    end
    
    aZeroCrossOverTime(i) = zeroCount;
end


a2 = newnine2(1:length(newnine2)-1);
b2 = newnine2(2:length(newnine2));
tt2 = a2.*b2;
index2 = find(tt2<0);

aZeroCrossOverTime2 = zeros(1, length(newnine2));
j = 1;
zeroCount = 0;
for i = 1:length(aZeroCrossOverTime2)
    if (j < length(index2))
        if (i == index2(j))
            zeroCount = zeroCount + 1;
            j = j + 1;
        end
    end
    
    aZeroCrossOverTime2(i) = zeroCount;
end

subplot(4,2,5)
plot(t1, aZeroCrossOverTime)
axis tight
grid on
ylabel('# of Zero Crossings')
xlabel('Time')
title('Zero Crossing Rate of nine1.wav')

subplot(4,2,6)
plot(t2, aZeroCrossOverTime2)
axis tight
grid on
ylabel('# of Zero Crossings')
xlabel('Time')
title('Zero Crossing Rate of nine4.wav')



% Plotting the spectral magnitude and computed critical band energies
startIndex = newfs * 0.2;
frameNine1 = newnine1(startIndex:startIndex+newfs*0.035);
frameNine2 = newnine2(startIndex:startIndex+newfs*0.035);

frameNine1 = frameNine1';
frameNine2 = frameNine2';

[energies1, frameCount1] = ExtractFeatures(frameNine1, newfs);
[energies2, frameCount2] = ExtractFeatures(frameNine2, newfs);

magSpecNine1 = mag2db(abs(fft(frameNine1, newfs)));
magSpecNine1 = magSpecNine1(1:newfs/2);
magSpecNine2 = mag2db(abs(fft(frameNine2, newfs)));
magSpecNine2 = magSpecNine2(1:newfs/2);

w = 1:newfs/2;

energies1a = ones(1, length(w));
energies1a(1:100) = energies1(1);
energies1a(101:200) = energies1(2);
energies1a(201:300) = energies1(3);
energies1a(301:400) = energies1(4);
energies1a(401:510) = energies1(5);
energies1a(511:630) = energies1(6);
energies1a(631:770) = energies1(7);
energies1a(771:920) = energies1(8);
energies1a(921:1080) = energies1(9);
energies1a(1081:1270) = energies1(10);
energies1a(1271:1480) = energies1(10);
energies1a(1481:1720) = energies1(12);
energies1a(1721:2000) = energies1(13);
energies1a(2001:2320) = energies1(14);
energies1a(2321:2700) = energies1(15);
energies1a(2701:3150) = energies1(16);
energies1a(3151:end) = energies1(17);

energies2a = ones(1, length(w));
energies2a(1:100) = energies2(1);
energies2a(101:200) = energies2(2);
energies2a(201:300) = energies2(3);
energies2a(301:400) = energies2(4);
energies2a(401:510) = energies2(5);
energies2a(511:630) = energies2(6);
energies2a(631:770) = energies2(7);
energies2a(771:920) = energies2(8);
energies2a(921:1080) = energies2(9);
energies2a(1081:1270) = energies2(10);
energies2a(1271:1480) = energies2(10);
energies2a(1481:1720) = energies2(12);
energies2a(1721:2000) = energies2(13);
energies2a(2001:2320) = energies2(14);
energies2a(2321:2700) = energies2(15);
energies2a(2701:3150) = energies2(16);
energies2a(3151:end) = energies2(17);

subplot(4,2,7)
plot(w, magSpecNine1)
hold on
stairs(w,energies1a);
axis tight
grid on
xlabel('Frequency')
ylabel('Magnitude')
title('Magnitude Spectra & Critical Band Energies for nine1.wav')

subplot(4,2,8)
plot(w, magSpecNine2)
axis tight
hold on
stairs(w,energies2a);
axis tight
grid on
xlabel('Frequency')
ylabel('Magnitude')
title('Magnitude Spectra & Critical Band Energies for nine1.wav')