% formant extraction
% based on 
% https://vlab.amrita.edu/?sub=3&brch=164&sim=615&cnt=1
%
% need -- pkg load signal
 
fsize = 256 ;
recorder = audiorecorder (8000, 16, 1);
recordblocking (recorder, 0.2);
data = getaudiodata (recorder) ;

% window[ii] = float2fix14(1.0 * (1.0 - cos(6.283 * ((float) ii) / (N_WAVE - 1))));
w = 1.0 - cos(6.283 * ([1:fsize]' /fsize))  ;
pause (2.0)
%sound(data)

% preemphisis
for i=[2:fsize]
  data(i) =  (data(i) - 0.95 * data(i-1)) ;  
endfor
data(1) = 0;

% specgram (x, n, Fs, window, overlap)
%figure 4; clf ;
%specgram (data, 256, 12000, :, 0)

% the fft
f = log(abs(fft(data(1:fsize),fsize)));
figure 1; clf ;
plot((f))
set(gca,'xlim', [0,fsize/2])

% get the cepstrum
ff = (fft(f,fsize)) ; % cepstrum
figure 2; clf ;
plot(log(abs(ff)), 'r')
set(gca,'xlim', [0,fsize/2])

% lowpass cepstrum
mask =  zeros(size(ff));
% low pass position
lp = 30;
mask(1:lp) = 1;
mask(lp+1) = 0.5;
%mask(lp+2) = 0.25;
ff = ff .* mask ;
iff = (abs(fft(ff,fsize)));
figure 3; clf ;
% minus sign here is weird, but seems to be necessary
plot(-iff, 'r')
set(gca,'xlim', [0,fsize/2])

