Pitch tracking

A project log for hgSynth

ESP32 based monosynth embedded in bass guitar

Igor BrkicIgor Brkic 05/10/2018 at 22:370 Comments

There are many methods for pitch detection of a sound signal both in time and in frequency domain, from very simple to very complex. Simplest one would be to count signal zero crossings and calculate frequency from it. It would work just fine on perfect sine wave without noise and/or higher harmonics.One of the simple ones with good results in presence of noise and  higher harmonics is basic autocorrelation method (and a lot of its variants) which correlates a block of signal with itself to find its periodicity. Of course, we need big enough block of data for this and sometimes it returns double frequency but it's workable. Problem with it is the required amount of calculation. Basically, you place (windowed, to avoid "sharp" edges at the beginning and the end of the block) block of signal on top of itself, multiply matching samples and sum everything. One copy of the signal is then shifted one sample to the side and the process is repeated (for all shifts where overlap exists). Some optimizations can be added by using FFT (algorithm for fast calculation of Fourier transform) but still it's a lot of calculations which isn't really suited for lower powered MCUs (even though ESP32 isn't really that low spec).

So, almost by accident, I discovered a really cool new method for very efficient pitch detection, even on MCUs. It's called Bitstream Autocorrelation and it's invented by Joel de Guzman from Cycfi Research. Check out the site for explanation and sample code. And I was amazed how great it works. Even on signals with really high amount of higher harmonics.

So, I took Joel's demo code pretty much without any changes, stuffed it into synth code and it works!

For bass guitar's signal input conditioning I just added two resistors to bias it to the middle of ESP's ADC input and added a simple RC lowpass (antialiasing :)) filter. It works but the signal is low so a proper signal conditioning which includes both lowpass filtering and amplification is required.