% The following program should help you to do problem 3
% in the latest homework assignment.

clear all; clf; hold off;
load touchtone      % Dialpad D, signal y, sample rate Fs
   y = double(y)/128;  % Convert from signed 8 bit integers to doubles
   n = length(y);      % Total number of samples
   t = (1:n)/Fs;       % Time for entire signal
% plot(t,y,'k')
% axis([min(t) max(t) -1 1])

% The following several lines take about 25 seconds
% if your machine has 256Mb of RAM, but may take a very long time
% if you have less memory.
%
% Window-average of the square of the signal: sWindow
   w = 20;          % window halfwidth (must be >10)
   tot = repmat(w+1:n-w,2*w+1,1);
   win = repmat((-w:w)',1,n-2*w);
   ind = tot+win;
   sWindow = sum(y(ind).^2,1);
   maxs = max(sWindow);

tt = t(w+1:n-w); yy = y(w+1:n-w);
% plot original signal and the averaged signal
plot(tt,sWindow,tt,yy);   xlabel('t(secs)'); hold on;

g = find(sWindow < 0.2*maxs);   % gap indices
