Jump to content

Pitch detection algorithm

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 85.100.168.116 (talk) at 04:37, 8 April 2005. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

An algorithm that attempts to detect the pitch (inverse of frequency) of a periodic signal (for example digitized sound).

Below is an example implementation in pascal language:

(* XAMDF pitch detection algorithm *)
const
 Sample_Size = 128;
 Half_Size   = Sample_Size div 2;
 Sample_Rate = 5000;
var
 Samples     : array [1..Sample_Size] of byte;
function Detect_Frequency:real;
var
 Period, Time     : integer;
 ErrPer1, ErrPerX : longint;
begin
 ErrPer1:=0;
 for Time:=1 to Half_Size do
   ErrPer1:=ErrPer1+abs(Samples[Time]-Samples[Time+1]);
 for Period:=2 to Half_Size do
   begin
   ErrPerX:=0;
   for Time:=1 to Half_Size do
     ErrPerX:=ErrPerX+abs(Samples[Time]-Samples[Time+Period]);
   if (ErrPerX < ErrPer1) then
     begin
     Detect_Frequency:=Sample_Rate/(Period+(ErrPerX/ErrPer1));
     Exit;
     end;
   end;
 Detect_Frequency:=0;
end;