clear all
% WAV file produced by the very good ATT text-to-speech utility
% http://www2.research.att.com/~ttsweb/tts/demo.php
[s, Fs] = wavread('Z:\Matlab\476\speech\Mike_digits_16khz.WAV');

%OPTIONAL !! drop the sample rate to 8 kHz IF you can.
% I find with numeric digits "six" is not clear
% at 8 kHz
s = resample(s,1,2);
Fs = 8000; 
figure(1)
subplot(2,1,1)
spectrogram(s,256,100,256,Fs, 'yaxis')
set(gca, 'ylim', [0 4000])

% now scale to 0-to-15 for the 4-bit DAC
min_s = min(s);
max_s = max(s);
s = s - min_s;
s = fix(s * (15.5/(max_s-min_s)));
%hist(diff(s),20)
ds = diff(s);
ds(ds>1) = 1;
ds(ds<-1) = -1;

ss = zeros(size(ds));
% integrate wave from truncated derivitive
% need a highpass term to stabilize the integration
for i=2:length(ds)
    ss(i) = ds(i) + ss(i-1)-(ss(i-1)-7)*1/127;
    if(ss(i)>15) 
        ss(i) = 15;
    end
    if(ss(i)<0) 
        ss(i) = 0;
    end
end

subplot(2,1,2)
% lowpass
[b,a] = butter(1,1600/4000); %10,000 rad/sec
ss = filter(b,a,ss);
spectrogram(ss,256,100,256,Fs, 'yaxis')
set(gca, 'ylim', [0 4000])

%and play it
sound(ss/16, Fs)
wavwrite(ss/16, Fs, 16, 'dpcm_filtered.wav')


