Measure signal power / noise power (at least in 2.4MHz mode).

Switch signalLevel back to a power measurement, don't put SNR in there.
But make it a 0.0 - 1.0 double so we're not scaling everywhere.

Adjust for the amplitude offset when calculating power.

Adapt everything else to the new scheme.
This commit is contained in:
Oliver Jowett 2015-01-22 01:01:39 +00:00
parent 5beecb9f4f
commit 19082d92ea
9 changed files with 118 additions and 28 deletions

View file

@ -153,10 +153,15 @@ void demodulate2400(uint16_t *m, uint32_t mlen) {
struct modesMessage mm;
unsigned char msg1[MODES_LONG_MSG_BYTES], msg2[MODES_LONG_MSG_BYTES], *msg;
uint32_t j;
uint32_t last_message_end = 0;
unsigned char *bestmsg;
int bestscore, bestphase;
// noise floor:
uint32_t noise_power_count = 0;
uint64_t noise_power_sum = 0;
memset(&mm, 0, sizeof(mm));
msg = msg1;
@ -167,6 +172,17 @@ void demodulate2400(uint16_t *m, uint32_t mlen) {
int initial_phase, first_phase, last_phase, try_phase;
int msglen;
// update noise for all samples that aren't part of a message
// (we don't know if m[j] is or not, yet, so work one sample
// in arrears)
if (j > last_message_end+1) {
// There seems to be a weird compiler bug I'm hitting here..
// if you compute the square directly, it occasionally gets mangled.
uint64_t s = TRUE_AMPLITUDE(m[j-1]);
noise_power_sum += s * s;
noise_power_count++;
}
// Look for a message starting at around sample 0 with phase offset 3..7
// Ideal sample values for preambles with different phase
@ -403,6 +419,28 @@ void demodulate2400(uint16_t *m, uint32_t mlen) {
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 = TRUE_AMPLITUDE(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
}
// Decode the received message
if (decodeModesMessage(&mm, bestmsg) < 0)
continue;
@ -426,10 +464,14 @@ void demodulate2400(uint16_t *m, uint32_t mlen) {
// where the preamble of the second message clobbered the last
// few bits of the first message, but the message bits didn't
// overlap)
last_message_end = j + (8 + msglen)*12/5;
j += (8 + msglen - 8)*12/5 - 1;
// Pass data to the next layer
useModesMessage(&mm);
}
Modes.stats_current.noise_power_sum += (noise_power_sum / MAX_POWER / noise_power_count);
Modes.stats_current.noise_power_count ++;
}