Skip to content

Commit 11e4e8d

Browse files
committed
added multiprocessing support
1 parent 40d4aa0 commit 11e4e8d

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

python_hackrf/pyhackrf_tools/pyhackrf_sweep.pyx

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@
2121
# SOFTWARE.
2222

2323
# cython: language_level=3str
24-
from python_hackrf import pyhackrf
24+
2525
try:
26-
from scipy.fft import fft, fftshift # type: ignore
26+
from pyfftw.interfaces.numpy_fft import fft, fftshift # type: ignore
2727
except ImportError:
28-
from numpy.fft import fft, fftshift
28+
try:
29+
from scipy.fft import fft, fftshift # type: ignore
30+
except ImportError:
31+
from numpy.fft import fft, fftshift # type: ignore
2932

30-
from queue import Queue
33+
from libc.stdint cimport uint32_t, uint64_t
34+
from python_hackrf import pyhackrf
3135
cimport numpy as np
3236
import numpy as np
3337
import datetime
@@ -60,9 +64,8 @@ cdef int frequency_step_3 = 0
6064
binary_output_mode = False
6165
one_shot_mode = False
6266
file_object = None
63-
queue_mode = False
67+
queue = None
6468

65-
queue = Queue()
6669
window = None
6770

6871
cdef double time_start = 0.
@@ -73,22 +76,26 @@ cdef double time_second = 1.
7376
cdef int fftSize = 20
7477
cdef int data_length = 0
7578

76-
7779
cdef int pwr_1_start = 0
7880
cdef int pwr_1_stop = 0
7981
cdef int pwr_2_start = 0
8082
cdef int pwr_2_stop = 0
81-
cdef float norm_factor = 0
82-
cdef unsigned long start_frequency = 0
83+
cdef double norm_factor = 0
84+
cdef uint64_t start_frequency = 0
8385

8486
sweep_started = False
8587
run_available = True
8688
check_max_num_sweeps = False
8789

8890
cdef int max_num_sweeps = 0
89-
cdef unsigned long long accepted_bytes = 0
91+
cdef uint32_t accepted_bytes = 0
9092
cdef double sweep_rate = 0
91-
cdef unsigned long long sweep_count = 0
93+
cdef uint64_t sweep_count = 0
94+
95+
96+
def clear_queue(queue_to_clear: object):
97+
while not queue_to_clear.empty():
98+
queue_to_clear.get_nowait()
9299

93100

94101
def sigint_callback_handler(sig, frame):
@@ -112,13 +119,14 @@ cdef sweep_callback(buffer: np.ndarray[:], buffer_length: int, valid_length: int
112119
global fftSize, window, pwr_1_start, pwr_1_stop, pwr_2_start, pwr_2_stop, norm_factor, data_length, SAMPLE_RATE
113120
global start_frequency, sweep_count, sweep_started, max_num_sweeps, check_max_num_sweeps, accepted_bytes, one_shot_mode, run_available
114121
global binary_output_mode, file_object, queue_mode, SWEEP_STYLE
122+
global frequency_step_1, frequency_step_2, frequency_step_3
115123
global queue
116124

117125
timestamp = datetime.datetime.now()
118126
time_str = timestamp.strftime("%Y-%m-%d, %H:%M:%S.%f")
119127

120-
cdef unsigned long frequency = 0
121-
cdef int index = 0
128+
cdef uint64_t frequency = 0
129+
cdef uint32_t index = 0
122130
cdef int i, j
123131
for j in range(PY_BLOCKS_PER_TRANSFER):
124132
if buffer[index] == 127 and buffer[index + 1] == 127:
@@ -150,11 +158,13 @@ cdef sweep_callback(buffer: np.ndarray[:], buffer_length: int, valid_length: int
150158

151159
fftwOut = fft((buffer[index: index + data_length: 2].astype(np.int8, copy=False) / 128 + 1j * buffer[index + 1: index + data_length: 2].astype(np.int8, copy=False) / 128) * window)
152160
pwr = np.log10(np.abs(fftwOut * norm_factor) ** 2) * 10.0
153-
161+
sys.stderr.write('1')
154162
if SWEEP_STYLE == pyhackrf.py_sweep_style.LINEAR:
155163
pwr = fftshift(pwr)
156164

157165
index += data_length
166+
sys.stderr.write('2')
167+
sys.stderr.write(f'2.5 ({len(pwr)}, {fftSize})')
158168
if binary_output_mode:
159169
if SWEEP_STYLE == pyhackrf.py_sweep_style.INTERLEAVED:
160170
record_length = 16 + (fftSize // 4) * 4
@@ -179,23 +189,23 @@ cdef sweep_callback(buffer: np.ndarray[:], buffer_length: int, valid_length: int
179189
else:
180190
file_object.write(line)
181191

182-
elif queue_mode:
192+
elif queue is not None:
183193
if SWEEP_STYLE == pyhackrf.py_sweep_style.INTERLEAVED:
184-
queue.put_nowait({
194+
queue.put({
185195
'timestamp': time_str,
186196
'start_frequency': frequency,
187197
'stop_frequency': frequency + frequency_step_1,
188198
'array': pwr[pwr_1_start: pwr_1_stop]
189199
})
190-
queue.put_nowait({
200+
queue.put({
191201
'timestamp': time_str,
192202
'start_frequency': frequency + frequency_step_2,
193203
'stop_frequency': frequency + frequency_step_3,
194204
'array': pwr[pwr_2_start: pwr_2_stop]
195205
})
196206

197207
else:
198-
queue.put_nowait({
208+
queue.put({
199209
'timestamp': time_str,
200210
'start_frequency': frequency,
201211
'stop_frequency': frequency + SAMPLE_RATE,
@@ -225,28 +235,29 @@ cdef sweep_callback(buffer: np.ndarray[:], buffer_length: int, valid_length: int
225235
else:
226236
file_object.write(line)
227237

238+
sys.stderr.write('3\n')
228239
accepted_bytes += valid_length
240+
sys.stderr.write('\n\n')
229241

230242
return 0
231243

232244

233245
def pyhackrf_sweep(frequencies: list = [0, 6000], lna_gain: int = 16, vga_gain: int = 20, bin_width: int = 100_000,
234246
serial_number: str = None, amp_enable: bool = False, antenna_enable: bool = False, sample_rate: int = 20_000_000, baseband_filter: int = 15_000_000,
235-
num_sweeps: int = None, binary_output: bool = False, one_shot: bool = False, use_queue: bool = False, filename: str = None, sweep_style: pyhackrf.py_sweep_style = pyhackrf.py_sweep_style.INTERLEAVED,
236-
print_to_console: bool = True, device: pyhackrf.PyHackrfDevice = None):
247+
num_sweeps: int = None, binary_output: bool = False, one_shot: bool = False, filename: str = None, sweep_style: pyhackrf.py_sweep_style = pyhackrf.py_sweep_style.INTERLEAVED,
248+
print_to_console: bool = True, device: pyhackrf.PyHackrfDevice = None, sweep_queue: object = None):
237249

238250
global fftSize, window, pwr_1_start, pwr_1_stop, pwr_2_start, pwr_2_stop, norm_factor, data_length, SAMPLE_RATE
239251
global start_frequency, sweep_count, sweep_started, max_num_sweeps, check_max_num_sweeps, accepted_bytes, one_shot_mode, run_available
240252
global binary_output_mode, file_object, queue_mode, SWEEP_STYLE
241253
global frequency_step_1, frequency_step_2, frequency_step_3
242254
global queue
243255

244-
queue = Queue()
245-
256+
queue = sweep_queue
246257
if sweep_style in pyhackrf.py_sweep_style:
247258
SWEEP_STYLE = sweep_style
248259
else:
249-
SWEEP_STYLE = pyhackrf.sweep_stylepyhackrf.py_sweep_style.INTERLEAVED
260+
SWEEP_STYLE = pyhackrf.py_sweep_style.INTERLEAVED
250261

251262
if sample_rate in AVAILABLE_SAMPLING_RATES:
252263
SAMPLE_RATE = int(sample_rate)
@@ -283,7 +294,6 @@ def pyhackrf_sweep(frequencies: list = [0, 6000], lna_gain: int = 16, vga_gain:
283294

284295
binary_output_mode = binary_output
285296
one_shot_mode = one_shot
286-
queue_mode = use_queue
287297

288298
if device is None:
289299
pyhackrf.pyhackrf_init()
@@ -383,6 +393,7 @@ def pyhackrf_sweep(frequencies: list = [0, 6000], lna_gain: int = 16, vga_gain:
383393
sys.stderr.write('Couldn\'t transfer any data for one second.\n')
384394
break
385395

396+
accepted_bytes = 0
386397
time_prev = time_now
387398

388399
if filename is not None:
@@ -413,5 +424,5 @@ def pyhackrf_sweep(frequencies: list = [0, 6000], lna_gain: int = 16, vga_gain:
413424
if print_to_console:
414425
sys.stderr.write('pyhackrf_exit() done\n')
415426

416-
queue.queue.clear()
427+
clear_queue(sweep_queue)
417428
run_available = False

0 commit comments

Comments
 (0)