Skip to content

Arduino: Compensating for a Logarithmic Curve

2011 August 4
by Tedb0t

Did you know that the scale of acoustic pitches is logarithmic?  This means that, even though we perceive the scale of notes as increasing or decreasing linearly, the actual frequencies are doubling or halving with each octave.

In my Deconspectrum installation, I’m mapping acoustic frequencies to color.  If I mapped them linearly, the colors would change very rapidly in the low frequencies, and then stop changing as obviously as the pitch gets higher.  This is not the effect you would expect, since you want each pitch to be represented by another color.

The solution is to map frequency to color (hue) logarithmically instead of linearly.  Thankfully, AVR C gives us a handy base-10 log function:

int hueFreq = log10(frequency - FREQ_MIN) * hueScale;

If we graph this, we get:

The ‘y’ scale is hue and the ‘x’ is frequency.  You can see that, with each octave, the change in color progresses more naturally, mimicking our linear perception of pitch.

Here’s the same graph with a logarithmic frequency axis:

The dotted line is the log10 function without the cutoff, so you can see the “simulated” linearity.

Handy, and highly effective!

Related Posts:

Comments are closed.