sharkdp / insect

High precision scientific calculator with support for physical units
https://numbat.dev/
MIT License
3.17k stars 126 forks source link

Practical use of decibel (dBW, dBV) #279

Open fxkr opened 2 years ago

fxkr commented 2 years ago

Thanks for writing insect; I like it a lot!

Feature request

insect claims to support bel and decibel (as per the README and #67). However it doesn't seem to be able to do actually do much useful with them.

Would it be possible to add support for the following use cases?

1. Converting between decibel values and ratios

   > 6 dB -> scalar

Challenge: should it return 4 (power ratio) or 2 (amplitude ratio)? Maybe it would need to be made explicit:

   > 6 dB power -> scalar

Expected output: 4.

If there is a default, it needs to be power.

And back:

   > 10 -> decibel power

Ideally it will automatically infer power/amplitude where possible. Probably complicated due to the fact that we probably lose the information whether we're dealing with power quantities or root power quantities before we even get to the scalar to decibel conversion.

   > 100 mW / 1 mW -> decibel

Expected answer: 20 dB

2. Applying decibel values to quantities

   > 20 decibel mV -> V

   > 10dB * 1mW -> W

   > 10 dBmW -> W

Expected answers: 0.01V and 0.01W and 0.01W

Note again that power and root-power quantities need to be treated differently.

Reverse direction:

   > 10mW -> dBmW
   > 10mV -> dBV

3. Converting between decibels of power and decibels of amplitude

   > 10 decibel power -> decibel amplitude

Expected answer: 20 dB amplitute

Technical context (for anyone not familiar with decibels)

Decibel (and bel, which is rarely used) are usually used as a way of expressing ratios between power quantities (e.g. watts) or root power quantities (e.g. volts). For example, 10dB means a factor 10 increase in power; 20dB means a factor of 10 (!) in increase in e.g. voltage.

This notation is pervasive in many fields, such as RF engineering (both because it turns multiplications into additions (e.g. for loss calculations), and because it allows easier representation of extremely wide ranges of values (femtowatts ... megawatts).

As a special case, decibel is also often used to express power quantities and root power quantities, by specifying a decibel value (e.g. 15dB) with reference to a convenient base value (such as 1mW).

15dBmV usually means "15 dB relative to 1 mV" = (10^(15 / 20)) * 1mV = 5.6mV (note divisor 20 because its an amplitude / root power quantity)

15dBmW usually means "15 dB relative to 1 mW" = (10^(15 / 10)) * 1mW = 31.62mW (note divisor 10 because it's a power quantity)

sharkdp commented 2 years ago

Thank you very much for this detailed feature request. I currently don't have the time to implement this or to think about the detailed design. I am afraid that it might pose some challenges indeed.

What you can definitely do is to define functions that would do useful conversions for you. Like

> to_mV(x) = 10^(x/20decibel) * 1mV

> to_mV(15decibel)

   = 5.62341 mV

But I realize that this is not super convenient.