From 7be4ddf30d29c68cd33ef350ff238602e96c20ed Mon Sep 17 00:00:00 2001 From: Matthias Wirth Date: Sun, 3 Nov 2024 19:16:13 +0100 Subject: [PATCH] autogain: faster startup, faster reaction to very loud signals --- readsb.c | 29 ++++++++++++++++++----------- readsb.h | 2 +- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/readsb.c b/readsb.c index b885451f..fba354ae 100644 --- a/readsb.c +++ b/readsb.c @@ -788,15 +788,18 @@ static void gainStatistics(struct mag_buf *buf) { // 29 gain values for typical rtl-sdr // allow startup to sweep entire range quickly, almost half it for double steps - int starting = mono_milli_seconds() - Modes.gainStartupTime < (29 / 1.5 * interval) * SECONDS; int noiseLow = noiseLowPercent > 5; // too many samples < noiseLowThreshold int noiseHigh = noiseHighPercent < 1; // too few samples < noiseHighThreshold int loud = loudEvents > 1; + int veryLoud = loudEvents > 5; if (loud || noiseHigh) { Modes.lowerGain = 1; + if (veryLoud && !Modes.gainStartup) { + Modes.lowerGain = 2; + } } else if (noiseLow) { - if (starting || slowRise > riseTime / interval) { + if (Modes.gainStartup || slowRise > riseTime / interval) { slowRise = 0; Modes.increaseGain = 1; } else { @@ -804,21 +807,24 @@ static void gainStatistics(struct mag_buf *buf) { } } + if (Modes.increaseGain && Modes.gain == 496 && buf->sysTimestamp < nextRaiseAgc) { goto reset; } if (Modes.increaseGain || Modes.lowerGain) { - if (starting) { - Modes.lowerGain *= 2; - Modes.increaseGain *= 2; + if (Modes.gainStartup) { + Modes.lowerGain *= Modes.gainStartup; + Modes.increaseGain *= Modes.gainStartup; } char *reason = ""; - if (loud) { - reason = "decreasing gain, strong signal found: "; + if (veryLoud) { + reason = "decreasing gain, many strong signals found: "; + } else if (loud) { + reason = "decreasing gain, strong signal found: "; } else if (noiseHigh) { - reason = "decreasing gain, noise too high: "; + reason = "decreasing gain, noise too high: "; } else if (noiseLow) { - reason = "increasing gain, noise too low: "; + reason = "increasing gain, noise too low: "; } sdrSetGain(reason); if (Modes.gain == MODES_RTL_AGC) { @@ -829,6 +835,7 @@ static void gainStatistics(struct mag_buf *buf) { } reset: + Modes.gainStartup /= 2; loudEvents = 0; noiseLowSamples = 0; noiseHighSamples = 0; @@ -1523,7 +1530,7 @@ static int parseLongs(char *p, long long *results, int result_size) { } static void parseGainOpt(char *arg) { - Modes.gainStartupTime = mono_milli_seconds(); + Modes.gainStartup = 8; int maxTokens = 128; char* token[maxTokens]; if (!arg) { @@ -1547,7 +1554,7 @@ static void parseGainOpt(char *arg) { if (token[1]) { Modes.gain = (int) (atof(token[1])*10); // Gain is in tens of DBs } else { - Modes.gain = 439; + Modes.gain = 300; } if (token[2]) { Modes.noiseLowThreshold = atoi(token[2]); diff --git a/readsb.h b/readsb.h index af295563..8bbf2014 100644 --- a/readsb.h +++ b/readsb.h @@ -538,8 +538,8 @@ struct _Modes int8_t lowerGain; int8_t autoGain; int8_t gainQuiet; + int8_t gainStartup; char *gainArg; - int64_t gainStartupTime; uint32_t loudThreshold; uint32_t noiseLowThreshold; uint32_t noiseHighThreshold;