clear all

[d,r] = wavread('test4small.WAV'); %4
%A little noise inproves the numerical stability
d = d + .00001*randn(size(d));

% Extract LPC parameters
%a is a matrix of IIR filter coefficients for each time segment
%g is a gain for each time segment
[a,g] = lpcfit(d,8);

%throw away a(:,1) which is always 1
%and put g in its place
%then multiply everyting by 256 to make it into integers for efficient
%storage
a(:,1) = g ;
c = round(256*a);

%make a textfile with Codevision source code in it.
fname='LPCsource.c';
fid = fopen(fname,'w');
[hops,poles] = size(c);
fprintf(fid,'flash int lpctable[%d,%d]={\r',hops,poles);
for i=1:hops-1
    fprintf(fid,' %5d, %5d, %5d, %5d, %5d, %5d, %5d, %5d, %5d,\r',c(i,:));
end
fprintf(fid,' %5d, %5d, %5d, %5d, %5d, %5d, %5d, %5d, %5d\r',c(hops,:));
fprintf(fid,'};\r');
fclose(fid);

%and resynthesize
%dl = lpcsynth476(a,g);
dl = lpcsynth476(c);
soundsc(dl,r);

% Compare the spectrograms
figure(1);clf
subplot(211)
specgram(d,256,r);
title('Original');
subplot(212)
specgram(dl,256,r);
title('synth');