
% KAIST CS891 Special Lecture on Music Information Retrieval (MIR)
% Lab2.2: Spectral envelope
% Kyogu Lee (August 2008)

clear all;
close all;

% read audio file
%[x,fs] = wavread('../wav/ooh.wav'); % vowel 'ooh'
[x,fs] = wavread('../wav/T08-violin.wav'); % violin
x = mean(x,2); % make mono

% analysis window size
M = 2^nextpow2(floor(fs*.1)); % 100ms long
%M = 1024;

% first compute spectrogram
[spec,fi,ti] = specgram(x,M,fs,hann(M),M/2);
nframe = length(ti);

% (1) linear predictive coding (LPC)
xt = x(1:M); % first frame
lpc_ord = 20; % lpc order
a = lpc(xt,lpc_ord); % allpass filter coefficients

% now plot original spectrum and its envelope
X = spec(:,1);
plot(fi,20*log10(abs(X))); hold on;
h = freqz(1,a,fi,fs); % filter frequency response
plot(fi,20*log10(abs(h)),'r');



% (2) cepstrum analysis
% use CepsAna() in CATbox
% function [C,S,E,win_pos] =  CepsAna (a,lift,win,overlap,nfft)
[ceps,se] = CepsAna(x,30,hamming(M),M/4,M); % ceps: cepstral coefficients, se: spectral envelope

% display
figure(2);
subplot(211);
plot(fftshift(ceps(:,1)));
subplot(212);
plot(fi,20*log10(abs(X))); hold on;
plot(fi,20*log10(abs(se(:,1))),'r');



% compute MFCC
% [ceps,freqresp,fb,fbrecon,freqrecon] = mfcc(input, samplingRate, [frameRate])
[mfceps,freqresp,fb,fbrecon,freqrecon] = mfcc(x,fs,fs/(M/2));

% display