%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Ken Mayle                                         %
% Chris Musser                                      %
% Vladi Gergov                                      %
% ECE 480 - Final Project                           %
% AVERAGE ENERGY FUNCTION                           %
% Modular Code, Takes a wave file, allows you to    %
% extract a section, save and calc ave energy       %
% Dis be the shiiiat                                %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



[Y,FS,BITS]=wavread('away'); %extracts info on wav file

plot(Y);							% Plot Y verse Samples
title('Orginial Wave Data');
xlabel('Samples n'),ylabel('Amplitude');

[xcoord, ycoord] = ginput(2);       %this section of code extracts desired sound section
xcoord = round(xcoord);
xcoord = sort(xcoord);
IY=Y(xcoord(1):xcoord(2));
figure(7);
plot(IY);
title('IY Data');
xlabel('Samples n'),ylabel('Amplitude');
pause(3);
sound(IY, FS, BITS);
nIYsamples = length(IY)             %

wavwrite(IY, FS, BITS, 'y');      %writes code to 'she.wav' (assuming you want she part)
pause(3);                           %gives a little pausy pause

infile='we';

spch=wavread( infile ); %extracts info on wav file, only need the array output

windowlength=220;                   %window length awad gave us
movewin=windowlength/2;             %gives our window movement (50%)
fs=11025;                           %sampling frequency

avg_energy(spch,windowlength);      %calls avg_energy function (must be named avg_energy - work dir)

energy=ans;                         %this is the output from the avg_energy function

avg_magnitude(spch,windowlength);      %calls avg_energy function (must be named avg_energy - work dir)

magnitude=ans;

zero_crossings(spch,windowlength);      %calls avg_energy function (must be named avg_energy - work dir)

zero_c=ans;

endspeech=length(spch);             %length of the vector spch

tspch = [1:endspeech]/fs;           %code awad gave us in slides for time vector
tspch = tspch*1000;

tenergy = [1:length(energy)];

tenergy = (windowlength+tenergy*movewin);   

tenergy=tenergy*1000;                   %
subplot(4,1,1);                         %   plot the crap
plot(tspch,spch);
title('"She" Speech');                  %label the crap you plot
subplot(4,1,2);
plot(tenergy,energy);
title('Short-Time Energy');    %label the crap you plot
xlabel('Time in milliseconds')          %label the crap you plot
subplot(4,1,3);
plot(tenergy,magnitude);
title('Short-Time Magnitude');    %label the crap you plot
xlabel('Time in milliseconds')          %label the crap you plot
subplot(4,1,4);
plot(tenergy,zero_c);
title('Zero Crossings');    %label the crap you plot
xlabel('Time in milliseconds')          %label the crap you plot
