Linear to semi-logarithmic scale I've got some FFT results I want to draw with a log10 scale on the

Ximena Skinner

Ximena Skinner

Answered question

2022-07-01

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 ...)]

Answer & Explanation

trantegisis

trantegisis

Beginner2022-07-02Added 20 answers

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 m x + 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 ( a f ) 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 n P, so
n P = m log ( 22050 / 20 )
So m = n P / log ( 22050 / 20 ), which is approximately n P / 3. Thus, given a frequency f, it should be located at about p = ( n P / 3 ) log ( f / 20 )
Hopefully that lines up with the answer you've found empirically

Do you have a similar question?

Recalculate according to your conditions!

Ask your question.
Get an expert answer.

Let our experts help you. Answer in as fast as 15 minutes.

Didn't find what you were looking for?