From 07d8c31697952fa65d8d1597cf930fe31ca253eb Mon Sep 17 00:00:00 2001 From: Matthias Wirth Date: Tue, 30 Apr 2024 19:16:17 +0200 Subject: [PATCH 1/4] autogain: make noise based logic more consistent --- rootfs/usr/local/bin/autogain1090 | 48 ++++++++++++++++++------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/rootfs/usr/local/bin/autogain1090 b/rootfs/usr/local/bin/autogain1090 index 69a5b80..1cc598b 100755 --- a/rootfs/usr/local/bin/autogain1090 +++ b/rootfs/usr/local/bin/autogain1090 @@ -11,12 +11,16 @@ mkdir -p $autogain_dir 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 @@ -54,8 +58,8 @@ 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) @@ -64,24 +68,34 @@ if [[ -z $strong ]] || [[ -z $total ]]; then 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)) + +if ! awk "BEGIN{ exit ($noise > $high_noise) }"; then + reason="Noise high: $noise dBFS > $high_noise dBFS" + action=Decreasing +fi -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 < $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 @@ -131,17 +145,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 From cc54c2bc683608d05fb4617ce04dc76cda797a79 Mon Sep 17 00:00:00 2001 From: Matthias Wirth Date: Tue, 30 Apr 2024 19:24:27 +0200 Subject: [PATCH 2/4] autogain: more cleanups --- rootfs/usr/local/bin/autogain1090 | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/rootfs/usr/local/bin/autogain1090 b/rootfs/usr/local/bin/autogain1090 index 1cc598b..e9ef28d 100755 --- a/rootfs/usr/local/bin/autogain1090 +++ b/rootfs/usr/local/bin/autogain1090 @@ -5,7 +5,10 @@ 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 touch $autogain_dir/strong $autogain_dir/total @@ -24,14 +27,7 @@ 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" @@ -50,21 +46,14 @@ 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) 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 @@ -76,6 +65,8 @@ fi strong=$((newstrong - oldstrong)) total=$((newtotal - oldtotal)) +#echo "strong: $strong total: $total noise: $noise" + if ! awk "BEGIN{ exit ($noise > $high_noise) }"; then reason="Noise high: $noise dBFS > $high_noise dBFS" action=Decreasing @@ -158,12 +149,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 From fe6e8803f93a86f6d6758d27f871a887c264bfe1 Mon Sep 17 00:00:00 2001 From: Matthias Wirth Date: Tue, 30 Apr 2024 20:58:06 +0200 Subject: [PATCH 3/4] shellcheck --- rootfs/usr/local/bin/autogain1090 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rootfs/usr/local/bin/autogain1090 b/rootfs/usr/local/bin/autogain1090 index e9ef28d..81d17d8 100755 --- a/rootfs/usr/local/bin/autogain1090 +++ b/rootfs/usr/local/bin/autogain1090 @@ -149,7 +149,7 @@ fi gain="${gain_array[$gain_index]}" -if (( $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 From 6bf958c8dc27ac01ebf7a9a0e0a678a4b797c053 Mon Sep 17 00:00:00 2001 From: Matthias Wirth Date: Wed, 1 May 2024 15:12:07 +0200 Subject: [PATCH 4/4] autogain: allow temporary suspension via lock file --- rootfs/usr/local/bin/autogain1090 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rootfs/usr/local/bin/autogain1090 b/rootfs/usr/local/bin/autogain1090 index 81d17d8..b7efe01 100755 --- a/rootfs/usr/local/bin/autogain1090 +++ b/rootfs/usr/local/bin/autogain1090 @@ -11,6 +11,12 @@ 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 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