
% plot and compare LP and FFT spectra

clear all;

wavFile = '/home/paddy/pappu/work/wavs/tfhtl.wav';
%wavFile = '/home/paddy/pappu/work/wavs/tsyww.wav';
%wavFile = '/home/paddy/pappu/work/wavs/tfxd.wav';
vad='/home/paddy/pappu/work/vads/tfhtl-a.vad';
%vad='/home/paddy/pappu/work/vads/tsyww-a.vad';
%vad='/home/paddy/pappu/work/vads/tfxd-a.vad';
LP_ORDER = 20;
FRAME_SIZE = 240;
FRAME_SHIFT = 120;
NFFT = 256;
STE_LEN = 20;
LW=2;

[wav,fs] = wavread(wavFile);
wav = wav(:,1);

[vadMask, junk] = read_VAD(vad,FRAME_SIZE/fs,FRAME_SHIFT/fs,length(wav)/fs);
vadMask = logical(vadMask);

speechFrames = enframe(wav, boxcar(FRAME_SIZE), FRAME_SHIFT);
speechFrames = speechFrames .* repmat(hamming(size(speechFrames,2))', ...
size(speechFrames,1), 1);

% do VAD
speechFrames = speechFrames(vadMask,:);
numFrames = length(speechFrames);

% plot spectrogram
% uses different parameters, so omitting for the moment
%[t_sg,f_sg,b_sg] = spgrambw(wav,fs,'p');


% magnitude spectrum
disp('Computing power spectrum');
magSpec = fft(speechFrames',NFFT);
magSpec = (abs(magSpec));
magSpec = magSpec(1:NFFT/2,:).^2;
o_magSpec = magSpec';

% log magnitude spectrum
logMagSpec = log(magSpec);
o_logMag = logMagSpec';

% lp spectrum
disp('Computing LP mag spectrum');
[a,e] = lpc(speechFrames',LP_ORDER);
A = fft(a',NFFT);
A = bsxfun(@rdivide,sqrt(e'),A);
A = abs(A(1:NFFT/2,:)).^2;
o_lpSpec = A';

%LP log mag spec
o_lpLogMag = log(o_lpSpec);

% LP group delay spectrum
% We dont need this for the moment
%disp('Computing LP gdelay spectrum');
%tau = extract_lpGdelayVec_diff(speechFrames,LP_ORDER,NFFT);
% proposed feature
% make gd positive
%minFV = min(tau,[],2);
%tauPos = bsxfun(@plus,tau,abs(minFV));
%o_lpGdelay = tauPos;

for i = 1:numFrames
	% plot for each frame indivdually

	subplot(3,2,1);
	currFrame = speechFrames(i,:);
	plot(currFrame,'LineWidth',LW);
	title('Speech frame');
	
	%subplot(3,2,2);
	%imagesc(t_sg,f_sg,10*log10(b_sg')); 
	%axis('xy');
	
	subplot(3,2,3);
	plot(o_magSpec(i,:),'LineWidth',LW);
	title('Mag spec');
	
	subplot(3,2,4);
	plot(o_logMag(i,:),'LineWidth',LW);
	hold on;
	plot(o_lpLogMag(i,:),'r','LineWidth',LW);
	hold off;
	legend('logmag','LP log mag');
	title('log mag spec');
	
	subplot(3,2,5);
	plot(o_lpSpec(i,:),'LineWidth',LW);
	title('LP mag spec');
	
%	subplot(3,2,6);
%	plot(o_lpGdelay(i,:),'LineWidth',LW);
%	title('LP gdelay');

	pause;
end


