function y = chvocod_syn(x,pitch,R)
%CHVOCOD_SYN   Synthesizes speech waveform from pitch and band envelope signals
%
%		y = CHVOCOD_SYN(x,p,R);
%		x 	matrix of band envelope signals
%			each column contains output of one band
%			each row contains output of one frame
%		p 	pitch signal containing one sample for each frame
%		R  	upsampling rate

% Initialize variables
Fs = 8000;		% Sampling frequency
N = size(x,2);		% Determine number of channels as the
                        % number of columns in the input matrix 'x'
frlen = R;		% length of each frame in samples

% Compute FIR coefficients for filter bank . . .
L = 65;				% length of each filter
bank = myownfilt_bank(N,L);

% Generate a voiced source signal using pulse_train . . .
src = sw_source(pitch,Fs,frlen);

% Compute length of source signal . . .
M = length(src);

% Preallocate output matrix for efficiency
ybands = zeros(M,N);

% In loop, process each band:
for i = 1:N
	% Interpolate the decimated signal . . .
	xint = interp(x(:,i),R);
	% . . . and replace any negative values with zeros 
        % because x contains band-envelope values, which must be positive
	negs = find(xint<0);
	xint(negs) = zeros(size(negs));

	% Multiply with source, trimming the interpolated signal to
	% match pulse train length, M.
	yint = xint(1:M) .* src;

	% Apply bandpass filter . . .
	ybands(:,i) = fftfilt(bank(:,i),yint);
end

% Add up the output of all of the bands to generate result . . .
y = sum(ybands');


