Skip to content

Instantly share code, notes, and snippets.

@ast
Created July 4, 2020 15:35
Show Gist options
  • Save ast/8caf676d851541959d7e11113e728629 to your computer and use it in GitHub Desktop.
Save ast/8caf676d851541959d7e11113e728629 to your computer and use it in GitHub Desktop.
Dead simple AGC.
typedef struct {
float attack_rate;
float decay_rate;
float reference;
float max_gain;
float gain;
} agc_t;
void agc_init(agc_t *agc, float attack_rate, float decay_rate, float reference) {
agc->attack_rate = attack_rate;
agc->decay_rate = decay_rate;
agc->gain = 1.;
agc->max_gain = 65000.;
agc->reference = reference;
}
void agc_execute(agc_t *agc, const float complex *input, float complex *output, int n) {
float pow;
float diff;
for (int i = 0; i < n; i++) {
output[i] = agc->gain * input[i]; // apply gain
pow = crealf(output[i] * conjf(output[i])); // is always real
diff = agc->reference - pow;
// adjust gain
agc->gain += diff * (diff > 0 ? agc->decay_rate : agc->attack_rate);
// clamp to max gain
agc->gain = agc->gain > agc->max_gain ? agc->max_gain : agc->gain;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment