kdavies4 / natu

Natural units in Python
http://kdavies4.github.io/natu/
Other
33 stars 5 forks source link

Handling of decibels suggestions #26

Open endolith opened 9 years ago

endolith commented 9 years ago

I don't understand how the dB unit works. It doesn't produce the right result for voltage ratios:

>>> float((2*V / (1*V))/dB)
3.0102999566398116

It seems to be a power ratio only? So 3*dB is 2 W/W?

>>> float(3*dB)
1.9952623149688797
>>> 10 == 10*dB
True
>>> 100 == 20*dB
True

But this can't be combined with a watt, whether added or multiplied:

>>> 2*W + 3*dB
Traceback (most recent call last):
...
AssertionError: The quantities must have the same dimension.
>>> 2*W * 3*dB
Traceback (most recent call last):
...
AssertionError: The argument to the lambda unit must be dimensionless.

It doesn't add to itself as I would expect, either:

>>> 20*dB + 40*dB
40.0432 dB

Here are some examples of how I would want dB-related units to be handled, ideally:

>>> 20*dB + 40*dB
60 dB

>>> gain = 2*V / 1*V
>>> gain.display_unit = 'dB'
>>> gain
6.02 dB

>>> gain = 2*W / 1*W
>>> gain.display_unit = 'dB'
>>> gain
3.01 dB

>>> 10*V == 20*dBV
True

>>> 100*W == 20*dBW
True

>>> 20*dBm == 100*mW
True

>>> voltage = 2*V
>>> voltage.display_unit = 'dBV'
>>> voltage
6.02 dBV

>>> power = 2*W
>>> power.display_unit = 'dBW'
>>> power
3.01 dBW

>>> pressure = 40*uPa
>>> pressure.display_unit = 'dBSPL'
>>> pressure
6.02 dBSPL

>>> 10*dBW + 20*dB
30 dBW

>>> 10*dBV + 20*dB
30 dBV

>>> 20*dB + 10*dBV
Either 30 dBV or an Exception; I'm not sure

>>> 10*dBV + 20*dB - 10*dB
20 dBV

>>> 30*dBV - 20*dBV
10 dB

Since absolute decibel units would be equal to the non-decibel units, these should probably work too?

>>> 1*V + 20*dB
10 V

>>> 1*W + 20*dB
100 W

>>> 10*Pa + 20*dB
100 Pa

>>> 2*W - 3*dB
1.002 W

I'm not sure about these:

>>> 40*dBV + 20*dBV
Either incompatible units Exception or interpret it as 100*V * 10*V = 1000 V2

>>> 40*dBV + 20*dBW
Either incompatible units Exception or interpret it as 100*V * 100*W = 10000 V*W

>>> 10 + 20*dB
Exception: ambiguous? could be voltage or power ratio

Might make sense to treat other logarithmic units similarly?

>>> 2000*Hz + 1*octave
4000 Hz

>>> 200*Hz + 1*decade
2000 Hz

>>> 1200*cents == 1*octave
True
SweRavn commented 6 years ago

dB does not have any dimension. dB has nothing to do with W, thus you cannot expect handling of dB wrt V or wrt W to work as you think. The dB-scale it self has no awareness of what it is dB'ing. The quantity cannot have any dimension. If you are interested in relative to power, one usually talks about dBm, but as far as I can see, that is not supported by natu at this point. And I guess it will be very hard to get that supported also.

Your example 20dB + 40dB also behaves as expected I think. The calculations works in the way that units are converted to linear scale and then added and then converted back to log scale. You are looking for (20+40)*dB, that will give you your expected result of 60 dB.

endolith commented 6 years ago

dB has nothing to do with W, thus you cannot expect handling of dB wrt V or wrt W to work as you think.

If you have a 1 V signal and you put it through an amplifier with a gain of +20 dB, then what is the output voltage?

SweRavn commented 6 years ago

It depends on the impedance. 20 dB gain means the power out will be 100 times greater than the power into the amplifier. But I can not say anything about the input power if you only specify the voltage. I also cannot say anything about the output voltage. If the input and output impedance are the same, then 20 dB gain means sqrt(100)*1V= 10 V output voltage.

endolith commented 6 years ago

@SweRavn The input impedances of each stage are infinite, so zero power is consumed anywhere, but decibels are still used in this context: https://en.wikipedia.org/wiki/Decibel#Field_quantities_and_root-power_quantities https://e2e.ti.com/blogs_/b/analogwire/archive/2013/07/10/how-to-determine-power-gain-and-voltage-gain-in-rf-systems