Pitch detection plug-in

Here’s a little analysis plug-in for detecting the pitch notes.

In most cases the default settings should work pretty well.
The default frequency range is 100 to 2000 Hz.

For detecting very low frequencies (less than a few hundred Hz) the plug-in should be set to the low frequency range (20 to 1000 Hz).
For detecting very high frequencies (several kHz) the plug-in should be set to the high frequency range (1 kHz to 10 kHz)

By default, the plug-in detects the pitch by analysing the first 0.2 seconds of the selection. In most cases this should work well.
If required the analysed section can be set to the first part of the selection ranging from the first 10th of a second (0.1 seconds) up to one second.

Limitations:

Extremely high frequencies may not be detected very accurately, especially if the sample rate is not very high. The plug-in will often detect very high frequencies better if the sample rate is 96000 Hz rather than 44100 Hz.

This plug-in is intended to detect single notes - you may get strange results if you try to analyse chords.

The plug-in should usually be able to detect pitches of single notes to within a few percent of the actual frequency. Don’t expect the frequency in Hz to be exact. The most accurate measure of frequency in Audacity is to use “Plot Spectrum” and set the “Size” setting to a high value.
Original version:
pitch-detect.ny (3.77 KB)
This plug-in is now available on the Audacity wiki

1 Like

Hi Steve
could need some help in adapting you plug in for my needs, but I’m not allowed to PM you yet.

Would only need the extracted frequency in Hz as a variable for further processing.
Any way you could point guide me there ?

The frequency in Hz is the value of the variable “f0” which is calculated by the function “bestguess” (line 86). The function is called at line 100:

(bestguess (getyin s dur))

This returns the “note” (equivalent to a MIDI note value) or NIL if a confident guess cannot be found.

To convert a “note” value to a frequency, use the function “step-to-hz”

So to get just the frequency value you could use:

(step-to-hz (bestguess (getyin s dur)))

However if (bestguess …) returns NIL, then (step-to-hz (bestguess …)) will produce an error, so you would want to check that it is not NIL before converting to Hz.
This is done in the last section of code (from line 98).

If you just want to set a variable (for example “f0”) to the Hz value, then you can delete from line 98 to the end and replace it with something like:

(setq f0 (bestguess (getyin s dur)))
(if f0 (setq f0 (step-to-hz f0))
       (setf err "Frequency not foundn"))

Before running further code, check that (length err) is 0.

Your code works great.
Thanks, you have saved me a looot of time !

How precise is the frequency - I mean - if I have a singele frequency to analyze not centered to a note ?
For example with a readout from the standard FFT analyze tool I get the peak at say 3.133Hz and the pitch detected says 3.157Hz.

Witch one is “correct” ?

Which tool is that? “Plot Spectrum” does not show decimal places.

Pitch detect does not work with such low frequencies.

To get an idea of accuracy, try generating some tones (“Generate” menu) and test them.

Sorrry, its 3133Hz and 3157Hz thus in teh kHz range

In my tests, the pitch detect plug-in is accurate to within about 1%.
How does that compare with your tests?

Well my example above is a real one, so its even well beyond your findings.
More than good enough, I guess.

I just was unsure if there are possibly steps too wide, if its first hand based on midi notes