Search code examples
c++audioalgebrafmod

Convert decibel range to byte range


Greeting,

I'm trying to find a formula to convert a range where:

min = -100db and max = -30db

to:

min = 0 and max = 255

for example: -60db = ?

Seems easy but it makes my head spin.


Solution

  • Assuming you mean the result to stay in terms of db, you're asking for a simple linear interpolation:

    f(x) = ymin + (x - xmin)*(ymax - ymin)/(xmax - xmin)
    

    or in your case,

    f(x) = 0 + (x + 100)*(255 - 0)/(-30 + 100) 
    f(-60 db) = 145.714 
    

    If instead you're talking about converting db to a scale factor with which to multiply an audio signal, then it's a bit more complex. For example, to multiply an audio signal by 0 is negative infinity db. So (at the very least) you'd have to special case that.