Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipe request for logarithmic units #47

Open
anojo opened this issue Mar 9, 2021 · 6 comments
Open

Recipe request for logarithmic units #47

anojo opened this issue Mar 9, 2021 · 6 comments

Comments

@anojo
Copy link

anojo commented Mar 9, 2021

plot([2u"dB", 3u"dB"]) doesn't work for me. Quick tests like this suggest that logarithmic units aren't working, for either axis. Is it possible/easy to add a recipe for these?

@jw3126
Copy link
Owner

jw3126 commented Mar 10, 2021

Interesting, thanks for reporting! The issue is, that we dispatch on Unitful.Quantity, however stuff measured in log units, apparently is not Quantity:

julia> using Unitful

julia> supertypes(typeof(2u"dB"))
(Gain{Unitful.LogInfo{:Decibel, 10, 10}, :?, Int64}, Unitful.LogScaled{Unitful.LogInfo{:Decibel, 10, 10}}, Number, Any)

julia> supertypes(typeof(2u"m"))
(Quantity{Int64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}, Unitful.AbstractQuantity{Int64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}, Number, Any)

This can probably be fixed by replacing Quantity by something like Union{AbstractQuantity, LogScaled} in this package. A PR would be welcome @anojo !

@briochemc
Copy link
Collaborator

I'm actually surprised that there is no parent abstract type for those:

julia> typejoin(typeof(1u"m"), typeof(1u"dB"))
Number

And I'm not sure there's a reason for that. Maybe worth asking the Unitful.jl peeps?

Otherwise, I think @jw3126's suggestion is the best and quickest solution, unless I'm missing something. Maybe we could add it a constant

const QuantityOrLogScaled = Union{AbstractQuantity, LogScaled}

and then dispatch on that?

(and also maybe it's worth upstreaming that up into Unitful?)


Also, side note: I realize we use Quantity throughout and not AbstractQuantity. Maybe we should loosen that up for future compatibility?

@anojo
Copy link
Author

anojo commented Mar 10, 2021

There's also that the function unit doesn't work (there's logunit instead), and that ustrip(u"dB", 2u"dB") doesn't seem to work either (but ustrip(2u"dB") does).

@briochemc
Copy link
Collaborator

OK so it looks like we need to think this through a bit more (and it will require a bit more work)!

@gustaphe
Copy link
Collaborator

Just a comment for this if you decide to go for it, a common construction is 1u"dB/m" which isa Quantity{Gain{Unitful.LogInfo{:Decibel, 10, 10}, :?, Int64}, 𝐋^-1, Unitful.FreeUnits{(m^-1), 𝐋^-1, nothing}}, so there is even more to think about.

@gustaphe
Copy link
Collaborator

gustaphe commented Aug 4, 2021

I looked into this a little, and my conclusions so far:

  • x::Level == 3u"dBm" can simply be plotted logarithmically using linear(x).
  • x::Gain == 3u"dB" can slightly less simply be plotted logarithmically using uconvertp(NoUnits, x) (assuming power logarithm rather than root power logarithm here, from my experience that's pretty uncontroversial, but should maybe be a keyword argument).
  • x::Quantity{<:Gain} == 3u"dB/m" is really tricky. It will rarely make sense to for instance convert this to a "linear" regime, as 0.01 /m ≠ -20 dB/m. So what we would want is to be able to save the "unit" dB/m on the axis, but we would then have to either feature request such a unit-like object from Unitful or roll our own, because in the Quantity object the Gain and the Unit is stored separately. There's a Unitful.MixedUnits type, but I haven't quite figured out how that enters into it.

https://github.com/gustaphe/UnitfulRecipes.jl/blob/logarithmic/src/logarithmic.jl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants