Skip to content

Commit 739f6af

Browse files
committed
tweak autogain
1 parent e1b2cec commit 739f6af

File tree

4 files changed

+84
-44
lines changed

4 files changed

+84
-44
lines changed

demod_2400.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -290,12 +290,12 @@ void demodulate2400(struct mag_buf *mag) {
290290
uint16_t *statsProgress = m;
291291

292292
const uint32_t statsWindow = MODES_SHORT_MSG_SAMPLES / 2; // half a short message
293-
uint32_t loudSamples = 0;
294-
uint32_t quietSamples = 0;
295-
uint32_t quiet2Samples = 0;
296-
uint32_t loudThreshold = 55000 * statsWindow; // max 65536
297-
uint32_t quietThreshold = 800 * statsWindow;
298-
uint32_t quiet2Threshold = 2 * quietThreshold;
293+
uint32_t loudEvents = 0;
294+
uint32_t noiseLowSamples = 0;
295+
uint32_t noiseHighSamples = 0;
296+
const uint32_t loudThreshold = Modes.loudThreshold * Modes.loudThreshold * statsWindow;
297+
const uint32_t noiseLowThreshold = Modes.noiseLowThreshold * Modes.noiseLowThreshold * statsWindow;
298+
const uint32_t noiseHighThreshold = Modes.noiseHighThreshold * Modes.noiseHighThreshold * statsWindow;
299299

300300
for (; pa < stop; pa++) {
301301
int32_t pa_mag, base_noise, ref_level;
@@ -323,9 +323,9 @@ void demodulate2400(struct mag_buf *mag) {
323323
for (uint32_t i = 0; i < statsWindow; i++) {
324324
magSum += pa[i];
325325
}
326-
loudSamples += statsWindow * (magSum > loudThreshold);
327-
quietSamples += statsWindow * (magSum < quietThreshold);
328-
quiet2Samples += statsWindow * (magSum < quiet2Threshold);
326+
loudEvents += (magSum > loudThreshold);
327+
noiseLowSamples += statsWindow * (magSum < noiseLowThreshold);
328+
noiseHighSamples += statsWindow * (magSum < noiseHighThreshold);
329329

330330
statsProgress = pa + statsWindow;
331331
}
@@ -493,9 +493,9 @@ void demodulate2400(struct mag_buf *mag) {
493493
netUseMessage(mm);
494494
}
495495

496-
mag->loudSamples = loudSamples;
497-
mag->quietSamples = quietSamples;
498-
mag->quiet2Samples = quiet2Samples;
496+
mag->loudEvents = loudEvents;
497+
mag->noiseLowSamples = noiseLowSamples;
498+
mag->noiseHighSamples = noiseHighSamples;
499499

500500
/* update noise power */
501501
{

readsb.c

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ static void configSetDefaults(void) {
116116

117117
// Now initialise things that should not be 0/NULL to their defaults
118118
Modes.gain = MODES_MAX_GAIN;
119+
120+
// 8 bit autogain defaults, will be squared and compared against magnitude data
121+
Modes.loudThreshold = 245;
122+
Modes.noiseLowThreshold = 25;
123+
Modes.noiseHighThreshold = 35;
124+
119125
Modes.freq = MODES_DEFAULT_FREQ;
120126
Modes.check_crc = 1;
121127
Modes.net_heartbeat_interval = MODES_NET_HEARTBEAT_INTERVAL;
@@ -752,61 +758,76 @@ static void *globeBinEntryPoint(void *arg) {
752758
}
753759

754760
static void gainStatistics(struct mag_buf *buf) {
755-
static uint64_t loudSamples;
756-
static uint64_t quietSamples;
757-
static uint64_t quiet2Samples;
761+
static uint64_t loudEvents;
762+
static uint64_t noiseLowSamples;
763+
static uint64_t noiseHighSamples;
758764
static uint64_t totalSamples;
759765
static int slowRise;
760766
static int lastGain;
761767

762-
loudSamples += buf->loudSamples;
763-
quietSamples += buf->quietSamples;
764-
quiet2Samples += buf->quiet2Samples;
768+
loudEvents += buf->loudEvents;
769+
noiseLowSamples += buf->noiseLowSamples;
770+
noiseHighSamples += buf->noiseHighSamples;
765771
totalSamples += buf->length;
766772

767-
if (totalSamples < 2 * Modes.sample_rate) {
773+
double interval = 0.5;
774+
775+
if (totalSamples < interval * Modes.sample_rate) {
768776
return;
769777
}
770778

771-
double loudPercent = loudSamples / (double) totalSamples * 100.0;
772-
double quietPercent = quietSamples / (double) totalSamples * 100.0;
773-
double quiet2Percent = quiet2Samples / (double) totalSamples * 100.0;
774-
775-
// reset
776-
loudSamples = 0;
777-
quietSamples = 0;
778-
quiet2Samples = 0;
779-
totalSamples = 0;
779+
double noiseLowPercent = noiseLowSamples / (double) totalSamples * 100.0;
780+
double noiseHighPercent = noiseHighSamples / (double) totalSamples * 100.0;
780781

781782
if (!Modes.autoGain) {
782-
// don't adjust anything
783-
return;
783+
goto reset;
784784
}
785785

786-
if (loudPercent > 0.05 || quiet2Percent < 0.05) {
786+
// 29 gain values for typical rtl-sdr
787+
// allow startup to sweep entire range quickly, half it for double steps
788+
int starting = getUptime() < (29 / 2.0 * interval) * SECONDS;
789+
790+
char *action = "";
791+
int noiseLow = noiseLowPercent > 5; // too many samples < noiseLowThreshold
792+
int noiseHigh = noiseHighPercent < 0.2; // too few samples < noiseHighThreshold
793+
int loud = loudEvents > 1;
794+
if (loud || noiseHigh) {
795+
action = "decreased";
787796
Modes.lowerGain = 1;
788-
} else if (
789-
quietPercent > 10.0
790-
) {
791-
if (getUptime() < 1 * MINUTES || slowRise > 10) {
797+
} else if (noiseLow) {
798+
if (starting || slowRise > 15 / interval) {
792799
slowRise = 0;
793800
Modes.increaseGain = 1;
801+
action = "increased";
794802
} else {
795803
slowRise++;
796804
}
797805
}
806+
798807
if (Modes.increaseGain || Modes.lowerGain) {
799-
if (getUptime() < 1 * MINUTES) {
808+
if (starting) {
800809
Modes.lowerGain *= 2;
801810
Modes.increaseGain *= 2;
802811
}
812+
sdrSetGain();
803813
if (Modes.gain != lastGain) {
814+
if (loud) {
815+
fprintf(stderr, "%9s gain. loudEvents: %4lld\n", action, (long long) loudEvents);
816+
} else if (noiseHigh) {
817+
fprintf(stderr, "%9s gain. noise high.\n", action);
818+
} else if (noiseLow) {
819+
fprintf(stderr, "%9s gain. noise low.\n", action);
820+
}
821+
//fprintf(stderr, "%9s gain. noiseLow: %5.2f %% noiseHigh: %5.2f %% loudEvents: %4lld\n", action, noiseLowPercent, noiseHighPercent, (long long) loudEvents);
804822
lastGain = Modes.gain;
805-
fprintf(stderr, "loud: %8.4f %% quiet: %8.4f %% quiet2 %8.4f %%\n", loudPercent, quietPercent, quiet2Percent);
806823
}
807-
sdrSetGain();
808824
}
809825

826+
reset:
827+
loudEvents = 0;
828+
noiseLowSamples = 0;
829+
noiseHighSamples = 0;
830+
totalSamples = 0;
810831
}
811832

812833

@@ -1505,9 +1526,24 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {
15051526
Modes.dev_name = strdup(arg);
15061527
break;
15071528
case OptGain:
1508-
if (strcmp(arg, "auto") == 0) {
1529+
if (strcasestr(arg, "auto") == arg) {
15091530
Modes.autoGain = 1;
1510-
Modes.gain = 439;
1531+
char *argdup = strdup(arg);
1532+
tokenize(&argdup, ",", token, maxTokens);
1533+
if (token[1]) {
1534+
Modes.gain = (int) (atof(token[1])*10); // Gain is in tens of DBs
1535+
} else {
1536+
Modes.gain = 439;
1537+
}
1538+
if (token[2]) {
1539+
Modes.noiseLowThreshold = atoi(token[2]);
1540+
}
1541+
if (token[3]) {
1542+
Modes.noiseHighThreshold = atoi(token[3]);
1543+
}
1544+
if (token[4]) {
1545+
Modes.loudThreshold = atoi(token[4]);
1546+
}
15111547
} else {
15121548
Modes.gain = (int) (atof(arg)*10); // Gain is in tens of DBs
15131549
}

readsb.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,9 @@ struct mag_buf
456456
unsigned length; // Number of valid samples _after_ overlap. Total buffer length is buf->length + Modes.trailing_samples.
457457
int64_t sysTimestamp; // Estimated system time at start of block
458458
int64_t sysMicroseconds; // sysTimestamp in microseconds
459-
uint32_t loudSamples;
460-
uint32_t quietSamples;
461-
uint32_t quiet2Samples;
459+
uint32_t loudEvents;
460+
uint32_t noiseLowSamples;
461+
uint32_t noiseHighSamples;
462462
uint32_t padding2;
463463
uint16_t *data; // Magnitude data. Starts with Modes.trailing_samples worth of overlap from the previous block
464464
#if defined(__arm__)
@@ -536,6 +536,9 @@ struct _Modes
536536
int8_t increaseGain;
537537
int8_t lowerGain;
538538
int8_t autoGain;
539+
uint32_t loudThreshold;
540+
uint32_t noiseLowThreshold;
541+
uint32_t noiseHighThreshold;
539542
int gain;
540543
int dc_filter; // should we apply a DC filter?
541544
int enable_agc;

sdr_rtlsdr.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ void rtlsdrSetGain() {
108108
int closest = getClosestGainIndex(Modes.gain);
109109
if (Modes.increaseGain) {
110110
closest += Modes.increaseGain;
111-
} else if (Modes.lowerGain) {
111+
}
112+
if (Modes.lowerGain) {
112113
closest -= Modes.lowerGain;
113114
}
114115
if (closest >= RTLSDR.numgains) {

0 commit comments

Comments
 (0)