Noob question - convert log eq scale value back to freq?

DSP, Plugin and Host development discussion.
Post Reply New Topic
RELATED
PRODUCTS

Post

Hi,

I hope someone can help me, I've found this JS function which im using to plot a logarithmic EQ scale onto HTML 5 canvas.

I now am getting back the horizontal cursor position, in px, but have no idea how to convert this back to it's corresponding frequency value.

I have been googling for hours and trying loads of things in codepen, but my maths is abysmal so I need help!!

I am tying this into HTML5 web audio api, to control a filter.

This is the function im using, over a scale of 1000px.

Code: Select all

var width_px = 1000;
function frequency_to_px(frequency) {
    var min_f = Math.log(20) / Math.log(10),
        max_f = Math.log(16000) / Math.log(10),
        range = max_f - min_f,
        position_px = (Math.log(frequency) / Math.log(10) - min_f) / range * width_px;
        return  position_px;
}

My early codepen just plotting that to canvas (before i've added all the mouse events to get the cursor etc) is here https://codepen.io/anon/pen/YRbVXQ

Any help greatly appreciated!

Post

Ok by sheer luck (that the original function i found had a spelling mistake) - this lead me to this github (https://github.com/wtandt/spectralpool/ ... e.com.html) , which had the same spelling mistake, but a slightly different implementation.

After a while of playing around, I reached this function, which from testing, seems almost on the money (if i round to 2dp)

Code: Select all


function px_to_frequency(px) {

    var min_f = Math.log(20) / Math.log(10),
        max_f = Math.log(16000) / Math.log(10),
        range = max_f - min_f,    
        position_frequency = Math.exp(( range * px/width_px)* Math.log(10)+min_f);

        return  position_frequency*5.44505;
}

But where on earth does the final ratio 5.44505 come from, what am I supposed to have calculated that from ( i got it from trial and error).... is that related to the scale somehow?

I am baffled, but at least I have something...!

Post

The correct inverse function is:
f = exp((px * range / width_px + min_f) * log(10))

Post

Max M. wrote: Wed Dec 05, 2018 2:07 am The correct inverse function is:
f = exp((px * range / width_px + min_f) * log(10))
Thank you!!!!! :D

Post Reply

Return to “DSP and Plugin Development”