From a2b38be6cfb447b215b62f47018cf7b745f876c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Hamal=20Dvo=C5=99=C3=A1k?= Date: Sat, 24 Feb 2024 23:40:28 +0100 Subject: [PATCH] Replace constant gain with AGC --- src/main.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/main.c b/src/main.c index b210394..a905c75 100644 --- a/src/main.c +++ b/src/main.c @@ -550,12 +550,7 @@ static void rf_rx(void) int lpQa3 = 0; int64_t dcI = 0, dcQ = 0; - - /* - * CLK / BW for oversampling, but 1/2 for the -1/0/0/1 PIO outcomes. - * Signal can get outside due to DC offset compensation. - */ - const int amp_max = CLK_SYS_HZ / 2 / BANDWIDTH * DECIMATION; + int64_t agc = 127; while (true) { if (multicore_fifo_rvalid()) { @@ -639,20 +634,19 @@ static void rf_rx(void) dI = ((dI << 19) - dcI) >> 19; dQ = ((dQ << 19) - dcQ) >> 19; - if (dI > amp_max) - dI = amp_max; + dI <<= 32; + dQ <<= 32; - if (dI < -amp_max) - dI = -amp_max; + agc -= (1 << 12) / DECIMATION * BANDWIDTH; - if (dQ > amp_max) - dQ = amp_max; + if (llabs(dI) > agc) + agc = llabs(dI); - if (dQ < -amp_max) - dQ = -amp_max; + if (llabs(dQ) > agc) + agc = llabs(dQ); - *blockptr++ = 127 * dI / amp_max; - *blockptr++ = 127 * dQ / amp_max; + *blockptr++ = 127 * dI / agc; + *blockptr++ = 127 * dQ / agc; } if (!queue_try_add(&iq_queue, block)) {