Recursive AI

Convert an amplitude ratio to decibels in C#

OneMath is a free, open-source deterministic math library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Decibels (dB) express a level relative to a reference. A ratio of 1 is 0 dB, not silence.

Terminal
dotnet add package RecursiveAI.OneMath

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneMath;
double ratio = 2.0; // amplitude/reference
var db = 20 * Math64.Log10(ratio);
Console.WriteLine(FormattableString
    .Invariant($"{db:F2} dB"));

The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.

Text
6.02 dB

The full program

The example takes nonnegative amplitude ratios, handles zero as silence, and rejects negative or nonfinite inputs with ArgumentOutOfRangeException instead of printing a misleading level.

C#
using RecursiveAI.OneMath;
using System.Globalization;
 
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
 
foreach (double ratio in new[] { 0.5, 1.0, 2.0, 10.0, 0.0 })
{
    double db = AmplitudeToDb(ratio);
    string level = double.IsNegativeInfinity(db) ? "-Infinity" : $"{db:F2}";
    Console.WriteLine($"{ratio:G}x amplitude: {level} dB");
}
 
static double AmplitudeToDb(double ratio)
{
    if (!double.IsFinite(ratio) || ratio < 0)
        throw new ArgumentOutOfRangeException(nameof(ratio));
    return ratio == 0 ? double.NegativeInfinity : 20 * Math64.Log10(ratio);
}
Text
0.5x amplitude: -6.02 dB
1x amplitude: 0.00 dB
2x amplitude: 6.02 dB
10x amplitude: 20.00 dB
0x amplitude: -Infinity dB

Variations

Power instead of amplitude. For a positive power ratio such as 2 W divided by 1 W, replace the ratio and db lines with these. The print line now gives 3.01 dB.

C#
double powerRatio = 2.0;
var db = 10 * Math64.Log10(powerRatio);

The short answer

For a positive amplitude ratio, use 20 * Math64.Log10(ratio). Compute the ratio by dividing a measured magnitude by a positive reference in the same units. A zero magnitude gives negative infinity. If you already have a power ratio, use 10 * Math64.Log10(ratio) instead.

What is the formula for calculating decibels from amplitude?

Divide the amplitude by its reference, take the base-ten logarithm, then multiply by 20. For example, 0.5 V / 1 V is a ratio of 0.5, which gives about −6.02 dB relative to 1 V. At the reference the ratio is 1 and the level is 0 dB; above it the level is positive. State the reference when you report a measured level, since the raw number in dB doesn’t tell the reader what you compared against. NIST’s Guide to the SI, §8.7 gives the formulas and examples of reporting the reference.

OneMath’s Math64.Log10 computes the correctly rounded base-ten logarithm of a double. That contract applies to the logarithm; the division used to form a ratio and the multiplication by 20 are separate floating-point operations. Formatting with F2 rounds only the displayed text, not the stored value.

What ratio is 20 dB?

It’s a ratio of 10 for amplitude or 100 for power. The formulas agree when the ratio of powers is the square of the amplitude ratio, as with voltage magnitudes across the same resistance. You don’t need to square the amplitude ratio before calling Log10 with the factor of 20. If your measurements are already in watts, use the factor of 10 directly or you’ll report twice the correct dB value.

Is 0 dB silence?

No. A level of 0 dB means the magnitude equals the reference. Silence has a ratio of zero. Math64.Log10(0) returns negative infinity, and the full program makes that case explicit. Keep it as a distinct value in your calculation. A display can show a label such as “silence” or choose a finite lower limit for its scale; that limit is a display choice, not what the logarithm returns. If your input is a signed sample, decide what magnitude you’re measuring before computing a ratio. A negative input to Log10 gives NaN, not a negative dB level.

All onemath recipes