Skip to content

Instantly share code, notes, and snippets.

@jkramarz
Last active January 8, 2020 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkramarz/d5e9b92775dfe01113a0e63677e47d4c to your computer and use it in GitHub Desktop.
Save jkramarz/d5e9b92775dfe01113a0e63677e47d4c to your computer and use it in GitHub Desktop.
kalibrate-rtl
#!/bin/env python3
import subprocess
import re
from functools import reduce
import sys
import argparse
def get_channels(band="GSM900", timeout=60):
output = subprocess.check_output(['./kal', '-s', band], timeout=timeout)
match = re.finditer('chan: (?P<channel>[\d]+) \(.*\) power: (?P<power>[\d.]+)', str(output))
return map(
lambda x: x.groupdict(),
match
)
def get_strongest_channel(channels):
return reduce(
lambda carry, item: carry if float(item['power']) < float(carry['power']) else item,
channels
)
def calibrate(channel_number, timeout=60):
output = subprocess.check_output(['./kal', '-c', channel_number], timeout=timeout)
match = re.search('average absolute error: (?P<ppm>[\d.\-]+) ppm', str(output))
return match.groupdict()['ppm']
parser = argparse.ArgumentParser(description='Get RTLSDR calibration PPM.')
parser.add_argument('band', type=str, nargs='?', help='GSM band to scan', default="GSM900", choices=['GSM850', 'GSM-R', 'GSM900', 'EGSM', 'DCS', 'PCS'])
args = parser.parse_args()
try:
channels = get_channels(args.band)
strongest = get_strongest_channel(channels)
ppm = calibrate(strongest['channel'])
print(ppm)
sys.exit(0)
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)
#!/bin/sh
if [[ "$1" == "-s" ]]
then
cat << EOF
Found 1 device(s):
0: ezcap USB 2.0 DVB-T/DAB/FM dongle
Using device 0: ezcap USB 2.0 DVB-T/DAB/FM dongle
Found Rafael Micro R820T tuner
Exact sample rate is: 270833.002142 Hz
kal: Scanning for GSM-850 base stations.
GSM-850:
chan: 128 (869.2MHz - 3.988kHz) power: 486634.32
chan: 143 (872.2MHz - 3.760kHz) power: 56331.63
EOF
elif [[ "$1" == "-c" ]]
then
cat << EOF
Found 1 device(s):
0: ezcap USB 2.0 DVB-T/DAB/FM dongle
Using device 0: ezcap USB 2.0 DVB-T/DAB/FM dongle
Found Rafael Micro R820T tuner
Exact sample rate is: 270833.002142 Hz
kal: Calculating clock frequency offset.
Using GSM-850 channel 128 (869.2MHz)
average [min, max] (range, stddev)
- 4.093kHz [-4102, -4083] (20, 5.314593)
overruns: 0
not found: 0
average absolute error: 4.709 ppm
EOF
else
cat << EOF
kalibrate v0.4.1-rtl, Copyright (c) 2010, Joshua Lackey
modified for use with rtl-sdr devices, Copyright (c) 2012, Steve Markgraf
Usage:
GSM Base Station Scan:
kal <-s band indicator> [options]
Clock Offset Calculation:
kal <-f frequency | -c channel> [options]
Where options are:
-s band to scan (GSM850, GSM-R, GSM900, EGSM, DCS, PCS)
-f frequency of nearby GSM base station
-c channel of nearby GSM base station
-b band indicator (GSM850, GSM-R, GSM900, EGSM, DCS, PCS)
-g gain in dB
-d rtl-sdr device index
-e initial frequency error in ppm
-E manual frequency offset in hz
-v verbose
-D enable debug messages
-h help
EOF
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment