% edcAndPlot.m  
% calculate and plot the Energy Decay Curve of a signal
%
% Music 421, Spring Quarter 2003-2004

function result = edcAndPlot(s,mode)

fileName = 'SLucia_22050mono.wav'; % name of soundfile to be analyzed
% addpath ~/mus421/hw3/; % location of soundfile
minPlotDB = -60; % minimum value in DB to be plotted
epsilon = 1e-9;

[signal,fs,bits] = wavread(fileName);
if (mode == 1)
signal = s;
end
nSamp = length(signal);

% calculate the EDC
signalEDC_DB = zeros(1,nSamp);


%%% WRITE CODE HERE %%%
% The variable "signalEDC_DB" is the EDC in units of decibels.
% Implementation hint: It is faster to calculate the EDC going
% backward in time rather than forward.

signalEDC_DB = 20*log10(signalEDC_DB);


% normalize the EDC to 0 dB
maxSignalEDC_DB = max(signalEDC_DB);
signalEDC_DB_N = signalEDC_DB - maxSignalEDC_DB;

% plot the energy decay curve
timeAxis = (0:length(signal)-1)/fs; % time axis in seconds
figure(gcf);clf;
plot(timeAxis,signalEDC_DB_N);grid;
v = axis; axis([v(1) v(2) minPlotDB v(4)]); % to reduce vertical axis
title('Normalized Energy Decay Curve (EDC)');
xlabel('time (s)');ylabel('magnitude (dB)');
zoom on;

