rtl_sdr_fm: avoid sqrt in RMS utility (use power-domain)

This commit is contained in:
arancormonk
2025-09-03 20:15:27 -04:00
parent 00e7c51a21
commit 6fc327c3b6

View File

@@ -943,28 +943,27 @@ static inline void audio_lpf_filter(struct demod_state *fm)
fm->audio_lpf_state = y; fm->audio_lpf_state = y;
} }
long int rms(int16_t *samples, int len, int step) long int mean_power(int16_t *samples, int len, int step)
/* largely lifted from rtl_power */ /* DC-corrected mean power (sqrt-free). Returns squared RMS units. */
{ {
int i; int i;
long int rms; int64_t p = 0;
long p, t, s; int64_t t = 0;
int64_t s;
double dc, err; double dc, err;
p = t = 0L; for (i = 0; i < len; i += step) {
for (i=0; i<len; i+=step) { s = (int64_t)samples[i];
s = (long)samples[i];
t += s; t += s;
p += s * s; p += s * s;
} }
/* correct for dc offset in squares */ /* correct for dc offset in squares */
dc = (double)(t*step) / (double)len; dc = (double)(t * step) / (double)len;
err = t * 2 * dc - dc * dc * len; err = (double)t * 2.0 * dc - dc * dc * (double)len;
rms = (long int)sqrt((p-err) / len); double power = ((double)p - err) / (double)len;
//going with a value that's easy to figure out when its done the thing if (power < 0.0) power = 0.0;
if (rms < 0){ rms = 999; } return (long int)power;
return rms;
} }
void full_demod(struct demod_state *d) void full_demod(struct demod_state *d)
@@ -1939,8 +1938,9 @@ long int rtl_return_rms()
//debug -- on main machine, lp_len is around 6420, so this probably contributes to very high CPU usage //debug -- on main machine, lp_len is around 6420, so this probably contributes to very high CPU usage
// fprintf (stderr, "LP_LEN: %d \n", demod.lp_len); // fprintf (stderr, "LP_LEN: %d \n", demod.lp_len);
//I've found that just using a sample size of 160 will give us a good approximation without killing the CPU //I've found that just using a sample size of 160 will give us a good approximation without killing the CPU
// sr = rms(demod.lowpassed, demod.lp_len, 1); // Return mean power (squared RMS) for soft squelch decisions (sqrt-free)
sr = rms(demod.lowpassed, 160, 1); //I wonder what a reasonable value would be for #2 (input len) there // sr = mean_power(demod.lowpassed, demod.lp_len, 1);
sr = mean_power(demod.lowpassed, 160, 1);
// #endif // #endif
return (sr); return (sr);
} }