d = wavread('mpgr1_sx419-8k.wav')';
sr = 8000;
[ai,g,ex] = lpcfit(d);

% original signal
Ed = 10*log10(mean(d.^2))

r = lpcsynth(ai,g,ex);
% best case re-synthesis
Er = 10*log10(mean(r.^2)) 

% good quality re-synthesis: 8 bits
ai8 = myquant(ai,[-3,3],2^8);
rq8 = lpcsynth(ai8,g,ex);
wavwrite(rq8,sr,'a_8-bits.wav');
% sound(rq8);

% SNR
Errq = 10*log10(mean((r-rq8).^2))

% not enough bits (7) - blip noises result
ai7 = myquant(ai,[-3,3],2^7);
rq7 = lpcsynth(ai7,g,ex);
wavwrite(rq7,sr,'a_7-bits.wav');
% sound(rq7);

% SNR
Errq = 10*log10(mean((r-rq7).^2))

% not enough bits (6) - weird gloopy noises result

ai6 = myquant(ai,[-3,3],2^6);
rq6 = lpcsynth(ai6,g,ex);
wavwrite(rq6,sr,'a_6-bits.wav');
% sound(rq6);

% SNR
Errq = 10*log10(mean((r-rq6).^2))

% not enough bits (5) - really LOUD noise
ai5 = myquant(ai,[-3,3],2^5);
rq5 = lpcsynth(ai5,g,ex);
wavwrite(rq5,sr,'a_5-bits.wav');
% sound(rq5);

% SNR
Errq = 10*log10(mean((r-rq5).^2))







