Qalculate / qalculate-gtk

Qalculate! GTK+ UI
https://qalculate.github.io/
GNU General Public License v2.0
969 stars 72 forks source link

Addition of log-based units (dBm) #73

Open Fondor1 opened 6 years ago

Fondor1 commented 6 years ago

There does not seem to be a way to establish/create a unit based on a logarithmic scale, for example dBm.

Can this unit be added as a builtin, or if not, allow users to define units via an equation?

hanna-kn commented 6 years ago

It is possible for users to add such units, even though it is not well documented. For non-linear relations \x should be used for the factor and \y for the exponent in the expression entered in "relation" and "inverse relation". Temperature units are defined in this way.

dBm, with base unit W, should be specified with relation 10*log(\x,10), and inverse relation 10^((\x-30)/10)). Unfortunately this will cause a segfault in the released versions (it has been fixed in git).

Fondor1 commented 6 years ago

Thank you for the clarification. I expected the ability to define exponential relations existed but it was unclear to me that the interface was exposed to users in this fashion.

I'm using Qalculate on both Windows 7 and Ubuntu 18.04. I will work on compiling the Ubuntu version from source to try it out. Thank you for the rapid response.

Gythuby commented 5 years ago

The support for Dezibels is nice but risky, because the calculations have to be implemented really complete and fully correct. To my humble understanding this is currently (Version 3.3.0) not the case. Only very simple Dezibel calculations are correct, but even not the simple Dezibel-additions or subtractions. Example:

Correct: 20 dBm to mW -> = 100 mW
Correct: 40 dBm to W -> = 10 W
Correct: 200 mW to dBm -> 23.01 dBm
**Incorrect**:  20 dBm + 20 dBm -> = 40 dBm 

The correct result for addition (20+20) dBm is 23.01 dBm instead. Doubling/halfing of power gives always +/- 3 dB and doubling/halfing of voltage gives +/- 6 dB.

Gythuby commented 5 years ago

With some other unit conversions you get the correct warning popup windows within Qualculate:

Calculations involving conversion of units without proportional linear relationship (e.g. with multiple temperature units), might give unexpected results and is not recommended.

QED!

hanna-kn commented 5 years ago

I have implemented a simple solution. Log-based units will now be converted before any other calculations (e.g. 20 dBm + 20 dBm = 0.1 W + 0.1 W = 0.2 W = 23.01 dBm).

Fondor1 commented 5 years ago

Another related comment then: when starting with a power in dBm (20dBm) and scaling/adding/subtracting dB (e.g. 10 dB), one receives the above warning about unexpected results. In this case, the calculation when converted to linear should be 20 dBm + 10 dB = 0.1W * 10 (unitless) = 1W = 30 dBm. Addition/subtraction in log-based units will be multiplication/division in linear. My use case is most often simple conversion, followed closely by this scaling calculation, then lastly by adding or subtracting (linearly) power with units in dBm.

hanna-kn commented 5 years ago

20 dBm + 10 dB will return 10 dB + 0.1 W (with warning that log-based units are converted before calculation). Bel is treated as any other base unit and is not compatible with power units.

20 dBm + 10 dB = 0.1 W * 10 seems to imply 10 dB + 10 dB = 20 dB, as it currently is in Qalculate!, while Wolfram Alpha returns 13.01 dB.

My impression is that their are no agreed conventions for the interpretation of expressions involving arithmetic with decibel and other log-based units.

Gythuby commented 5 years ago

Thank you Hanna,

My impression is that their are no agreed conventions for the interpretation of expressions involving arithmetic with decibel and other log-based units

The conventions are just math! With this they are very clearly defined! But dBs are difficult and nearly impossible to be implemented correctly and clearly for the user in any general math tool. And the tool has to inform about all wrong inputs like "10 dBµV + 10 dBm = ?". And with dBs based on Voltage the impedance (50, 75, ... Ohm) is required. A neverending story...

For Android I can recommend the free and powerful App "dB Calculator" from Rohde & Schwarz.

endolith commented 5 years ago

20 dBm + 10 dB

The correct answer is 30 dBm. You have taken an absolute level and increased it by a relative level.

20 dBm + 20 dBm

This is meaningless, and should not return anything.

You cannot sum them "in the decibel domain", though. That would be equivalent to multiplication. In this case the "sum" is of voltages, not decibels. Since the answer varies with the type of signal, these would require functions to combine them, not addition.

20 dBm - 20 dBm

The correct answer would be 0 dB.

In other words, you can subtract absolute units to get a relative difference, and you can add a relative difference to an absolute unit to get another absolute unit, but you cannot add two absolute units together, that doesn't make sense.

Gythuby commented 5 years ago

Thank you very much Endolith,

yes, the correlation of signals is something on top of dBx additions/subtractions.

I have to do such calculations on the spectrum analyzer with weak sinusoidal signals which are very near to uncorrelated noise floor. The real level of sinus signals is a little bit lower than the measured signal, because of the noise floor. The corresponding calculation was a nice training for my colleagues and students.

Gythuby commented 5 years ago

Really "nice" are calculations with densities like dBm/Hz or dBµv/sqrt(Hz). So you need to know correlation, impedance, bandwidth... in order to do it right.

hanna-kn commented 5 years ago

This honestly very confusing.

It would probably be wise to first implement simple decibel arithmetic correctly (if possible). I will definitely add a warning which advice the user against calculations, other than direct conversions, with logarithmic ratio units.

Is my impression that Wolfram Alpha often handles decibel and related units oddly (e.g. 10 dB - 1 dB equals 10.33 dB, 10 dB * 2 = 20 dB, and 20 dBm - 1 dBm = 20.03 dBm) correct?

Confusingly "25 dB = 10 dB + 10 dB + 3 dB + 1 dB + 1 dB", and similar, appears on Wikipedia and elsewhere.

endolith commented 5 years ago

10 dB - 1 dB equals 10.33 dB

That makes no sense, it should just be 9 dB.

For example, if you have an amplifier with a gain of 20 dB, followed by an attenuator with a gain of -6 dB, the total gain is 20 dB - 6 dB = (20-6) dB = 14 dB.

10 dB * 2 = 20 dB

I guess that makes sense? It's equivalent to 10 dB + 10 dB? Like, "two 10 dB amplifiers in series have a total gain of 20 dB".

20 dBm - 1 dBm = 20.03 dBm

No that doesn't make sense to me. 20 dBm - 1 dBm should be 19 dB, the difference between the two levels. You should not be able to get another absolute unit out of that. They're converting it back to power levels and doing the difference there or something.

25 dB = 10 dB + 10 dB + 3 dB + 1 dB + 1 dB

That makes sense, you can add relative differences to get other relative differences.

hanna-kn commented 5 years ago

So I should ignore the way various decibel calculators (including Wolfram Alpha in some cases) handles this (using 10*log10(10^(x₁/10)+10^(x₂/10), ...) for addition) and preserve the current state?

20 dBm - 1 dBm = 20.03 dBm is most likely the result of 20 dBm - 1 dBm = 20 dBm + (- 1 dBm) ≈ 100 mW + 0.8 mW ≈ 20.03 dBm. I think it makes more sense that if 19.94 dBm + 1 dBm ≈ 20 dBm, then 20 dBm - 1 dBm ≈ 100 mW - 1.26 mW ≈ 19.94 dBm, but I guess it is not that simple.

endolith commented 5 years ago

I think that would make the most sense, yes. When working with dB units, addition is really multiplication of the underlying linear values.

Now, when you add decibels (dB, dBm,...), because of their logarithmic nature, you are actually multiplying the original figures together. So, if you add a gain of 3 dB to a power of 10 dBm, you obtain 13 dBm. But what you really did, is multiplying a factor of 2 with a power of 10 mW obtaining 20 mW, which is 13 dBm!

So far so good, adding is much easier to do in your head than multiplying, and this makes decibels so handy. But there is a problem: since adding decibels together correspond to multiplying the original factors, how can one add (combine) the power of two signals? Well, you can't. You can't add dBm to dBm.

http://www.giangrandi.ch/electronics/anttool/decibel.shtml

There could be functions for combining different dB_ levels in different ways? One for coherent signals and one for incoherent?

NYKevin commented 3 years ago

In the SRE business, we have a logarithmic unit called a "nine." "Three nines" formally means 99.9% (or 1 - 10^-3), a dimensionless figure. But in practice, its use is rather more specific than that. Here are some plausible interpretations of "We run a three nines service":

  1. 99.9% of all queries are served successfully. Equivalently, there is one error for every 1000 queries.
  2. 99.9% of the time, the service is up.

In particular, interpretation 1 is not dimensionless; it has units error / query. So I figured I could define two base units, one for errors and one for queries, an unnamed derived unit of error / query, and then define the nine as a derived logarithmic unit of that. I gave the relation 10 ^ −\x and inverse relation −log(\x, 10), and it almost works...

I'm quite happy to be told "you're holding it wrong," but I would appreciate more explicit documentation about how logarithmic units are expected to behave in this context.

hanna-kn commented 3 years ago

3 nines times 1000 query / s gives 300 error / s, which is wrong (it should be 1 error / s). It's also inconsistent with the first bullet, because Qalculate! said that 3 nines is equal to 0.001 error / query, so I would expect it to use that figure here.

The issue probably is that Qalculate simplifies 3 nines times 1000 to 3 × 1000 × nines (3000 nines) before conversion. I will fix this.