function pitch = detPitch(x,wsize,sol)
% DETPITCH Detects the pitch sequence within a sequence of samples.
%
% [PITCH] = DETPITCH(X,WSIZE,SOL) where the out (PITCH) collects the pitch sequence
% estimated from the input X, being WSIZE the size of the analysis window in seconds
% (tipically between 25-40ms) and SOL the overlap between two consecutives frames
% also in seconds (tipically between 25-50% of the analysis window length).
% Copyright (C) Iván López Espejo 2010
% Version: Id: detPitch.m , v 1.0 2010
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You can obtain a copy of the GNU General Public License from
% ftp://prep.ai.mit.edu/pub/gnu/COPYING-2.0 or by writing to
% Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Preprocessing.
fm1 = 44100;
fm = 8000;
x = (x(:,1) + x(:,2))/2;
x = resample(x,fm,fm1);
x = x/max(abs(x));
% VAD parameters.
THRESHOLD = 0.01;
MIN_SPEECH_FRAME_HANGOVER = 4;
HANGOVER = 1;
hangOver = 0;
meanEn = 0;
nbSpeechFrame = 0;
ind = 1;
% Designing of the Butterworth low-pass filter with cut frequency of 800Hz.
fc = 800;
Nf = 2; % Filter order.
Wn = 2*fc/fm;
[b,a] = butter(Nf,Wn,'low');
x = filter(b,a,x); % Low-pass filtering.
x = resample(x,2*fc,fm); % Downsampling.
% Frame analysis.
wsize = round(wsize * 2 * fc);
sol = round(sol * 2 * fc);
% Window constants.
a0 = 0.53836;
a1 = 0.46164;
Npred = 12; % Amount of LPC coefficients.
b0 = 1;
fint = 5; % Interpolation factor.
pitch = [];
for m = 1:(wsize-sol-1):length(x)
if wsize > length(x) - m
N = length(x) - m + 1;
else
N = wsize;
end
% Voice activity detection.
frameEn = dot(x(m:m+N-1),x(m:m+N-1))/N;
if frameEn - meanEn > THRESHOLD
VAD(ind) = 1;
nbSpeechFrame = nbSpeechFrame + 1;
if nbSpeechFrame > MIN_SPEECH_FRAME_HANGOVER
hangOver = HANGOVER;
end
else
if hangOver == 0
VAD(ind) = 0;
else
VAD(ind) = 1;
hangOver = hangOver - 1;
end
end
ind = ind + 1;
% Hamming windowing.
n = 1:N;
win = a0 - a1*cos(2*pi*n/(N-1));
frame = x(m:m+N-1).*win';
acoef = lpc(frame,Npred);
eps = filter(acoef,b0,frame); % FIR filtering.
reps = xcorr(eps,'biased');
% Augment of the temporal resolution.
Pm = interp(reps,fint);
Pm = Pm(floor(length(Pm)/2):length(Pm));
% Detection of the maximum peak from the autocorrelation function.
picomax = detPico(Pm);
if VAD(ind-1) == 1
pitch = [pitch (log(2*fc*fint/(picomax-1)))];
end
end
% Plotting the result.
plot(pitch,'-*r')
grid on
xlabel('Evolución temporal')
ylabel('Frecuencia de pitch logarítmica')
title('PITCH estimado')
function pico = detPico(x)
% DETPICO Detects a maximum peak within a sequence of samples.
%
% [PICO] = DETPICO(X) where the out (PICO) returns the position of the
% maximum peak detected in the input, the sequence of samples (X).
% Copyright (C) Iván López Espejo 2010
% Version: Id: detPico.m , v 1.0 2010
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You can obtain a copy of the GNU General Public License from
% ftp://prep.ai.mit.edu/pub/gnu/COPYING-2.0 or by writing to
% Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
THRES = 3; % Default threshold to consider a peak.
sube = 0; % Positive slope counter.
baja = 0; % Negative slope counter.
pico = 0; % Maximum detected peak.
for n = 1:length(x)-1
if x(n+1) - x(n) > 0
sube = sube + 1;
prepico = n + 1;
baja = 0;
end
if x(n+1) - x(n) < 0
if sube > THRES
baja = baja + 1;
if baja > THRES
if pico ~= 0
if x(pico) < x(prepico)
pico = prepico;
end
sube = 0;
else
pico = prepico;
sube = 0;
end
end
else
sube = 0;
end
end
end