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)) 

lp = lpca2lsp(ai);

% quantizing to 7 bits sounds fine now
lp7 = myquant(lp,[0,0.5],2^7);
air = lpclsp2a(lp7);
rq7 = lpcsynth(air,g,ex);
wavwrite(rq7,sr,'b_7-bits.wav');
%sound(rq7);

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


% six sounds a bit messy
lp6 = myquant(lp,[0,0.5],2^6);
air = lpclsp2a(lp6);
rq6 = lpcsynth(air,g,ex);
wavwrite(rq6,sr,'b_6-bits.wav');
%sound(rq6)

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

% how does the bit-rate ratio look for multi-pulse vs. excitation?
num_ai_bits = prod(size(ai))*8; % 8 bits/number
num_lp_bits = prod(size(lp))*7; % 7 bits/number 

% figure out the original time scale
sr = 8000;
t = length(d)/sr; % 3.0656 seconds

ai_bit_rate = num_ai_bits/t % bits/second
lp_bit_rate = num_lp_bits/t  

