Floating point exception

I trying to employ my RTL-SDR to listen to the radio and got an AI program produced to find and play stations with a strong signal.

Here is what it came up with:-
sh:
#!/bin/sh

# ====================== CONFIGURATION ======================
MIN_FREQ=88.0
MAX_FREQ=108.0
STEP=0.1
GAIN=40
SQUELCH=-25
LISTEN_TIME=12
# ===========================================================

echo "FM Strong Station Player (FreeBSD)"
echo "Squelch: $SQUELCH dB | Gain: $GAIN"
echo "Press Ctrl+C to stop"
echo

freq=$MIN_FREQ

while [ "$(echo "$freq <= $MAX_FREQ" | bc -l)" -eq 1 ]; do
    # Measure signal power
    power=$(rtl_power -f ${freq}M:${freq}M:10k -g $GAIN -i 0.3 -e 1s 2>/dev/null | \
            awk -F',' '{s+=$6; n++} END {if(n>0) printf "%.1f", s/n; else print -99}')

    printf "\r→ %6.1f MHz   Power: %6.1f dB" "$freq" "$power"

    # Squelch check
    if [ "$(echo "$power > $SQUELCH" | bc -l)" -eq 1 ]; then
        echo
        echo
        echo ">>> Strong station found: ${freq} MHz ($power dB)"
        echo "Playing for $LISTEN_TIME seconds..."

        # Play station
        rtl_fm -f ${freq}M -M wbfm -s 200k -r 32k -g $GAIN -l 15 2>/dev/null | \
        play -r 32k -t raw -e signed-integer -b 16 -c 1 - 2>/dev/null &
        play_pid=$!

        sleep "$LISTEN_TIME"
        kill $play_pid 2>/dev/null
        wait $play_pid 2>/dev/null

        echo "----------------------------------------"
    fi

    # Increase frequency
    freq=$(echo "$freq + $STEP" | bc -l)
done

echo
echo "Scan finished."

Unfortunately the line

if [ "$(echo "$power > $SQUELCH" | bc -l)" -eq 1 ]; then
dumps with a floating point exception.

Is this line correctly created?

How would I run it from a command prompt using values of 99 and 25 for $power an $SQUELCH ?
 
Two further suggestion by GROK to eliminate the problem have failed.

The requirement was to create a program to find and play stations with a strong signal from an RTL-SDR dongle using rtl_power.
 
I was able to run it after changing "→" to "->" on line 24.

Code:
FM Strong Station Player (FreeBSD)
Squelch: -25 dB | Gain: 40
Press Ctrl+C to stop

 ->  108.0 MHz   Power:  -99.0 dB
Scan finished.
I simply ran it, so it might not have any particular significance.
 
The error seems to occur as a result of this line:-

while [ "$(echo "$freq <= $MAX_FREQ" | bc -l)" -eq 1 ]; do

Can anyone tell me what this means?

If $freq is 88.0 and $MAX_FREQ is 90.0 what does this expression evaluate to?

I can't figure out what bc -l does.
 
Is there a particular reason for using the shell here? Because there are plenty of languages that are much better suited for this—and more powerful, too.
man bc(1)

(Don't rely too heavily on AI to generate fully functional programs.)
 
Perhaps reading
Bash:
man bc
could shed some light into the last part of your question
-l, --mathlib
Sets scale (see the SYNTAX section) to 20 and loads the included
math library and the extended math library before running any code,
including any expressions or files specified on the command line.


That is clear as mud.
 
Is there a particular reason for using the shell here? Because there are plenty of languages that are much better suited for this—and more powerful, too.
man bc(1)

(Don't rely too heavily on AI to generate fully functional programs.)
Unfortunately that is all I have to go on.

I usually come here where there is HI to get mistakes corrected.

I'm getting the idea that AI created additional work rather than saving work.
 
I'm having trouble following along since I'm not at all familiar with RTL-SDR and shell, but I asked ChatGPT to analyze the script, and it found both major and minor errors.

Don't hesitate to let me know if you want more information.
PriorityIssueConsequence
1$6 used as signal powerThe detector does not work correctly
2freq:freq scan rangeMeasurement is questionable / poorly defined
3rtl_power launched 201 timesExtremely slow scanning
4No peak detectionThe same station may be detected multiple times
5No trap handlingCtrl+C shutdown is not clean
6Fixed 40 dB gainRisk of tuner saturation
7Floating-point values handled with bcWorks, but is unnecessarily cumbersome
 
I'm having trouble following along since I'm not at all familiar with RTL-SDR and shell, but I asked ChatGPT to analyze the script, and it found both major and minor errors.

Don't hesitate to let me know if you want more information.



I think a knowledge of RTL_SDR and shell is a prequisite to find a solution to this and probably need to be done from first principles and seems to revolve around the commands rtl_power and rtl_fm.

I don't have much knowledge of these commands but will try to investigate.
 
Start by reading the bc man page. It is actually intelligible. Then think about what the -l switch does. It would help to actually read the man page, which explicitly sends you to the discussion of scale in the syntax section.

What does floating point error mean? That should be a quick web search.

And then look at the command line that's used for bc here. Why don't you start by echoing the parameters that are really being sent to bc. I would duplicate the line with the bc, and replace bc with "echo" and see what is really happening. Be super careful with quoting, as the expression being sent into bc contains common shell meta characters (such as < and >), which is they are not quoted will make the shell do something other than is intended.

After that, think about what that line does: it calls bc, seemingly to compare two numbers. It then compares the result of bc with a constant. Do you really need bc here? If yes, why?
 
Back
Top