---
title: "Convert an amplitude ratio to decibels in C#"
url: https://recursiveai.org/onemath/cookbook/convert-amplitude-ratio-to-decibels/
description: "Convert an amplitude ratio to decibels in C# with OneMath’s deterministic Log10. Use the right formula for amplitude or power and handle silence."
version: "1.0.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneMath) · [GitHub](https://github.com/recursive-ai-oss/onemath-csharp) · [Docs](https://recursiveai.org/onemath/docs/) · [Download example (.zip)](https://recursiveai.org/onemath/cookbook/convert-amplitude-ratio-to-decibels/convert-amplitude-ratio-to-decibels.zip)

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

```shell
dotnet add package RecursiveAI.OneMath
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

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

[The example](https://recursiveai.org/onemath/cookbook/convert-amplitude-ratio-to-decibels/convert-amplitude-ratio-to-decibels.zip) 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.

```csharp
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`.

```csharp
double powerRatio = 2.0;
var db = 10 * Math64.Log10(powerRatio);
```

## Related recipes

- [Calculate deterministic exponentials and logarithms](https://recursiveai.org/onemath/cookbook/deterministic-exp-and-log/)
- [Calculate a deterministic base-two logarithm](https://recursiveai.org/onemath/cookbook/calculate-deterministic-base-two-logarithm/)

---

## 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](https://www.nist.gov/pml/special-publication-811/nist-guide-si-chapter-8) gives the formulas and examples of reporting the reference.

[OneMath’s `Math64.Log10`](https://recursiveai.org/onemath/docs/functions/) 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.
