Units
Overview
The Q DSP library processes audio signals by applying various audio processors to sample inputs. These processors typically operate with floating-point input samples in the typical -1.0 to 1.0 range. However, the values are not restricted to sampled signals. For instance, signal envelopes are best represented as decibels processed in the logarithmic domain. Consequently, dynamic-range processors such as compressors and expanders accept decibel as input and return decibel as output.
The Q DSP library is typeful and typesafe, which means that each data type is explicitly defined and enforced. This is crucial because it avoids the potentially catastrophic errors that could result from mismatching values of different types. For example, if all values were just raw floating point types, it would be possible to accidentally mix up the frequency and decibel values. Mistakenly interchanging frequency and decibel values could result in erroneous processing of the audio signal and will lead to undefined results.
This document aims to provide comprehensive documentation of the fundamental data types used by the Q DSP library. These basic units are utilized consistently throughout the library and serve as the basis for quantifying specific parameters, such as decibels (dB) for signal level and hertz (Hz) for frequency.
Expressions
Notation
U
-
Unit
type. T
-
Underlying arithmetic value type.
v
-
Arithmetic
value. a
-
Instance of a type that conforms to
Unit
. b
-
Object with the same
unit_type
asa
. A
-
Type of object
a
. B
-
Type of object
b
.
Arithmetic Concept
The arithmetic value concept is documented in Basic Concepts and declared in this C++ concept as:
namespace cycfi::q::concepts
{
template <typename T>
concept Arithmetic = std::integral<T> || std::floating_point<T>;
}
Type Definitions
Expression | Semantics | Type |
---|---|---|
|
Get the underlying arithmetic value type. |
|
|
Get the unique unit type. Each Unit type
has its own unique unit type to distinguish
from differnt |
unspecified |
SameUnit Concept
Compatible unit types A
and B
are allowed for certain expressions as defined below. These types conform to the SameUnit
concept declared below:
namespace cycfi::q::concepts
{
template <typename A, typename B>
concept SameUnit = std::same_as<typename A::unit_type, typename B::unit_type>;
}
Basically, the declaration states that unit types A
and B
are the same if they have the same unit_type
.
Constructors and Assignment
Expression | Semantics |
---|---|
|
Construct a |
|
Copy construct from |
|
Assign |
C++ brace initialization may also be used. |
Access
Expression | Semantics | Return Type |
---|---|---|
|
Direct access to the underlying arithmetic value representation. |
|
Comparison
Expression | Semantics | Return Type |
---|---|---|
|
Equality. |
|
|
Equality with an arithmetic value. |
|
|
Equality with an arithmetic value. |
|
|
Non-Equality. |
|
|
Non-Equality with an arithmetic value. |
|
|
Non-Equality with aan arithmetic value. |
|
|
Less than. |
|
|
Less than with an arithmetic value. |
|
|
Less than with an arithmetic value. |
|
|
Less than equal. |
|
|
Less than equal with an arithmetic value. |
|
|
Less than equal with an arithmetic value. |
|
|
Greater than. |
|
|
Greater than with an arithmetic value. |
|
|
Greater than with an arithmetic value. |
|
|
Greater than equal. |
|
|
Greater than equal with an arithmetic value. |
|
|
Greater than equal with an arithmetic value. |
|
Arithmetic
Expression | Semantics | Return Type |
---|---|---|
|
Positive. |
|
|
Negative. |
|
|
Add assign. |
|
|
Add assign with an arithmetic value. |
|
|
Subtract assign. |
|
|
Subtract assign with an arithmetic value. |
|
|
Multiply assign with an arithmetic value. |
|
|
Divide assign with an arithmetic value. |
|
|
Addition. |
|
|
Addition with an arithmetic value. |
|
|
Addition with an arithmetic value. |
|
|
Subtraction. |
|
|
Subtraction with an arithmetic value. |
|
|
Subtraction with an arithmetic value. |
|
|
Multiplication with an arithmetic value. |
|
|
Multiplication with an arithmetic value. |
|
|
Division. |
|
|
Division with an arithmetic value. |
|
Unit Promotion
On binary operations a + b
and a - b
, where a
and b
conform to the SameUnit
concept (see above), the resuling type will be whichever has the value_type
of decltype(a.ref + b.rep)
, else if both operands are promoted, then whichever has the larger value_type
will be chosen.
For example, if a.rep is double
and b.rep is int
, the result will be the unit type of a
.