Add decibel gain and absolute level types#1
Merged
Merged
Conversation
Adds an opt-in <electro/decibel> header with two types, modeled on the
duration/time_point distinction:
- gain: a dimensionless logarithmic ratio (dB), an ordinary quantity
unit that adds, subtracts, negates, and scales.
- level: an absolute point on a logarithmic scale (dBm, dBW, dBV, dBmV,
dBµV), parameterized by a reference tag carrying the linear unit, the
0 dB reference precision, and the 10*log10 vs 20*log10 factor.
The affine algebra is enforced at compile time: level +/- gain -> level
and level - level -> gain, while level + level, level * scalar, and
mixing references are ill-formed. Combining uncorrelated signals is the
explicit add_powers() (10 dBm + 10 dBm = 13.01 dBm), restricted to
power references.
Crossing between the logarithmic and linear domains is via explicit
named functions (to_linear, to_level, power_ratio, amplitude_ratio,
power_gain, amplitude_gain), which are the only non-constexpr parts of
the header (std::log10/std::pow are not constexpr until C++26).
Decibels opt out of the SI-prefix suffix machinery; precisions render
as fixed-point decimals ("13.01dBm", never "1301cdBm"). Literals cover
_dB, _cdB, _dBm, _cdBm, _dBW, _dBV, _dBmV, _dBuV, and _dBµV.
The core electro.hpp header is unchanged.
The vcpkg port SHA512 is reset to the placeholder; it can only be set once the v0.2.0 release tarball is published, as was done for v0.1.0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an opt-in
<electro/decibel>header with two types, modeled on theduration/time_pointdistinction:gain— a dimensionless logarithmic ratio (dB). An ordinaryquantityunit: adds, subtracts, negates, scales.level— an absolute point on a logarithmic scale (dBm, dBW, dBV, dBmV, dBµV), parameterized by a reference tag that carries the linear unit, the 0 dB reference precision, the 10·log10 vs 20·log10 factor, and the display symbol.The affine algebra is enforced at compile time:
Crossing into the linear domain is via explicit named functions —
to_linear(30_dBm)is 1000.0 mW,to_level<dbm>(milliwatts(1))is 0 dBm — the only non-constexpr parts of the header (std::log10/std::powaren't constexpr until C++26). Combining uncorrelated signals is the explicitadd_powers()(10 dBm ⊕ 10 dBm = 13.01 dBm), restricted to power references since summing linear amplitudes on a field reference would model coherent addition instead.electro.hppis unchanged; decibels are opt-in so<cmath>and the non-constexpr bridge stay out of the core header.Version
Bumped to 0.2.0. The vcpkg port
SHA512is reset to the placeholder — it can only be set once the v0.2.0 release tarball is published, mirroring the two-step flow used for v0.1.0.Testing
static_assertcompile-time checks and concept-based ill-formed-operation tests with positive controls._dBµVliteral on MSVC (needs the already-required/utf-8).