DSPark 1.8.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
RationalKnee.h
1// DSPark - Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi - MIT License
3
4#pragma once
5
14#include "../DspMath.h"
15#include <cmath>
16
17namespace dspark::detail
18{
20template <FloatType T>
21[[nodiscard]] inline T rationalKneeMagnitude(T magnitude, T knee, T ceiling) noexcept
22{
23 if (magnitude <= knee)
24 return magnitude;
25 const T excess = magnitude - knee;
26 const T range = ceiling - knee;
27 return knee + (range * excess) / (excess + range);
28}
29
31[[nodiscard]] inline double rationalKneeShape(double input, double positiveKnee,
32 double negativeKnee) noexcept
33{
34 const double knee = input >= 0.0 ? positiveKnee : negativeKnee;
35 return std::copysign(rationalKneeMagnitude(std::abs(input), knee, 1.0), input);
36}
37
39[[nodiscard]] inline double rationalKneeIntegral(double input, double positiveKnee,
40 double negativeKnee) noexcept
41{
42 const double magnitude = std::abs(input);
43 const double knee = input >= 0.0 ? positiveKnee : negativeKnee;
44 if (magnitude <= knee)
45 return 0.5 * magnitude * magnitude;
46 const double range = 1.0 - knee;
47 const double excess = magnitude - knee;
48 return 0.5 * knee * knee + excess - range * range * std::log((excess + range) / range);
49}
50
51} // namespace dspark::detail
T rationalKneeMagnitude(T magnitude, T knee, T ceiling) noexcept
Positive magnitude of the original Clipper rational soft knee.
double rationalKneeShape(double input, double positiveKnee, double negativeKnee) noexcept
Unit-ceiling curve with independent positive and negative knees.
double rationalKneeIntegral(double input, double positiveKnee, double negativeKnee) noexcept
Antiderivative of the unit-ceiling curve, with F(0) = 0.