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;
}
long int rms(int16_t *samples, int len, int step)
/* largely lifted from rtl_power */
long int mean_power(int16_t *samples, int len, int step)
/* DC-corrected mean power (sqrt-free). Returns squared RMS units. */
{
int i;
long int rms;
long p, t, s;
int64_t p = 0;
int64_t t = 0;
int64_t s;
double dc, err;
p = t = 0L;
for (i=0; i<len; i+=step) {
s = (long)samples[i];
for (i = 0; i < len; i += step) {
s = (int64_t)samples[i];
t += s;
p += s * s;
}
/* correct for dc offset in squares */
dc = (double)(t*step) / (double)len;
err = t * 2 * dc - dc * dc * len;
dc = (double)(t * step) / (double)len;
err = (double)t * 2.0 * dc - dc * dc * (double)len;
rms = (long int)sqrt((p-err) / len);
//going with a value that's easy to figure out when its done the thing
if (rms < 0){ rms = 999; }
return rms;
double power = ((double)p - err) / (double)len;
if (power < 0.0) power = 0.0;
return (long int)power;
}
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
// 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
// sr = rms(demod.lowpassed, demod.lp_len, 1);
sr = rms(demod.lowpassed, 160, 1); //I wonder what a reasonable value would be for #2 (input len) there
// Return mean power (squared RMS) for soft squelch decisions (sqrt-free)
// sr = mean_power(demod.lowpassed, demod.lp_len, 1);
sr = mean_power(demod.lowpassed, 160, 1);
// #endif
return (sr);
}