How would I convert from frequency back to percent?

4.1k Views Asked by At

I'm working on a web application that uses an oscillator. It has a parameter that goes from 0 to 100 percent. I translate that percent into hertz using this equation: $$ \text{frequency} = 2^{(\lfloor 128*\text{percent} \rfloor - 69)/12}*440 $$

I am having some trouble solving the equation for percent so that given a frequency, I can get a percent value to send back to my app. Help please? :]

3

There are 3 best solutions below

0
On BEST ANSWER

$$f = 2^{(x - 69)/12} \times 440$$ inverts into $$x = 69 + 12 \log_2(f/440)$$ but in your case $x = \lfloor 128p \rfloor$, which is impossible to invert exactly because it maps intervals to single numbers (i.e. the function is not one-to-one and so does not have an inverse).

For example, $p = 10^{-9}$ and $p = 10^{-8}$ would yield the same $x=0$, so if I tell you $x=0$, there is no way to imply what the original $p$ was directly.

EDIT You could bound it, however. $x$ must certainly be an integer and then, since you are rounding down, $x = \lfloor 128p \rfloor$ means $$ x \leq 128p < x+1 \Leftrightarrow \frac{x}{128} \leq p < \frac{x+1}{128} $$

2
On

Since the floor function is not bijective, one can not invert this equation. Ignoring the floor function gives: $$p=\frac{69}{128}+\frac{12\ln\left(\frac{f}{440}\right)}{128\ln 2}$$ where $f$ is the frequency and $p$ is the percentage.

0
On

There will not be a unique answer because the $\lfloor\cdot\rfloor$ function is not 1-1. There will be a range of values. All of the values of $p$ will be (where $f$ denotes frequency and $p$ denotes percent) at least

$$(69 + 12\log_2(f/440))/128$$

but strictly less than

$$(70 + 12\log_2(f/440))/128,$$

I believe. The reason is that if $\lfloor x \rfloor = n$, then $n\leq x < n+1$. You should be able to test this pretty quickly (I'm lazy).