# Linear to semi-logarithmic scale

I've got some FFT results I want to draw with a log10 scale on the x axis.

Let's call

• nBins the number of bins (window size / 2)
• nPixels the total number of pixels

We will assume that the frequencies are between 20 Hz and 22050 Hz

For each bin, I'd like to find out the corresponding pixel.

First, converting between a bin and it's corresponding frequency is easy :

freq= nBins/22050* numBin +20

Now I have freq it would be easy to draw it on a linear x scale but how to draw it on a log10 x scale ?

My math is really rusty and this doesn't seem trivial (maybe it is ...)

• OK I managed to code this, so it's not a problem anymore but I did empirically and I'd love to understand the maths behind. – Dinaiz Mar 9 '14 at 8:23

## 1 Answer

as I understand your question, when you are at freq = 20Hz, you want to be at pixel 0, and when freq = 22050Hz you want to be at pixel nPixels so that you're using the full amount of space available for the problem at hand. To do this, you need to scale the logarithm of your frequencies to the appropriate size. We do this with a constant scaling multiple in front of the log function. In order to put the origin of the x axis in the right place, we also have to add a constant term. This would look like a linear function $mx + b$, except the $x$ in this case is a log function of frequency, so $m\log(f) + b$. I am going to shorten this using logarithmic identities to $m\log(af)$ for some constant $a$.

For your particular problem, we would want to be at pixel 0 for $f=20$, and the only way to get 0 is to make sure the inside of the logarithm is equal to 1. so in this case, $a=1/20$. For $f=22050$ we want to be at pixel $nP$, so

$$nP = m\log(22050/20)$$

So $m=nP/\log(22050/20)$, which is approximately $nP/3$. Thus, given a frequency $f$, it should be located at about $p = (nP/3)\log(f/20)$.

Hopefully that lines up with the answer you've found empirically

• Thanks, that's exactly what I was looking for. – Dinaiz Mar 16 '14 at 20:13