% Plotting the time-domain Waveforms

function plotPhoneme(word, phoneme, phonemeFile)

close all
mkdir('images', phonemeFile);
folderPath = [pwd '/images/' phonemeFile '/'];

audioPath = [pwd '/audio/'];

%% Time Domain Plots

% Plot the word time domain signal
wordFile = [word ' 1.wav'];
[wordAudioSignal, fs] = audioread(wordFile);
wordAudioSignal = wordAudioSignal';
wordAudioSignal = 0.95 .* wordAudioSignal;

%save the denormalized file
audiowrite([audioPath word '.wav'], wordAudioSignal, fs);

%re-do the normalization
wordAudioSignal = -1 + 2.*(wordAudioSignal - min(wordAudioSignal))./(max(wordAudioSignal) - min(wordAudioSignal));

t = 0:1/fs:(length(wordAudioSignal)-1)/fs;
t = 1000.*t;

figure(1);
plot(t, wordAudioSignal);
xlabel('Time (ms)');
ylabel('Amplitude');
title(['Time Domain plot of "' word '"']);
xlim([0 (length(t)-1)/fs*1000]);
ylim([-1 1]);
grid on;

fileName = [folderPath phoneme '-' word '.png' ];
saveas(gcf, fileName);

% Plot the phoneme time domain signal
phonemeFile2 = [phonemeFile ' 1.wav'];
[phonemeAudioSignal, fs] = audioread(phonemeFile2);
phonemeAudioSignal = phonemeAudioSignal';
phonemeAudioSignal = 0.95 .* phonemeAudioSignal;

%save the denormalized file
audiowrite([audioPath phonemeFile '.wav'], phonemeAudioSignal, fs);

%re-do the normalization
phonemeAudioSignal = -1 + 2.*(phonemeAudioSignal - min(phonemeAudioSignal))./(max(phonemeAudioSignal) - min(phonemeAudioSignal));

t = 0:1/fs:(length(phonemeAudioSignal)-1)/fs;
t = 1000.*t;

figure(2)
plot(t, phonemeAudioSignal);
xlabel('Time (ms)');
ylabel('Amplitude');
title(['Time Domain plot of /' phoneme '/']);
xlim([0 (length(t)-1)/fs*1000]);
ylim([-1 1]);
grid on;

fileName = [folderPath phoneme '.png' ];
saveas(gcf, fileName);

%% Spectrograms

T = 0.025; % Window size of 25ms
N = 2^(round(log2(T*fs))); % Window size

figure(3)

% Narrowband Spectrogram
subplot(2,1,1)
spectrogram(wordAudioSignal, hamming(N), round(0.97 * N), [], fs, 'yaxis');
axis tight;
title(['Narrowband Spectrogram of "' word '"']);

% Wideband Spectrogram
subplot(2,1,2)
spectrogram(wordAudioSignal, hamming(N/4), round(0.9 * N/4), [], fs, 'yaxis');
axis tight;
colormap jet;
shading interp;
title(['Wideband Spectrogram of "' word '"']);

fileName = [folderPath phoneme '_spectrograms.png'];
saveas(gcf, fileName);

%% 3D Spectrogram Plot (Waterfall Plot)

T = 0.02; % Window size of 25ms
N = 2^(round(log2(T*fs))); % Window size

[sg, ~, ~, ~] = spectrogram(wordAudioSignal, hamming(N/4), round(0.75*N/4), [], fs, 'yaxis');
sg = 20 .* log10(abs(sg));

figure(4)
surf(sg, 'EdgeColor', 'none');
view(60, 45);
axis tight;
ylabel('Frequency (Hz)');
xlabel('Time (ms)');
zlabel('Magnitude (dB)');
title(['3D-Spectrogram of "' word '"']);
colormap jet;
shading interp;

ylim auto;
set(gca, 'YTickLabel', {1300, 2600, 4100, 5400, 6800, 8000});

fileName = [folderPath phoneme '_waterfall.png'];
saveas(gcf, fileName);

%% Magnitude Spectrum

n = 512;

FFT = fft(phonemeAudioSignal, n)';
FFT = abs(FFT(1:n/2));
FFT = 20 * log10(FFT/512);

freq = 0:fs/n:fs/2-fs/n;

figure(5)
plot(freq, FFT);
hold on;

p = fs/1000 + 4;
[a, g] = lpc(phonemeAudioSignal, p);
lspec = freqz(g, a, freq, fs);
lspec = 20 * log10(abs(lspec));
plot(freq, lspec, 'k');
title(['Spectral Analysis of /' phoneme '/']);
axis tight;

fileName = [folderPath phoneme '_spectrum.png'];
saveas(gcf, fileName);

end