Skip to content

Commit

Permalink
add agc (3)
Browse files Browse the repository at this point in the history
  • Loading branch information
timva1 committed Nov 5, 2024
1 parent a54882b commit aa16f6b
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/gain_control/agc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use num::Complex;
use libm::sqrtf;

/// # Automatic Gain Controller
/// automatic gain control using basic algorithm found here:
Expand Down Expand Up @@ -48,7 +49,8 @@ impl<const BLOCK_SIZE: usize> AGC<BLOCK_SIZE> {
for n in 0..BLOCK_SIZE {
output_signal[n] = samples[n].scale(self.gain);

let error = self.target - self.gain * samples[n].norm(); // can use approximation |z[n]| = sqrt(z_i^2 + z_q^2) approx. = |z_i| + |z_q|
let norm = sqrtf(samples[n].re * samples[n].re + samples[n].im * samples[n].im); // can use approximation |z[n]| = sqrt(z_i^2 + z_q^2) approx. = |z_i| + |z_q|
let error = self.target - self.gain * norm;
self.gain += error * self.step_size;
}
}
Expand Down

0 comments on commit aa16f6b

Please sign in to comment.