www.pudn.com > pitch_detect.rar > pitch_detect.m


function [pitch] = pitch_detect(x) 
%PITCH_DETECT Performs pitch detection on a speech waveform 
% 
% p = pitch_detect(x); 
% x is vector containing frame of speech data 
% p is scalar containing pitch of frame in Hz or 0 if unvoiced 
Fs = 8000; 
% First, remove the dc value of the frame by subtracting the mean . . . 
x = x-mean(x); 
% Then find the minimum and maximum samples and center clip to 
% 75% of those values (`cclip') . . . 
x = cclip(x,'clc',0.75); 
% Compute the autocorrelation of the frame . . . 
x = xcorr(x,'coeff'); %calculates the autocorrelation and also normalizes the maximum of the correlation to be 1 
maxim = find(x== max(x)); %finding the index of the maximum value 
poscorr = x(maxim:length(x)); %extracting the positive (t = positive) part of the correlation 
% Find the maximum peak following Rx[0] (`peak') . . . 
[peakporc index] = peak(poscorr); 
% peakporc=0.3935; 
% index=88; 
peaktime = index/Fs; % converting from index of sample to seconds 
% Determine if the segment is unvoiced based on the 'voicing strength' 
% (the ratio of the autocorrelation function at the peak pitch lag 
% to the autocorrelation function lag=0) . . . 
%calculating what porcentage of the maximum at zero represents the founded maximum (without mult by 100) 
% If voicing strength is less than 0.25, call it unvoiced and set pitch = 0, 
% otherwise compute the pitch from the % index of the peak . . . 
if peakporc<.25, 
%the segment is unvoiced 
pitch = 0; 
else 
%the segment is voiced 
pitch = 1/peaktime; 
end