% ECE540 -- Homework #1
% Evan Shenkman

%% Problem 2
clear all;

% Creating the Time Domain Sequence
N = 32;                                                 % Lenght of Window
n = -50:1:49;                                           % Discrete Time Vector

wrect = cat(2,zeros(1, 50),rectwin(N)',zeros(1,50-N));  % Discrete Time Signal

F = 2048;                                               % FFT Points
Wrect = fft(wrect, F);                                  % Take the FFT

WrectMagDB = 20.*log10(abs(Wrect));                     % Log-Mag Spectrum
WrectPhase = angle(Wrect).*180/pi;                      % Phase Spectrum in degrees 

w = linspace(0,pi,F/2);                                 % Frequency Vector

figure(1)
subplot(3,1,1)
stem(n, wrect);
axis([n(1) n(end) -2 2]);
xlabel('Time_{samples}');
ylabel('Amplitude');
title('Rectangular Window');

subplot(3,1,2)
plot(w, WrectMagDB(1:length(WrectMagDB)/2));
axis([w(1) w(end) -40 40])
xlabel('Frequency in Radians');
ylabel('Magnitude_{dB}');
title('Mag Response -- |H(e^{jw})|');

subplot(3,1,3)
plot(w, WrectPhase(1:length(WrectPhase)/2));
axis([w(1) w(end) -180 180])
xlabel('Frequency in Radians');
ylabel('Phase Shift in Degrees');
title('Phase Response -- arg(H(e^{jw}))');



%% Problem 3
clear all;

% Creating the Time Domain Sequence
N = 32;                                                 % Lenght of Window
n = -50:1:49;                                           % Discrete Time Vector

wrect = cat(2,zeros(1, 50),hamming(N)',zeros(1,50-N));  % Discrete Time Signal

F = 2048;                                               % FFT Points
Wrect = fft(wrect, F);                                  % Take the FFT

WrectMagDB = 20.*log10(abs(Wrect));                     % Log-Mag Spectrum
WrectPhase = angle(Wrect).*180/pi;                      % Phase Spectrum

w = linspace(0,pi,F/2);                                 % Frequency Vector

figure(2)
subplot(3,1,1)
stem(n, wrect);
axis([n(1) n(end) -2 2]);
xlabel('Time_{samples}');
ylabel('Amplitude');
title('Hamming Window');

subplot(3,1,2)
plot(w, WrectMagDB(1:length(WrectMagDB)/2));
axis([w(1) w(end) -40 40])
xlabel('Frequency in Radians');
ylabel('Magnitude_{dB}');
title('Mag Response -- |H(e^{jw})|');

subplot(3,1,3)
plot(w, WrectPhase(1:length(WrectPhase)/2));
axis([w(1) w(end) -180 180])
xlabel('Frequency in Radians');
ylabel('Phase Shift in Degrees');
title('Phase Response -- arg(H(e^{jw}))');

%% Problem 4
clear all;

AppendZeros = 'Beginning';                                         % Change between No, End, Beginning

S = load('ex2M1.mat');                                      % Load the speech signal
speech = S.speech1_10k;
fs = 10000;                                                 % Sample Rate

if strcmp(AppendZeros, 'End')                               % Edit the speech signal
    speech = cat(2, speech, zeros(1, length(speech)));      % Append zeros to the end
elseif strcmp(AppendZeros, 'Beginning')
    speech = cat(2, zeros(1, length(speech)), speech);      % Append zeros to the beginning
end

N = length(speech);                                         % Length of the speech signal
soundsc(speech, fs);                                        % Play the sound

n = 0:1:N-1;                                                % Discrete Time vector

rectWin = rectwin(N)';                                      % Rectangular Window sequence
hammWin = hamming(N)';                                      % Hamming Window sequence

fig = figure(1)                                                   % Plot the time domain waveforms
set(fig, 'Position', [100 100 900 700]);

subplot(3,1,1)
stem(n, speech);
axis([n(1) n(end) -1.5 1.5]);
title('Speech');

subplot(3,1,2)
stem(n, rectWin);
axis([n(1) n(end) -1.5 1.5]);
title('Rectangular Window');

subplot(3,1,3)
stem(n, hammWin);
axis([n(1) n(end) -1.5 1.5]);
title('Hamming Window');


speechRect = speech.*rectWin;                               % Window using Rect Window
speechHamm = speech.*hammWin;                               % Window using Hamm Window

fig = figure(2)                                                   % Plot the windowed waveforms
set(fig, 'Position', [100 100 900 700]);

subplot(2,1,1)
stem(n, speechRect);
axis([n(1) n(end) -1.5 1.5]);
title('Speech analyzed with Rectangular Window');

subplot(2,1,2)
stem(n, speechHamm);
axis([n(1) n(end) -1.5 1.5]);
title('Speech analyzed with Hamming Window');

F = 1024;                                                   % FFT length
w = linspace(0,pi,F/2);                                     % Frequency vector (bassband)

                                                            % RECTANGULAR WINDOW
speechRectFFT = fft(speechRect, F);                         % Take the FFT
speechRectMag = abs(speechRectFFT);                         % Linear Mag Spectrum
speechRectMagDB = 20.*log10(speechRectMag);                 % Log Mag Spectrum
speechRectPhase = angle(speechRectFFT).*180/pi;             % Phase Spectrum

fig = figure(3);                                            % Plot the waveform and spectrum
set(fig, 'Position', [100 100 900 700]);                    % for the rectangular windowed
                                                            % speech signal
subplot(4,1,1)
stem(n, speechRect);
axis([n(1) n(end) -1.5 1.5]);
title('Speech analyzed with Rectangular Window');

subplot(4,1,2)
plot(w, speechRectMag(1:F/2))
axis([w(1) w(end) -32 64]);
title('Linear Mag Spectrum');

subplot(4,1,3)
plot(w, speechRectMagDB(1:F/2))
axis([w(1) w(end) -32 64]);
title('Log Mag Spectrum');

subplot(4,1,4)
plot(w, speechRectPhase(1:F/2))
axis([w(1) w(end) -180 180]);
title('Linear Mag Spectrum')

                                                            % HAMMING WINDOW
speechHammFFT = fft(speechHamm, F);                         % Take the FFT
speechHammMag = abs(speechHammFFT);                         % Linear Mag Spectrum
speechHammMagDB = 20.*log10(speechHammMag);                 % Log Mag Spectrum
speechHammPhase = angle(speechHammFFT).*180/pi;             % Phase Spectrum

fig = figure(4);                                            % Plot the waveform and
set(fig, 'Position', [100 100 900 700]);                    % spectrums of the hamming
                                                            % windowed speech signal 
subplot(4,1,1)
stem(n, speechHamm);
axis([n(1) n(end) -1.5 1.5]);
title('Speech analyzed with Hamming Window');

subplot(4,1,2)
plot(w, speechHammMag(1:F/2))
axis([w(1) w(end) -32 64]);
title('Linear Mag Spectrum');

subplot(4,1,3)
plot(w, speechHammMagDB(1:F/2))
axis([w(1) w(end) -32 64]);
title('Log Mag Spectrum');

subplot(4,1,4)
plot(w, speechHammPhase(1:F/2))
axis([w(1) w(end) -180 180]);
title('Linear Mag Spectrum')




