Skip to content

Instantly share code, notes, and snippets.

@mrmikejones
Created February 12, 2013 11:22
Show Gist options
  • Save mrmikejones/4761676 to your computer and use it in GitHub Desktop.
Save mrmikejones/4761676 to your computer and use it in GitHub Desktop.
Conversion between decibel and linear scales
#include <stdio.h>
#include <math.h>
static double ConvertDecibelsToLinear(double dB)
{
if(dB <= -60.0)
{
return 0.0;
}
return pow(10.0, dB/20.0);
}
static double ConvertLinearToDecibels(double linear)
{
if(linear <= 0.0)
{
return -60.0;
}
return 20.0 * log10(linear);
}
int main(int argc, char* argv[])
{
printf("-6dB in linear is %.2f\n", ConvertDecibelsToLinear(-6.0));
printf("0.25 in decibels is %.2f\n", ConvertLinearToDecibels(0.25));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment