
% CCRMA MIR Workshop 2009
% Lab1.7: Phase-based Onset Detection
% Kyogu Lee (June 2009)

clear all;
close all;

% read audio file
%[x,fs] = wavread('../wav/T39-piano-8k.wav'); % vowel 'ooh'
[x,fs] = wavread('../wav/T08-violin-8k.wav'); % vowel 'ooh'
if size(x,2) > size(x,1)
    x = x'; % make a column vector
end
x = mean(x,2); % make mono
x = x(1:2*fs);

M = 512;
win = hamming(M);
hopsize = 1;
hopfac = M/hopsize;

% phase deviation
% function [pd, dif, T, F] = phasedev(x,fs,Nfft,hopfac)
hopfac = 2;
[pd,dif,T,F] = phasedev(x,fs,M,hopfac);

dist_step = [-.5:.01:.5];
pd_dist = zeros(length(dist_step),length(T)); % pd distribution
for t=1:length(T)
    pd_dist(:,t) = hist(dif(:,t),dist_step);
end

% detection function using Kurtosis measure
df_kurtosis = kurtosis(dif,1);

% display
subplot(411); plot([0:length(x)-1]/fs,x); title('signal'); ylabel('x(t)');
subplot(412); imagesc(T,dist_step,pd_dist); colormap(1-gray);
subplot(413); plot(T,pd); title('mean absolute phase deviation'); ylabel('df(t)');
subplot(414); plot(T,df_kurtosis); title('Kurtosis'); ylabel('df(t)');
