function [y,p]=chvocod_ana(x, D, N)
%CHVOCOD_ANA	Analyzes speech waveform into pitch and band envelope signals
%		[y,p] = CHVOCOD_ANA(x, D, N)
%
%		x	vector containing speech signal to encode
%		D	decimation rate
%		N	number of bands
%
%		y	output matrix of band envelope signals
%			each column contains output of one band
%			each row contains output of one frame
%		p	output vector containing pitch signal with one
%			value for each frame
% This code template has two separate stages, corresponding to the 
% source-filter model of speech production:  
% The first stage involves characterizing the "source" by pitch detection.
% Pitch detection is accomplished by breaking up the original signal 
% into frames and then determining if each frame is is voiced or unvoiced.
% If the frame is voiced, then we also estimate the fundamental frequency 
% of the glottal source.  
% The second stage involves characterizing the "filter", that is 
% determining the band envelope values.  This is accomplished by filtering 
% the original signal into frequency bands, determining the envelope of 
% each band and decimating. 

% Make x a column vector just to be sure.
x = x(:);

Fs = 8000;			% sampling frequency
frlen = floor(0.030 * Fs);      % use 30ms frame length
nframes = ceil(length(x)/D);    % total number of frames
% Note that nframes depends on the decimation rate, D.
% Try to understand why . . .

% preallocate outputs for speed
p = zeros(nframes, 1);
y = zeros(nframes,N);

%---------------------------------------------------------------------------%

%-------------------- get "source parameters" (pitch detection)-------------%

%---------------------------------------------------------------------------%

% First, lowpass filter the signal with 500 Hz cutoff frequency, 
% since we're interested in pitch, which is 80-320 Hz for adult voices.
% Be careful: the lowpass filtered signal should be used as the input
% to the pitch detector, but not to the filter bank! 

xlpf = 


% Here's the loop. Each iteration processes one frame of data.

for i = 1:nframes

   %  Get the next segment.  The indexing has been done for you.
   startseg = (i-1)*D+1;
   endseg = startseg+frlen-1;
   if endseg > length(xlpf)
     endseg = length(xlpf);
   end
   seg = xlpf(startseg:endseg);
   
   % Call your pitch detector . . .
   p(i) = 

end

% Remove spurious values from pitch signal with median filter . . .

%---------------------------------------------------------------------------%

%--------- get "filter" parameters (determine band envelope values)---------%

%---------------------------------------------------------------------------%

% Compute FIR coefficients for filter bank (use 65-point filters) . . .
% The variable bank should be a 65xN matrix with each column containing
% the impulse response of one filter

bank = myownfilt_bank(N,65);

% Apply the filterbank to the input signal, x . . .
% Please be sure that you understand why you shouldn't apply 
% the filterbank to the lowpass filtered signal, xlpf

% In loop, process each band:
for i = 1:N
%	Apply filter for this band (bank(:,i)) to input x (not xlpf) . . .

% 	Take magnitude of signal and decimate.
% 	(The matlab function decimate includes lowpass filtering) . . .

end
