FastLED 3.9.15
Loading...
Searching...
No Matches

◆ updatePIController()

float fl::audio::AutoGain::updatePIController ( float targetGain,
float dt )
private

Update PI controller toward target gain.

Parameters
targetGainDesired gain
dtTime step in seconds
Returns
Smoothed gain output

Definition at line 159 of file auto_gain.cpp.hpp.

159 {
160 const float error = targetGain - mLastGain;
161
162 // Bug 4 fix: Use absolute error threshold when gain is small to avoid
163 // huge errorRatio from dividing by tiny mLastGain
164 const bool largeError = (mLastGain > 0.1f)
165 ? (fl::abs(error) / mLastGain > 0.2f) // Relative: >20% of current gain
166 : (fl::abs(error) > 0.1f); // Absolute: >0.1 when gain is tiny
167 const float tau = largeError ? mGainFollowFastTau : mGainFollowSlowTau;
168
169 // Proportional term
170 const float pTerm = mKp * error;
171
172 // Bug 1 fix: PI target is where we WANT gain to be (not mLastGain + delta + delta)
173 // targetGain + pTerm + integrator = the desired gain level
174 const float piTarget = targetGain + pTerm + mIntegrator;
175
176 // Smooth with exponential filter using adaptive tau
177 float alpha = 1.0f;
178 if (tau > 0.0f && dt > 0.0f) {
179 alpha = 1.0f - fl::exp(-dt / tau);
180 }
181
182 const float unclamped = mLastGain + alpha * (piTarget - mLastGain);
183
184 // Bug 2 fix: Back-calculation anti-windup — only accumulate integrator
185 // when output is NOT clamped. Decay integrator when saturated (WLED: *= 0.91)
186 const bool saturated = (unclamped > mConfig.maxGain) || (unclamped < mConfig.minGain);
187 if (saturated) {
188 mIntegrator *= 0.91f;
189 } else {
190 mIntegrator += mKi * error * dt;
191 }
192 // Clamp integrator magnitude to prevent runaway in pathological cases
193 const float maxIntegral = 4.0f * mConfig.maxGain;
194 mIntegrator = fl::max(-maxIntegral, fl::min(maxIntegral, mIntegrator));
195
196 return unclamped;
197}
float mLastGain
Last smoothed gain output.
Definition auto_gain.h:158
float mIntegrator
PI integrator state.
Definition auto_gain.h:155
AutoGainConfig mConfig
Definition auto_gain.h:140
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NOEXCEPT
Definition math.h:71
constexpr common_type_t< T, U > max(T a, U b) FL_NOEXCEPT
Definition math.h:75
enable_if< is_fixed_point< T >::value, T >::type exp(T x) FL_NOEXCEPT
constexpr enable_if< is_fixed_point< T >::value, T >::type abs(T x) FL_NOEXCEPT

References fl::abs(), fl::exp(), fl::max(), mConfig, mGainFollowFastTau, mGainFollowSlowTau, fl::min(), mIntegrator, mKi, mKp, and mLastGain.

Referenced by process().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: