load 'sp4';
speech = sp4;
speech = speech(2321:33520);
fs = 8000;
segdur = 0.02;
seglen = segdur*fs;
overlap = seglen/2;
stepsize = seglen - overlap;
nframes = length(speech)/stepsize-1;
w = window(@hamming,160);
p = 12;
alpha = zeros(p+1, nframes);
segstart = 1; segend = seglen;
synspeech = zeros(length(speech),1);
wgnsq = randn(seglen+p,1)/sqrt(seglen+p);
for i = 1:nframes
frame = speech(segstart:segend);
% alpha(:,i) = lpc(frame,p);
wframe = frame .* w;
alpha(:,i) = lpc(wframe,p);
estframe = conv([0; -alpha(2:end,101)], frame);
d = [frame; zeros(p,1)]-estframe;
% wgnexcitation = randn(seglen+p,1)/sqrt(seglen+p)*norm(d);
wgnexcitation = wgnsq*norm(d);
synframe = filter(1, alpha(:,i), wgnexcitation(1:seglen));
% synspeech(segstart:segend) = synspeech(segstart:segend) + synframe;
wsynframe = synframe .* w;
synspeech(segstart:segend) = synspeech(segstart:segend) + wsynframe;
segstart = segstart + stepsize;
segend = segend + stepsize;
i
end