How to scale spectrum demod bin amplitudes to dBm?

16 views
Skip to first unread message

Scott Newell N5TNL

unread,
Nov 29, 2024, 4:23:08 PM11/29/24
to ka9q-...@googlegroups.com
Can anyone help me understand the ka9q-radio spectrum demod bin
amplitude values? I'm wanting to scale them back to dBm, but I don't know how!

At radio_status.c:576 ( or
https://github.com/ka9q/ka9q-radio/blob/12220bc605a1e9073182b787f154042e645c50e4/radio_status.c#L576
)

// Average and clear
float const scale = 1.f / chan->status.blocks_since_poll;
for(int i=0; i < chan->spectrum.bin_count; i++)
chan->spectrum.bin_data[i] *= scale;

(BTW, Is there any chance blocks_since_poll could be zero?)

Trying to understand the scaling, I made a hack to see the min/max
and id the hottest bin:

// Average and clear
float const scale = 1.f / chan->status.blocks_since_poll;
float max_data=-9e99;
float min_data=9e99;
int hot_bin=-1;
for(int i=0; i < chan->spectrum.bin_count; i++){
chan->spectrum.bin_data[i] *= scale;
if (chan->spectrum.bin_data[i]>max_data){
max_data=chan->spectrum.bin_data[i];
if (i<chan->spectrum.bin_count/2){
hot_bin=i;
}
}
if (chan->spectrum.bin_data[i]<min_data)
min_data=chan->spectrum.bin_data[i];
}

fprintf(stderr, "ssrc=%d chan=%p blocks_since__poll=%lu
scale=%.3f min=%.0f max=%.0f hot bin=%d\n", chan->output.rtp.ssrc,
chan, chan->status.blocks_since_poll, scale, min_data, max_data, hot_bin);


My hottest signal is a local AM at 1230 kHz. With ka9q-web tuned to
1220 kHz and zoomed all the way in (40 Hz/bin), the hot bin is 250
(as expected) at about 18.5 M. And that 18.5 M is what I'm seeing at
ka9q-web. But the scale factor is bouncing all over the place (here
it ranges 0.167 to 0.250), yet the max amplitude seems about the
same. Is the energy in the bin so volatile that the 50% scale change
isn't noticeable?

2024-11-29T20:56:40.045388+00:00 srd radiod[1971598]: ssrc=1001
chan=0x7f28c84799f0 blocks_since_poll=4 scale=0.250 min=0
max=17293056 hot bin=250 bin_count=1620

2024-11-29T20:56:40.146164+00:00 srd radiod[1971598]: ssrc=1001
chan=0x7f28c84799f0 blocks_since_poll=5 scale=0.200 min=0
max=17477758 hot bin=250 bin_count=1620

2024-11-29T20:56:40.263663+00:00 srd radiod[1971598]: ssrc=1001
chan=0x7f28c84799f0 blocks_since_poll=6 scale=0.167 min=0
max=17493860 hot bin=250 bin_count=1620


On the other side, I've hacked ka9q-web to show the bin amplitudes.
It mostly matches:

BIN_DATA (96): l_count=1620 min=0.145159 max=17293056.000000 at bin 250
RF_GAIN (98): 23.287998
RF_ATTEN (97): 0.000000
RF_LEVEL_CAL (110): -1.400000
RF_AGC (99): 1
AD_OVER (104): overrange==7, BLOCKS_SINCE_POLL=103 = 2
SAMPLES_SINCE_OVER (108): 28465752165

BIN_DATA (96): l_count=1620 min=0.134593 max=17477758.000000 at bin 250
RF_GAIN (98): 23.287998
RF_ATTEN (97): 0.000000
RF_LEVEL_CAL (110): -1.400000
RF_AGC (99): 1
AD_OVER (104): overrange==7, BLOCKS_SINCE_POLL=103 = 2
SAMPLES_SINCE_OVER (108): 28473354341

BIN_DATA (96): l_count=1620 min=0.185661 max=17493860.000000 at bin 250
RF_GAIN (98): 23.287998
RF_ATTEN (97): 0.000000
RF_LEVEL_CAL (110): -1.400000
RF_AGC (99): 1
AD_OVER (104): overrange==7, BLOCKS_SINCE_POLL=103 = 2
SAMPLES_SINCE_OVER (108): 28479907941


Thinking I could undo the ka9q-radio bin amplitude scaling, I tried
to log blocks_since_poll in ka9q-web, but it doesn't match the
ka9q-radio log file! Sometimes it's 0, 3, 5, or 25. Quite possibly a
bug I've introduced. (But I didn't have any trouble grabbing metadata
such as rf gain, rf attenuation, rf gain cal, adc overs, and time
since last over...)

As a workaround, I've forced the radio_status.c scale to 1.0, and I'm
now seeing an amplitude of 88-90 M for that bin. I assume I should
scale each bin by 1/N (this is a '888 at 64.8 ksps, so N=1.62M), but
even after that my hottest bin will still read 50-60 or so. Should I
do that and assume full scale for a bin is 32768? That would put the
hot bin at (90/1.62)/32768, or -27 dB, so that's starting to sound
reasonable, at least until I add the '888 rf cal factor (do I add or
subtract it?).

Any advice or pointers would be appreciated.


thanks!
newell N5TNL

Scott Newell N5TNL

unread,
Dec 6, 2024, 12:46:27 AM12/6/24
to ka9q-...@googlegroups.com
At 03:23 PM 11/29/2024, Scott Newell wrote:
>Can anyone help me understand the ka9q-radio spectrum demod bin
>amplitude values? I'm wanting to scale them back to dBm, but I don't know how!

Progress! rx888.c was hacked up to overwrite the incoming '888
samples with int16_t data from a file. Created files of a simulated
100 kHz carrier with amplitudes of +/- 32766, +/- 3276, and +/- 327
ADC counts. During playback of the simulated carrier, metadump,
control, and the ka9q-web spectrum all acted as expected. I think I
know what to do now.

thanks,
newell N5TNL

Reply all
Reply to author
Forward
0 new messages