-
Notifications
You must be signed in to change notification settings - Fork 4
/
chesrf_sweep.py
executable file
·70 lines (59 loc) · 2.68 KB
/
chesrf_sweep.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
import argparse
import csv
import io
import shutil
import subprocess
import sys
# Parse Arguments
parser = argparse.ArgumentParser(description='Wireless Workbench scan generator that uses hackrf_sweep')
parser.add_argument('-o', '--output', default='./output.csv', metavar='FILE', help='Output file')
parser.add_argument('-b', '--bottom', default=400, type=int, metavar='Hz', help='Bottom end frequency bound')
parser.add_argument('-t', '--top', default=800, type=int, metavar='Hz', help='Top end frequency bound')
parser.add_argument('-w', '--width', default=2500, type=int, metavar='Hz', help='Sample bin width')
parser.add_argument('-z', '--offset', default=-23000, type=int, metavar='Hz', help='Frequency offset')
parser.add_argument('-g', '--gain', default=-30, type=int, metavar='dB', help='Gain adjust')
args = parser.parse_args()
# Check if the hackrf_sweep command is avaliable
if shutil.which('hackrf_sweep') is None:
print('The "hackrf_sweep" command is not avaliable!')
print('Please follow this guide to install the HackRF tools then try again:')
print('https://github.com/mossmann/hackrf/wiki/Operating-System-Tips')
sys.exit(1)
# Build command
command_str = ['hackrf_sweep', '-1',
'-f', '{}:{}'.format(args.bottom, args.top),
'-w', '{}'.format(args.width)]
# Run command and return output if there was a problem
print('Running command: {}'.format(' '.join(command_str)))
command = subprocess.run(command_str, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if command.returncode != 0:
print('\n' + command.stderr.decode().strip())
sys.exit(1)
# Read in the output from the command directly
print('Reading output')
csvreader = csv.reader(io.StringIO(command.stdout.decode()), delimiter=',', skipinitialspace=True)
# Loop over the results and do the do
print('Offsetting results by {} Hz'.format(args.offset))
print('Adjusting gain by {}{} dB'.format(('+' if args.gain > 0 else ''), args.gain))
freqs = []
powers = []
for line in csvreader:
date = line[0]
time = line[1]
hz_low = int(line[2])
hz_high = int(line[3])
hz_bin_width = float(line[4])
num_samples = line[5]
for number, value in enumerate(line[6:], start=1):
# Calculate the frequency for each bin and convert to MHz
freq = ((hz_low + (hz_bin_width * number) - (hz_bin_width / 2)) + args.offset) / 1000000
freqs.append('{:.3f}'.format(freq))
powers.append(float(value) + args.gain)
results = list(zip(freqs, powers))
# Save the output
print('Saving output to {}'.format(args.output))
with open(args.output, 'w') as csvfile:
csvwriter = csv.writer(csvfile)
for result in results:
csvwriter.writerow(result)