frequency: freq Namespace Reference
Frequency types and utilities. More...
Classes | |
| class | frequency |
| A frequency value with a representation and precision. More... | |
| struct | is_frequency |
| Trait to detect frequency specializations. More... | |
| struct | is_frequency< frequency< Rep, Precision > > |
Typedefs | |
| using | millihertz = frequency< int64_t, std::milli > |
| Frequency with 0.001 Hz (millihertz) precision. | |
| using | hertz = frequency< int64_t > |
| Frequency with 1 Hz precision. | |
| using | kilohertz = frequency< int64_t, std::kilo > |
| Frequency with 1000 Hz (kilohertz) precision. | |
| using | megahertz = frequency< int64_t, std::mega > |
| Frequency with 1,000,000 Hz (megahertz) precision. | |
| using | gigahertz = frequency< int64_t, std::giga > |
| Frequency with 1,000,000,000 Hz (gigahertz) precision. | |
| using | terahertz = frequency< int64_t, std::tera > |
| Frequency with 1,000,000,000,000 Hz (terahertz) precision. | |
Frequency types and utilities.
This library provides type-safe frequency handling with support for multiple precisions (mHz, Hz, kHz, MHz, GHz, THz), following the design of std::chrono::duration.
Basic Usage
The library provides standard integer-based frequency types:
using namespace freq;
A frequency value with a representation and precision.
Frequency types and utilities.
constexpr ToFreq frequency_cast(const frequency< Rep, Precision > &f)
Converts a frequency to a different precision or representation.
User-defined literals for frequency types.
Floating-Point Frequencies
For fractional precision (e.g., musical tuning, scientific measurements), use floating-point representations:
frequency semitone_shift(T semitones) const
Returns this frequency shifted by a number of semitones.
frequency octave_shift(T octaves) const
Returns this frequency shifted by a number of octaves.
Integer vs Floating-Point
- Integer types (default): Exact arithmetic, support modulo operations, preferred for digital systems and counting applications
- Floating-point types: Fractional precision, natural for calculations involving division and musical intervals, but no modulo operations
Conversions between integer and floating-point follow the same rules as precision conversions (implicit when lossless, explicit when lossy).
◆ abs()
Returns the absolute value of a frequency.
- Template Parameters
-
Rep Representation type. Precision Precision type.
- Parameters
- Returns
- The absolute value of the frequency.
using namespace freq;
constexpr frequency< Rep, Precision > abs(const frequency< Rep, Precision > &f)
Returns the absolute value of a frequency.
Definition at line 976 of file frequency.hpp.
References frequency_cast(), and freq::frequency< Rep, Precision >::zero().
Referenced by beat().
◆ beat()
Calculates the beat frequency between two frequencies.
In acoustics and signal processing, the beat frequency is the absolute difference between two frequencies. When two sound waves of slightly different frequencies interfere, they produce a beating pattern at this frequency.
This is a convenience function equivalent to abs(f1 - f2).
- Template Parameters
-
Rep1 First frequency representation type. Precision1 First frequency precision. Rep2 Second frequency representation type. Precision2 Second frequency precision.
- Parameters
-
f1 The first frequency. f2 The second frequency.
- Returns
- The beat frequency as the absolute difference.
using namespace freq;
constexpr auto beat(const frequency< Rep1, Precision1 > &f1, const frequency< Rep2, Precision2 > &f2) -> std::common_type_t< frequency< Rep1, Precision1 >, frequency< Rep2, Precision2 > >
Calculates the beat frequency between two frequencies.
- See also
- abs(), operator-()
Definition at line 954 of file frequency.hpp.
References abs(), and frequency_cast().
◆ ceil()
Converts a frequency to the target type, rounding toward positive infinity.
This function performs a frequency conversion with ceiling rounding semantics. When converting to a coarser precision, values are rounded up.
- Template Parameters
-
ToFreq The target frequency type. Rep Source representation type. Precision Source precision.
- Parameters
-
f The frequency to convert.
- Returns
- The converted frequency, rounded toward positive infinity.
Definition at line 858 of file frequency.hpp.
References frequency_cast().
◆ floor()
Converts a frequency to the target type, rounding toward negative infinity.
This function performs a frequency conversion with floor rounding semantics. When converting to a coarser precision, values are rounded down.
- Template Parameters
-
ToFreq The target frequency type. Rep Source representation type. Precision Source precision.
- Parameters
-
f The frequency to convert.
- Returns
- The converted frequency, rounded toward negative infinity.
Definition at line 825 of file frequency.hpp.
References frequency_cast().
◆ frequency_cast()
template<typename ToFreq , typename Rep , typename Precision >
|
constexpr |
Converts a frequency to a different precision or representation.
For integer-to-integer conversions, this function uses wider intermediate types (128-bit when available) to minimize overflow risk during ratio arithmetic.
- Template Parameters
-
ToFreq The target frequency type. Rep Source representation type. Precision Source precision.
- Parameters
-
f The frequency to convert.
- Returns
- The converted frequency.
Definition at line 749 of file frequency.hpp.
References frequency_cast().
Referenced by abs(), beat(), ceil(), floor(), frequency_cast(), freq::frequency< Rep, Precision >::harmonic(), freq::frequency< Rep, Precision >::octave_shift(), freq::frequency< Rep, Precision >::octaves_from(), operator%(), operator%(), freq::frequency< Rep, Precision >::operator%=(), freq::frequency< Rep, Precision >::operator%=(), operator*(), operator*(), freq::frequency< Rep, Precision >::operator*=(), operator+(), freq::frequency< Rep, Precision >::operator+=(), operator-(), freq::frequency< Rep, Precision >::operator-=(), operator/(), operator/(), freq::frequency< Rep, Precision >::operator/=(), operator<=>(), operator==(), freq::frequency< Rep, Precision >::period(), round(), freq::frequency< Rep, Precision >::semitone_shift(), freq::frequency< Rep, Precision >::semitones_from(), freq::frequency< Rep, Precision >::subharmonic(), to_string(), to_string(), freq::frequency< Rep, Precision >::wavelength(), and freq::frequency< Rep, Precision >::wavelength().
◆ operator%() [1/2]
◆ operator%() [2/2]
◆ operator*() [1/2]
◆ operator*() [2/2]
◆ operator+()
◆ operator-()
◆ operator/() [1/2]
◆ operator/() [2/2]
◆ operator<=>()
◆ operator==()
◆ round()
Converts a frequency to the target type, rounding to nearest (ties to even).
This function performs a frequency conversion with round-to-nearest rounding semantics. When converting to a coarser precision, values are rounded to the nearest representable value, with ties rounded to even.
- Template Parameters
-
ToFreq The target frequency type. Rep Source representation type. Precision Source precision.
- Parameters
-
f The frequency to convert.
- Returns
- The converted frequency, rounded to nearest.
Definition at line 894 of file frequency.hpp.
References frequency_cast().
◆ to_string() [1/2]
|
inline |
◆ to_string() [2/2]
|
inline |