clc;
clear;
close all;
warning off;
addpath(genpath(pwd));
input_audio = 'sample.wav';
[y,Fs] = audioread(input_audio);
y = y * 2.^15; % original values into integer
n_samples = length(y);
frame_length = Fs/40; % Assuming Fs is integer.
% Frame length of 400 ms (for Fs = 16 kHz)
trailing_samples = mod(n_samples, frame_length);
frames = reshape( y(1:end-trailing_samples), frame_length, []);
n_frames = length(frames(1,:));
% -------------------------Zero-Crossing Rate-----------------------------
[ zcr ] = ZCR( y, frames, n_frames, Fs );
% -----------------------Hamming Windowing--------------------------------
h = hamming(frame_length);
h = repmat(h,1,n_frames);
w = frames.*h;
w_reshaped = reshape(w, 1, frame_length*n_frames);
% signal
figure;
subplot(2,1,1); plot(y); ylabel('Amplitude'); title('Audio input');
subplot(2,1,2); plot(w_reshaped, '.-');
xlabel('time'); ylabel('Amplitude'); title('Windowed signal');
energy = sum(w.^2);
figure;
subplot(2,1,1); plot(y); ylabel('Amplitude'); title('Audio input');
subplot(2,1,2); plot(energy, '.-');
xlabel('time'); ylabel('Energy (J)'); title('Signal energy');