Google

main
dspblog
cyclic_signals
FFT_interpolation
FFT_interpolation_how_does_it_work
FFT_smoothness_cyclic
discrete_time_reconstruction
discrete_time_reconstruction2
Nyquist_on_the_edge
FFT_delay_special_case
Fourier_reconstruction
pulse_and_Nyquist
FFT_complexError
matlabOctave
matlab_upsampling
matlab_downsampling
FFT_delay
FFT_filter_example
FFT_interpolation_example
FFT_bin_frequencies
fit_signal
FFT_peaksearch_audio_example
matlab_binary_readwrite
Octavesvg
C
FFTW_example
looprecord
SNR
SNR3
SNR_example_96kAudio
SNR_FFT_correlation_example
lua
luagpib
luasplit
luadump
mnoofltk
wxLuaDll
wxLua_loadAsDll
wxLua_HelloWorld
wxLua_simpleButton
wxLua_resourceManagement
wxLua_XMLparser
DSP
IQ_LO
IQ_LO_2
optimum_receiver
DSP_basics
sampleRateChange_terms
dirac_pulse
freqresp_s
zfilter_example
freqresp_z
freqresp_z_sign
misc
zero_forcing_equalizer_example
nonminphase_inverse
periodic_spectrum
lagrange_multipliers
Entropy
RC_chopper
TRex450_setup
EP100
EP100SE
EP100Gremlins
essential_spares
tail_rotor
motoradjustment
blade_balancing
blade_repair
GAUI_SAE12A
Walkera43


valid html (click to verify)



prevupnextdisable ads

FFT peak search-pitch detection experiment

Summary

A Matlab / Octave example program that finds the peak in the spectrum of an audio audio file and gives the corresponding pitch.
The simple approach works for basic waveforms, but is not a good pitch detection algorithm per se (see the comp.dsp thread).
Related: Usenet comp.dsp thread

Matlab / Octave example program


Download note.wav
% *******************************************************
% FFT peak search example
% Markus Nentwig 2007
% This program is in the public domain
% Note: Peak search as shown works with basic waveforms
% such as sine, triangle, square, sawtooth etc, but is not a 
% good general pitch detection algorithm 
% *******************************************************

% read wave file
close all; clear all;
[y, fs, bits]=wavread('note.wav');
[n, nChan]=size(y);
if nChan > 1
  error('this example requires a mono audio file');
end

% calculate the frequency corresponding to each FFT bin.
% this includes negative frequencies!
freqbase=fs*(mod(((0:n-1)+floor(n/2)), n)-floor(n/2))/n;

% apply window and get FFT. "Hamming" could be omitted
spectrum=fft(y .* hamming(n)); 

% get power (square of bins)
spectrum=spectrum .* conj(spectrum);

% pick out the audible range (for plotting)
bin1=min(find(freqbase > 16));
bin2=min(find(freqbase > 16000)-1);
spectrum=spectrum(bin1:bin2);
freqbase=freqbase(bin1:bin2);

% plot. Empty bin: use -150 dB, can't take log(0).
plot(freqbase, 10*log10(spectrum+1e-15)); xlabel('f/Hz'); ylabel('p/dB');

% peak search. Note: comparing floats by == is OK here. 
% 'find' can return several results.
peakbin=find(spectrum==max(spectrum));

% look up the frequency corresponding to the bin
fPeak=freqbase(peakbin)

% For comparison: The fundamental pitch of note.wav.
% keyhole C => 9 semitones below 440 Hz "A"
fActual_Hz=440*power(2, -9/12)

The spectrum is shown in figure 1.

Figure 1: Spectrum


prevupnextdisable ads

© Markus Nentwig 2007-2008
The content of this page is provided without any warranty and may not be reproduced without permission.

Comments? Questions?

Please send me a mail! mnentwig@elisanet.fi