Converting between angular frequency and frequency (e.g., rad/s to Hz)
See original GitHub issueI just tried a couple of ways of converting from 'rad / s' to 'Hz' that gave me different behavior than I was expecting. Doing this conversion requires dividing by 2π, but the dimensionless_angles equivalency uses a factor of 1 instead of 2π. Here are some examples that confused me when I first tried them. The second example gets pretty close to the behavior I was expecting, except that it seems non-intuitive that 'cycle / s' cannot be converted to 'Hz'.
>>> from astropy import units as u
>>> angular_frequency = 1.0 * u.rad/u.s
>>> angular_frequency.to(u.Hz)
UnitConversionError: 'rad / s' (angular speed) and '1 / s' (frequency) are not convertible
>>> frequency = angular_frequency.to(u.cycle/u.s) # a cycle is 2π radians
<Quantity 0.15915494309189535 cycle / s>
>>> frequency.to(u.Hz)
UnitConversionError: 'cycle / s' (angular speed) and 'Hz' (frequency) are not convertible
>>> u.set_enabled_equivalencies(u.dimensionless_angles())
>>> angular_frequency = 1.0 * u.rad/u.s
>>> angular_frequency.to(u.Hz)
<Quantity 1.0 Hz>
Should units allow conversion between angular frequency and frequency by default? Would it be better to add this as another equivalency along with dimensionless_angles? Or is the status quo preferred? Allowing this conversion by default would result in more readable code for me and I think this would be more intuitive, but I don’t know if there are any adverse consequences.
Also, here’s my code for a custom equivalency that gave me the expected behavior:
>>> angfreq_to_freq = [(u.rad/u.s, u.Hz, lambda x: x/(2*pi), lambda x: 2*pi*x)]
>>> angular_frequency = 1.0 * u.rad / u.s
>>> angular_frequency.to(u.Hz, equivalencies=angfreq_to_freq)
<Quantity 0.15915494309189535 Hz>
I tried this with astropy 1.3.2 and 2.0.dev18104. Related pull requests: https://github.com/astropy/astropy/pull/1160, https://github.com/astropy/astropy/pull/1161, https://github.com/astropy/astropy/pull/2544, https://github.com/astropy/astropy/pull/2559
Many thanks!
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:9 (8 by maintainers)

Top Related StackOverflow Question
@mhvk - Sure! I’ll aim to get to that in the next couple of days, while simultaneously trying to get myself unconfused about this. You solution is very clear and explicity, so I like it and will use that approach going forward. I’ll also include it in the pull request.
I’m presently a net negative contributor (minus three characters!) so I’m happy to make a positive contribution!
Hey @namurphy, this conversion is exactly what I was looking for in the context of neural population simulations. Thanks for raising the issue.