www.gusucode.com > 声音的处理有:LPC,FFT,共振峰,频谱源码程序 > siganlandsystemusingMatlab/SSUM/feature/pitch1.m

    %
%  Auto-Correlation Pitch Detection Algorithm
%
% Code borrowed in part from C.H. Wong
% http://www.cse.cuhk.edu.hk/~chwong1/research.html#MatLabCode
%
% Something happens around f = 2000... it starts going down.
% Use only on signals with a fundamental < 1000 Hz.
% Return freq, and error estimate!

function freq = pitch1(frame, Fs)

	num_samples = length(frame);

	frame_xcorr = xcorr(frame, frame, 'coeff' );

	length_xcorr = length(frame_xcorr);
	peak = frame_xcorr;
	peak(num_samples) = 0;

	% Find the Peaks
	for i=2:length(frame_xcorr)-1
		if frame_xcorr(i)>frame_xcorr(i-1) & frame_xcorr(i)>frame_xcorr(i+1)
			peak(i) = frame_xcorr(i);
		else
			peak(i) = 0;
		end
	end

	% Find the maximum
	[sort_peak,sort_peak_index] = sort(-peak);

	% Choose three highest peaks
	index = sort(sort_peak_index(1:3));
	p1 = index(2)-index(1);
	p2 = index(3)-index(2);
	freq = Fs/((p1+p2)/2);