Skip to content

Instantly share code, notes, and snippets.

@ParkWardRR
Created December 22, 2023 20:31
Show Gist options
  • Save ParkWardRR/38d2d1f965935d57ef0e36a413693ebe to your computer and use it in GitHub Desktop.
Save ParkWardRR/38d2d1f965935d57ef0e36a413693ebe to your computer and use it in GitHub Desktop.
sdr-scan.sh
#!/bin/bash
: '
This script, named sdr-scan.sh, is designed to scan specific frequency ranges using a compatible Software-Defined Radio (SDR) such as RTL-SDR. The default ranges are set to scan segments commonly used by Walkie Talkie communications and trunked radios. These values can be changed by editing the RANGES array in the script.
User configurable variables:
- BANDWITH: Specifies the frequency range for each FFT.
- SAMPLING: Specifies the interval at which the frequency scan is performed.
- GAIN: Sets the tuner gain. Please note, high gain values may reduce the Signal to Noise Ratio (SNR).
- DURATION: Duration of the scan in format acceptable by `rtl_power` (e.g. 10s, 20m, 1h).
- OUT_DIR: Specifies the directory where the output CSV files will be stored.
Frequency ranges are provided in a bash array, with each element representing a different range given as "start_frequency:end_frequency". Results of the scan are stored into output CSV files for each range in the specified output directory.
How to use:
1. Make the script executable with: chmod +x sdr-scan.sh
2. Run the script: ./sdr-scan.sh
3. Check the /tmp/sdrOut directory for output CSV files.
Please note that the scanning results are raw and unprocessed data, suited for generating heatmaps or similar visual graphics.
'
# User configured variables
BANDWITH=1M # Bandwidth
SAMPLING=10 # Sampling interval
GAIN=50 # Gain
DURATION=1h # Duration of the scan
OUT_DIR=/tmp/sdrOut # Output directory
# Frequency ranges (start and end) for the targeted frequencies to scan
RANGES=("150M:400M" "400M:500M" "800M:900M")
# Function that converts the frequencies to Hz
function freq_to_hz() {
local freq=$1
local len=${#freq}
local scale="M"
if [[ $freq == *"k"* ]]; then
scale="k"
elif [[ $freq == *"G"* ]]; then
scale="G"
fi
# Transform frequency into Hertz
if [[ $scale == "k" ]]; then
freq=${freq//k/}*1000
elif [[ $scale == "M" ]]; then
freq=${freq//M/}*1000000
elif [[ $scale == "G" ]]; then
freq=${freq//G/}*1000000000
fi
echo $freq
}
mkdir -p "$OUT_DIR"
for range in "${RANGES[@]}"; do
START_FREQ=${range%:*}
END_FREQ=${range#*:}
START_FREQ_INT=$(freq_to_hz "$START_FREQ")
END_FREQ_INT=$(freq_to_hz "$END_FREQ")
if (( $START_FREQ_INT < 24000000 || $END_FREQ_INT > 1700000000 )); then
echo "$START_FREQ - $END_FREQ is out of the tuner's range. Please set the starting frequency >=24M and ending frequency <=1700M"
continue
fi
if (( $GAIN > 50 )); then
echo "Gain value $GAIN is too high. Please set the gain value <=50"
continue
fi
LOGFILE=$OUT_DIR/logfile_${START_FREQ}-$END_FREQ.csv
/usr/bin/rtl_power -f "$START_FREQ":"$END_FREQ":"$BANDWITH" -i "$SAMPLING" -g "$GAIN" -e "$DURATION" "$LOGFILE"
echo -e "\nScan results for frequency range $START_FREQ - $END_FREQ: "
cat "$LOGFILE"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment