0

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.

neosettler
  • 261
  • 3
  • 14
  • Just to clarify, do you mean the byte range also to be (relative) db? Or do you mean to convert it to a multiplier for a signal (in linear space)? – fearless_fool Jan 30 '17 at 00:16
  • The idea is to mimic what the WebAudio API is doing with getByteFrequencyData that returns a byte array. https://webaudio.github.io/web-audio-api/#smoothing-over-time It seems like the actual db relation is lost in the process where it gives only a linear multiplier as you suggest. The equivalent function in FMOD that I’m using does return linear values between 0 and 1 and I’m simply looking to interpolate the result to 0-255. – neosettler Jan 30 '17 at 00:40
  • You could clarify your question by telling us what you'd expect the formula to produce for -60db. f(-60) = ?. – fearless_fool Jan 30 '17 at 00:44
  • As simply as I could put it, if -100db = 0 and -30db = 255, what value between 0-255 -60db would give us? – neosettler Jan 30 '17 at 00:52

1 Answers1

2

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.

fearless_fool
  • 33,645
  • 23
  • 135
  • 217
  • As fearless brought it up, I think the result should indeed be a multiplier instead of in db. To confirm, I plugged the linear interpolation and it seems to be off the chart. Should x be converted from db to linear first? – neosettler Jan 30 '17 at 00:46
  • @neosettler: I read the doc you referenced (https://webaudio.github.io/web-audio-api/#smoothing-over-time) and it seems pretty clear that everything stays in units of dB, so I believe linear interpolation is appropriate here. What's more, I plugged '-60' into the formula, and it returns 45.71, which is well within the range of 0 - 255. So one of us is confused! :) – fearless_fool Jan 30 '17 at 17:47
  • Thank you for your input fearless. It appears that there is a mistake in your formulas. Regarding to this site: http://www.ajdesigner.com/phpinterpolation/linear_interpolation_equation.php#ajscroll It should be : f(x) = ymin + (x - xmin)*(ymax - ymin)/(xmax - xmin) = 145.71428571429 – neosettler Jan 30 '17 at 21:43
  • Doh! You're right -- I'll correct the formula. But with the corrected formula, f(-60) => 145.7 is still within the 0-255 range. – fearless_fool Jan 31 '17 at 01:10
  • Yes indeed, it seems to be the correct answer. You've been helpful thank you. – neosettler Jan 31 '17 at 05:33
  • @neosettler: Not that I need the points, but if you found it helpful, you should mark this as the correct answer. That will help others in the future. – fearless_fool Jan 31 '17 at 18:30
  • I checked the check mark, is that what you mean? neosettler <--- noob! – neosettler Jan 31 '17 at 21:46