Skip to content

Commit

Permalink
different autogain
Browse files Browse the repository at this point in the history
  • Loading branch information
wiedehopf committed Nov 1, 2024
1 parent 69bb3d1 commit ce8ffe7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
25 changes: 18 additions & 7 deletions demod_2400.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,15 @@ void demodulate2400(struct mag_buf *mag) {
uint16_t *pa = m;
uint16_t *stop = m + mlen;

uint16_t *statsProgress = m;

const uint32_t statsWindow = MODES_SHORT_MSG_SAMPLES / 2; // half a short message
uint32_t loudSamples = 0;
uint32_t quietSamples = 0;
uint32_t loudThreshold = 65400 * 8; // max 65536
uint32_t quietThreshold = 2000 * 8;
uint32_t quiet2Samples = 0;
uint32_t loudThreshold = 55000 * statsWindow; // max 65536
uint32_t quietThreshold = 800 * statsWindow;
uint32_t quiet2Threshold = 2 * quietThreshold;

for (; pa < stop; pa++) {
int32_t pa_mag, base_noise, ref_level;
Expand All @@ -313,12 +318,17 @@ void demodulate2400(struct mag_buf *mag) {
// due to plenty room in the message buffer for decoding
// we can with pa go beyond stop without a buffer overrun ...

uint32_t magSum = 0;
for (int i = 0; i < 8; i++) {
magSum += pa[i];
if (pa >= statsProgress) {
uint32_t magSum = 0;
for (uint32_t i = 0; i < statsWindow; i++) {
magSum += pa[i];
}
loudSamples += statsWindow * (magSum > loudThreshold);
quietSamples += statsWindow * (magSum < quietThreshold);
quiet2Samples += statsWindow * (magSum < quiet2Threshold);

statsProgress = pa + statsWindow;
}
loudSamples += 8 * (magSum > loudThreshold);
quietSamples += 8 * (magSum < quietThreshold);

if (pa[1] > pa[7] && pa[12] > pa[14] && pa[12] > pa[15]) { goto after_pre; }

Expand Down Expand Up @@ -494,6 +504,7 @@ void demodulate2400(struct mag_buf *mag) {

mag->loudSamples = loudSamples;
mag->quietSamples = quietSamples;
mag->quiet2Samples = quiet2Samples;

/* update noise power */
{
Expand Down
23 changes: 18 additions & 5 deletions readsb.c
Original file line number Diff line number Diff line change
Expand Up @@ -754,10 +754,13 @@ static void *globeBinEntryPoint(void *arg) {
static void gainStatistics(struct mag_buf *buf) {
static uint64_t loudSamples;
static uint64_t quietSamples;
static uint64_t quiet2Samples;
static uint64_t totalSamples;
static int slowRise;

loudSamples += buf->loudSamples;
quietSamples += buf->quietSamples;
quiet2Samples += buf->quiet2Samples;
totalSamples += buf->length;

if (totalSamples < 2 * Modes.sample_rate) {
Expand All @@ -766,27 +769,37 @@ static void gainStatistics(struct mag_buf *buf) {

double loudPercent = loudSamples / (double) totalSamples * 100.0;
double quietPercent = quietSamples / (double) totalSamples * 100.0;
double quiet2Percent = quiet2Samples / (double) totalSamples * 100.0;

// reset
loudSamples = 0;
quietSamples = 0;
totalSamples = 0;

if (!Modes.autoGain) {
if (0 && !Modes.autoGain) {
// don't adjust anything
return;
}

if (loudPercent > 0.01 || quietPercent < 0.1) {
if (loudPercent > 0.05 || quiet2Percent < 0.05) {
Modes.lowerGain = 1;
} else if (
quietPercent > 30.0
quietPercent > 10.0
) {
Modes.increaseGain = 1;
if (getUptime() < 1 * MINUTES || slowRise > 10) {
slowRise = 0;
Modes.increaseGain = 1;
} else {
slowRise++;
}
}
if (Modes.increaseGain || Modes.lowerGain) {
if (getUptime() < 1 * MINUTES) {
Modes.lowerGain *= 2;
Modes.increaseGain *= 2;
}
fprintf(stderr, "loud: %8.4f %% quiet: %8.4f %% quiet2 %8.4f %%\n", loudPercent, quietPercent, quiet2Percent);
sdrSetGain();
fprintf(stderr, "loud: %8.4f %% quiet: %8.4f %%\n", loudPercent, quietPercent);
}

}
Expand Down
2 changes: 2 additions & 0 deletions readsb.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ struct mag_buf
int64_t sysMicroseconds; // sysTimestamp in microseconds
uint32_t loudSamples;
uint32_t quietSamples;
uint32_t quiet2Samples;
uint32_t padding2;
uint16_t *data; // Magnitude data. Starts with Modes.trailing_samples worth of overlap from the previous block
#if defined(__arm__)
/*padding 4 bytes*/
Expand Down
4 changes: 2 additions & 2 deletions sdr_rtlsdr.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ void rtlsdrSetGain() {
if (Modes.increaseGain || Modes.lowerGain) {
int closest = getClosestGainIndex(Modes.gain);
if (Modes.increaseGain) {
closest++;
closest += Modes.increaseGain;
} else if (Modes.lowerGain) {
closest--;
closest -= Modes.lowerGain;
}
if (closest >= RTLSDR.numgains) {
closest = RTLSDR.numgains - 1;
Expand Down

0 comments on commit ce8ffe7

Please sign in to comment.