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


% in the diary
ex6 = myquant(ex, [-18 22], 2^6);
rq = lpcsynth(ai,g,ex6);
% sound(rq)
% % sounds pretty good...

disp('Diary range: [-18,22]');
% SNR
Errq = 10*log10(mean((r-rq).^2))
% and the comparison...
Er - Errq


% reduce the range a lot...
ex6 = myquant(ex,[-5,10],2^6);
rq = lpcsynth(ai,g,ex6);
wavwrite(rq,sr,'c_reducedRange.wav');
% sound(rq)
% % still pretty good...

disp('Reduced range: [-5,10]');
% SNR
Errq = 10*log10(mean((r-rq).^2))
% and the comparison...
Er - Errq
% practically the same error!!


% really, really reduce the range...
ex6 = myquant(ex,[-2,5],2^6);
rq = lpcsynth(ai,g,ex6);
wavwrite(rq,sr,'c_reallyReducedRange.wav');
% sound(rq)
% % I thought this still sounded pretty good

disp('Really reduced range: [-2,5]');
% SNR
Errq = 10*log10(mean((r-rq).^2))
% and the comparison...
Er - Errq
% not too bad, still let's stick with [-5,10] and reduce the
% number of bits...


% 4 bits seems to still do a good job...
ex4 = myquant(ex,[-5,10],2^4);
rq = lpcsynth(ai,g,ex4);
wavwrite(rq,sr,'c_4-bitsEx.wav');
% sound(rq)

disp('4 bits');
% SNR
Errq = 10*log10(mean((r-rq).^2))
% and the comparison...
Er - Errq


% but 3 bits doesn't sound very good to my ears
ex3 = myquant(ex,[-5,10],2^3);
rq = lpcsynth(ai,g,ex3);
wavwrite(rq,sr,'c_3-bitsEx.wav');
% sound(rq)

disp('3 bits');
% SNR
Errq = 10*log10(mean((r-rq).^2))
% and the comparison...
Er - Errq


% multi-pulse encoding
[t,m] = lpcMPEenc(ai,ex,64);

% decoding...
exr = lpcMPEdec(t,m);

% synthesizing...
rq64 = lpcsynth(ai,g,exr);
wavwrite(rq64,sr,'c_64mm.wav');
% sound(rq);
% % sounds good to me!

disp('Multi-pulse: 64');
% SNR
Errq = 10*log10(mean((r-rq64).^2))
% and the comparison...
Er - Errq
% not much change


% multi-pulse encoding
[t,m] = lpcMPEenc(ai,ex,16);

% decoding...
exr = lpcMPEdec(t,m);

% synthesizing...
rq16 = lpcsynth(ai,g,exr);
wavwrite(rq16,sr,'c_16mm.wav');
% sound(rq);
% % it sounds a little fuzzier, maybe?  It's hard to tell a difference

disp('Multi-pulse: 16');
% SNR
Errq = 10*log10(mean((r-rq16).^2))
% not quite as good, but still good
Er - Errq


% multi-pulse encoding
[t,m] = lpcMPEenc(ai,ex,45);

% decoding...
exr = lpcMPEdec(t,m);

% synthesizing...
rq45 = lpcsynth(ai,g,exr);
wavwrite(rq45,sr,'c_45mm.wav');
% sound(rq);
% % sounds good to me!

disp('Multi-pulse: 45');
% SNR
Errq = 10*log10(mean((r-rq45).^2))
% and the comparison...
Er - Errq
% not much change

% how does the bit-rate ratio look for multi-pulse vs. excitation?
num_ex_bits = prod(size(ex))*4; % 4 bits/number
num_m_bits = prod(size(m))*4; % 4 bits/number 
num_t_bits = log2(128)*prod(size(t)); % window size

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

ex_bit_rate = num_ex_bits/t % bits/second
m_bit_rate = num_m_bits/t  
t_bit_rate = num_t_bits/t
total_bit_rate = (num_m_bits + num_t_bits)/t







