Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autogain tweaks / cleanups #182

Merged
merged 4 commits into from
May 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 45 additions & 40 deletions rootfs/usr/local/bin/autogain1090
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,35 @@ source /scripts/common

low="${READSB_AUTOGAIN_LOW_PCT:-2.5}"
high="${READSB_AUTOGAIN_HIGH_PCT:-6.0}"
gain_array=(0.0 0.9 1.4 2.7 3.7 7.7 8.7 12.5 14.4 15.7 16.6 19.7 20.7 22.9 25.4 28.0 29.7 32.8 33.8 36.4 37.2 38.6 40.2 42.1 43.4 43.9 44.5 48.0 49.6 -10)

# this excludes 58 / tuner agc as the step is rather big resulting in oscillations and it's rarely a big improvement to use it
gain_array=(0.0 0.9 1.4 2.7 3.7 7.7 8.7 12.5 14.4 15.7 16.6 19.7 20.7 22.9 25.4 28.0 29.7 32.8 33.8 36.4 37.2 38.6 40.2 42.1 43.4 43.9 44.5 48.0 49.6)

autogain_dir="/var/globe_history/autogain"
mkdir -p $autogain_dir

if [[ -f "${autogain_dir}/suspend" ]]; then
# allow temporary suspending of autogain by touching this file
exit 0
fi

touch $autogain_dir/strong $autogain_dir/total


# if there are no messages received and noise is lower than this, increase gain
low_noise=-30
# if noise is higher than this, never increase gain
# if noise is higher than this, reduce gain
# this is valuable for cases of strong interference where we might not see any messages at all until the gain is lowered
high_noise=-15


# if noise is lower than this, increase gain
# this is valuable for cases of unamplified SDRs combined with bad antennas and a low initial gain setting
low_noise=-36


#work around stupid locale stuff
export LC_ALL=C

APP=dump1090-fa
if [[ -f /run/dump1090-fa/stats.json ]]; then
APP=dump1090-fa
elif [[ -f /run/readsb/stats.json ]]; then
APP=readsb
fi

stats=/run/$APP/stats.json
stats=/run/readsb/stats.json

if [[ "$1" == "reset" ]]; then
echo "Reset AutoGain - restarting with initial values and initialization process"
Expand All @@ -46,42 +52,47 @@ if [[ -z $oldstrong ]] || [[ -z $oldtotal ]]; then
oldtotal=0
fi

if ! grep -qs total $stats | grep -qs -e strong_signals $stats; then
echo "the decoder doesn't seem to be using an rtl-sdr device, can't help with that."
exit 1
fi

# start=$(jq '.total.start' < $stats)
# end=$(jq '.total.end' < $stats)

strong=$(jq '.total.local.strong_signals' < $stats | tee $autogain_dir/strong)
total=$(jq '.total.local.accepted | add' < $stats | tee $autogain_dir/total)
newstrong=$(jq '.total.local.strong_signals' < $stats)
newtotal=$(jq '.total.local.accepted | add' < $stats)

noise=$(jq '.last1min.local.noise'< $stats)

if [[ -z $strong ]] || [[ -z $total ]]; then
echo "unrecognized format: $stats"
if [[ -z $newstrong ]] || [[ -z $newtotal ]]; then
echo "unrecognized format or no rtl-sdr in use: $stats"
exit 1
fi

if (( oldtotal > total )) || (( oldstrong > strong )); then
if (( oldtotal > newtotal )) || (( oldstrong > newstrong )); then
oldstrong=0
oldtotal=0
fi

strong=$((strong - oldstrong))
total=$((total - oldtotal))
strong=$((newstrong - oldstrong))
total=$((newtotal - oldtotal))

#echo "strong: $strong total: $total noise: $noise"

if ! awk "BEGIN{ exit ($total < 100) }"; then
if ! awk "BEGIN{ exit ($noise < $low_noise) }"; then
reason="Very few messages seen and noise is $noise < $low_noise"
action=Increasing
else
if ! awk "BEGIN{ exit ($noise > $high_noise) }"; then
reason="Noise high: $noise dBFS > $high_noise dBFS"
action=Decreasing
fi

if ! awk "BEGIN{ exit ($noise < $low_noise) }"; then
reason="Noise low: $noise dBFS < $low_noise dBFS"
action=Increasing
else


if [[ -z "$action" ]] && ! awk "BEGIN{ exit ($total < 500) }"; then
echo "The decoder hasn't been running long enough, wait a bit!"
exit 0
fi
fi

echo "$newstrong" > $autogain_dir/strong
echo "$newtotal" > $autogain_dir/total

if [[ $total == 0 ]]; then
percent=0
else
Expand Down Expand Up @@ -131,17 +142,11 @@ if [[ -z "$action" ]] && ! awk "BEGIN{ exit ($strong < $low) }"; then
action=Increasing
fi

if ! awk "BEGIN{ exit ($strong > $high) }"; then
if [[ -z "$action" ]] && ! awk "BEGIN{ exit ($strong > $high) }"; then
reason="${strong}% messages >-3dB exceed upper boundary of ${high}%"
action=Decreasing
fi

if ! awk "BEGIN{ exit ($noise > $high_noise) }"; then
# if noise is higher than this, decrease gain
reason="Noise at ${noise} dBFS, decreasing gain!"
action=Decreasing
fi

if [[ "$action" == "Decreasing" ]]; then
gain_index=$((gain_index-1))
elif [[ "$action" == "Increasing" ]]; then
Expand All @@ -150,12 +155,12 @@ fi

gain="${gain_array[$gain_index]}"

if [[ "$action" == "Decreasing" ]] && [[ $gain_index == 0 ]]; then
if (( gain_index < 0 )); then
echo "Could have used some lower gain, but gain already at minimum! ($reason)"
exit 0
fi

if [[ $gain == "" ]] || [[ $gain == "-10" ]]; then
if [[ "$action" == "Increasing" ]] && [[ $gain == "" ]]; then
echo "Could have used some more gain, but gain is already at maximum! ($reason)"
exit 0
fi
Expand Down