From 21bdc45bf0ac4f3b1233e7508b4d9ebc443103b3 Mon Sep 17 00:00:00 2001 From: Oliver Jowett Date: Tue, 16 Jun 2015 10:13:25 +0100 Subject: [PATCH] Tweaks to noise measurements. --- convert.c | 25 ++++++++++++----------- demod_2400.c | 56 ++++++++++++++++++++++++++-------------------------- dump1090.c | 2 -- dump1090.h | 1 - net_io.c | 4 ++-- stats.c | 6 +++--- stats.h | 4 ++-- 7 files changed, 48 insertions(+), 50 deletions(-) diff --git a/convert.c b/convert.c index f09990a..cb55dca 100644 --- a/convert.c +++ b/convert.c @@ -65,8 +65,8 @@ static void convert_uc8_nodc_power(void *iq_data, { uint16_t *in = iq_data; unsigned i; - uint16_t mag; uint64_t power = 0; + uint16_t mag; MODES_NOTUSED(state); @@ -74,45 +74,46 @@ static void convert_uc8_nodc_power(void *iq_data, for (i = 0; i < (nsamples>>3); ++i) { mag = Modes.maglut[*in++]; *mag_data++ = mag; - power += mag*mag; + power += (uint32_t)mag * (uint32_t)mag; mag = Modes.maglut[*in++]; *mag_data++ = mag; - power += mag*mag; + power += (uint32_t)mag * (uint32_t)mag; mag = Modes.maglut[*in++]; *mag_data++ = mag; - power += mag*mag; + power += (uint32_t)mag * (uint32_t)mag; mag = Modes.maglut[*in++]; *mag_data++ = mag; - power += mag*mag; + power += (uint32_t)mag * (uint32_t)mag; mag = Modes.maglut[*in++]; *mag_data++ = mag; - power += mag*mag; + power += (uint32_t)mag * (uint32_t)mag; mag = Modes.maglut[*in++]; *mag_data++ = mag; - power += mag*mag; + power += (uint32_t)mag * (uint32_t)mag; mag = Modes.maglut[*in++]; *mag_data++ = mag; - power += mag*mag; + power += (uint32_t)mag * (uint32_t)mag; mag = Modes.maglut[*in++]; *mag_data++ = mag; - power += mag*mag; + power += (uint32_t)mag * (uint32_t)mag; } for (i = 0; i < (nsamples&7); ++i) { mag = Modes.maglut[*in++]; *mag_data++ = mag; - power += mag*mag; + power += (uint32_t)mag * (uint32_t)mag; } - if (out_power) - *out_power = power / (65535.0 * 65535.0); + if (out_power) { + *out_power = power / 65535.0 / 65535.0; + } } static void convert_uc8_generic(void *iq_data, diff --git a/demod_2400.c b/demod_2400.c index 363cf33..5d81cb2 100644 --- a/demod_2400.c +++ b/demod_2400.c @@ -161,7 +161,7 @@ void demodulate2400(struct mag_buf *mag) uint16_t *m = mag->data; uint32_t mlen = mag->length; - double total_signal_power = 0.0; + uint64_t sum_scaled_signal_power = 0; memset(&mm, 0, sizeof(mm)); msg = msg1; @@ -412,30 +412,6 @@ void demodulate2400(struct mag_buf *mag) mm.score = bestscore; mm.bFlags = mm.correctedbits = 0; - // measure signal power - { - uint64_t signal_power_sum = 0; - double signal_power; - int signal_len = msglen*12/5 + 1; - int k; - - for (k = 0; k < signal_len; ++k) { - uint64_t s = m[j+19+k]; - signal_power_sum += s * s; - } - - mm.signalLevel = signal_power = signal_power_sum / MAX_POWER / signal_len; - Modes.stats_current.signal_power_sum += signal_power; - Modes.stats_current.signal_power_count ++; - - if (signal_power > Modes.stats_current.peak_signal_power) - Modes.stats_current.peak_signal_power = signal_power; - if (signal_power > 0.50119) - Modes.stats_current.strong_signal_count++; // signal power above -3dBFS - - total_signal_power += signal_power_sum / MAX_POWER; - } - // Decode the received message { int result = decodeModesMessage(&mm, bestmsg); @@ -450,6 +426,29 @@ void demodulate2400(struct mag_buf *mag) } } + // measure signal power + { + double signal_power; + uint64_t scaled_signal_power = 0; + int signal_len = msglen*12/5; + int k; + + for (k = 0; k < signal_len; ++k) { + uint32_t mag = m[j+19+k]; + scaled_signal_power += mag * mag; + } + + signal_power = scaled_signal_power / 65535.0 / 65535.0; + mm.signalLevel = signal_power / signal_len; + Modes.stats_current.signal_power_sum += signal_power; + Modes.stats_current.signal_power_count += signal_len; + sum_scaled_signal_power += scaled_signal_power; + + if (mm.signalLevel > Modes.stats_current.peak_signal_power) + Modes.stats_current.peak_signal_power = mm.signalLevel; + if (mm.signalLevel > 0.50119) + Modes.stats_current.strong_signal_count++; // signal power above -3dBFS + } // Skip over the message: // (we actually skip to 8 bits before the end of the message, @@ -457,7 +456,7 @@ void demodulate2400(struct mag_buf *mag) // where the preamble of the second message clobbered the last // few bits of the first message, but the message bits didn't // overlap) - j += (8 + msglen - 8)*12/5 - 1; + j += msglen*12/5; // Pass data to the next layer useModesMessage(&mm); @@ -465,8 +464,9 @@ void demodulate2400(struct mag_buf *mag) /* update noise power if measured */ if (Modes.measure_noise) { - Modes.stats_current.noise_power_sum += (mag->total_power - total_signal_power) / mag->length; - Modes.stats_current.noise_power_count ++; + double sum_signal_power = sum_scaled_signal_power / 65535.0 / 65535.0; + Modes.stats_current.noise_power_sum += (mag->total_power - sum_signal_power); + Modes.stats_current.noise_power_count += mag->length; } } diff --git a/dump1090.c b/dump1090.c index 5941ca2..a8e96af 100644 --- a/dump1090.c +++ b/dump1090.c @@ -175,7 +175,6 @@ void modesInit(void) { Modes.trailing_samples = (MODES_PREAMBLE_US + MODES_LONG_MSG_BITS + 16) * 1e-6 * Modes.sample_rate; if ( ((Modes.maglut = (uint16_t *) malloc(sizeof(uint16_t) * 256 * 256) ) == NULL) || - ((Modes.magsqlut = (uint16_t *) malloc(sizeof(uint16_t) * 256 * 256) ) == NULL) || ((Modes.log10lut = (uint16_t *) malloc(sizeof(uint16_t) * 256 * 256) ) == NULL) ) { fprintf(stderr, "Out of memory allocating data buffer.\n"); @@ -231,7 +230,6 @@ void modesInit(void) { if (magsq > 1) magsq = 1; - Modes.magsqlut[le16toh((i*256)+q)] = (uint16_t) round(magsq * 65535.0); Modes.maglut[le16toh((i*256)+q)] = (uint16_t) round(sqrtf(magsq) * 65535.0); } } diff --git a/dump1090.h b/dump1090.h index 999814a..67f0c11 100644 --- a/dump1090.h +++ b/dump1090.h @@ -268,7 +268,6 @@ struct { // Internal state int fd; // --ifile option file descriptor input_format_t input_format; // --iformat option uint16_t *maglut; // I/Q -> Magnitude lookup table - uint16_t *magsqlut; // I/Q -> Magnitude-squared lookup table uint16_t *log10lut; // Magnitude -> log10 lookup table int exit; // Exit from the main loop when true diff --git a/net_io.c b/net_io.c index ba079e5..236f439 100644 --- a/net_io.c +++ b/net_io.c @@ -853,9 +853,9 @@ static char * appendStatsJson(char *p, p += snprintf(p, end-p, "]"); - if (st->signal_power_count > 0) + if (st->signal_power_sum > 0 && st->signal_power_count > 0) p += snprintf(p, end-p,",\"signal\":%.1f", 10 * log10(st->signal_power_sum / st->signal_power_count)); - if (st->noise_power_count > 0) + if (st->noise_power_sum > 0 && st->noise_power_count > 0) p += snprintf(p, end-p,",\"noise\":%.1f", 10 * log10(st->noise_power_sum / st->noise_power_count)); if (st->peak_signal_power > 0) p += snprintf(p, end-p,",\"peak_signal\":%.1f", 10 * log10(st->peak_signal_power)); diff --git a/stats.c b/stats.c index 26467f3..8c47732 100644 --- a/stats.c +++ b/stats.c @@ -89,17 +89,17 @@ void display_stats(struct stats *st) { for (j = 1; j <= Modes.nfix_crc; ++j) printf(" %u accepted with %d-bit error repaired\n", st->demod_accepted[j], j); - if (st->noise_power_count) { + if (st->noise_power_sum > 0 && st->noise_power_count > 0) { printf(" %.1f dBFS noise floor\n", 10 * log10(st->noise_power_sum / st->noise_power_count)); } - if (st->signal_power_count) { + if (st->signal_power_sum > 0 && st->signal_power_count > 0) { printf(" %.1f dBFS mean signal power\n", 10 * log10(st->signal_power_sum / st->signal_power_count)); } - if (st->peak_signal_power) { + if (st->peak_signal_power > 0) { printf(" %.1f dBFS peak signal power\n", 10 * log10(st->peak_signal_power)); } diff --git a/stats.h b/stats.h index 0c30c8f..5692d08 100644 --- a/stats.h +++ b/stats.h @@ -73,11 +73,11 @@ struct stats { // noise floor: double noise_power_sum; - uint32_t noise_power_count; + uint64_t noise_power_count; // mean signal power: double signal_power_sum; - uint32_t signal_power_count; + uint64_t signal_power_count; // peak signal power seen double peak_signal_power;