Skip to content

Commit e94cce5

Browse files
committed
updated formula to db/Hz
1 parent 5dbec26 commit e94cce5

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

python_hackrf/pyhackrf_tools/pyhackrf_sweep.pyx

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def sweep_callback(object device, cnp.ndarray[cnp.int8_t, ndim=1] buffer, int bu
8484
cdef str time_str = timestamp.strftime('%Y-%m-%d, %H:%M:%S.%f')
8585

8686
cdef dict current_device_data = device_data[device.serialno]
87-
cdef double norm_factor = 1.0 / current_device_data['fft_size']
87+
cdef double norm_factor = current_device_data['sample_rate'] / current_device_data['fft_size']
8888
cdef uint32_t data_length = current_device_data['fft_size'] * 2
8989
cdef object sweep_style = current_device_data['sweep_style']
9090
cdef uint32_t sample_rate = current_device_data['sample_rate']
@@ -94,13 +94,13 @@ def sweep_callback(object device, cnp.ndarray[cnp.int8_t, ndim=1] buffer, int bu
9494
cdef uint64_t start_frequency = current_device_data['start_frequency']
9595

9696
cdef cnp.ndarray fftwOut
97-
cdef cnp.ndarray pwr
97+
cdef cnp.ndarray dbph
9898

99-
cdef uint32_t pwr_1_start = 1 + (fft_size * 5) // 8
100-
cdef uint32_t pwr_1_stop = 1 + (fft_size * 5) // 8 + fft_size // 4
99+
cdef uint32_t fft_1_start = 1 + (fft_size * 5) // 8
100+
cdef uint32_t fft_1_stop = 1 + (fft_size * 5) // 8 + fft_size // 4
101101

102-
cdef uint32_t pwr_2_start = 1 + fft_size // 8
103-
cdef uint32_t pwr_2_stop = 1 + fft_size // 8 + fft_size // 4
102+
cdef uint32_t fft_2_start = 1 + fft_size // 8
103+
cdef uint32_t fft_2_stop = 1 + fft_size // 8 + fft_size // 4
104104

105105
cdef uint64_t frequency = 0
106106
cdef uint32_t index = 0
@@ -138,10 +138,10 @@ def sweep_callback(object device, cnp.ndarray[cnp.int8_t, ndim=1] buffer, int bu
138138
index += (pyhackrf.PY_BYTES_PER_BLOCK - data_length)
139139

140140
fftwOut = fft((buffer[index:index + data_length:2] / 128 + 1j * buffer[index + 1:index + data_length:2] / 128) * window)
141-
pwr = np.log10(np.abs(fftwOut) ** 2 * norm_factor) * 10.0
141+
dbph = np.log10(np.abs(fftwOut) ** 2 * norm_factor) * 10.0
142142

143143
if sweep_style == pyhackrf.py_sweep_style.LINEAR:
144-
pwr = fftshift(pwr)
144+
dbph = fftshift(dbph)
145145

146146
index += data_length
147147

@@ -151,18 +151,18 @@ def sweep_callback(object device, cnp.ndarray[cnp.int8_t, ndim=1] buffer, int bu
151151
line = struct.pack('I', record_length)
152152
line += struct.pack('Q', frequency)
153153
line += struct.pack('Q', frequency + sample_rate // 4)
154-
line += struct.pack('<' + 'f' * (fft_size // 4), *pwr[pwr_1_start:pwr_1_stop])
154+
line += struct.pack('<' + 'f' * (fft_size // 4), *dbph[fft_1_start:fft_1_stop])
155155
line += struct.pack('I', record_length)
156156
line += struct.pack('Q', frequency + sample_rate // 2)
157157
line += struct.pack('Q', frequency + (sample_rate * 3) // 4)
158-
line += struct.pack('<' + 'f' * (fft_size // 4), *pwr[pwr_2_start:pwr_2_stop])
158+
line += struct.pack('<' + 'f' * (fft_size // 4), *dbph[fft_2_start:fft_2_stop])
159159

160160
else:
161161
record_length = 16 + fft_size * 4
162162
line = struct.pack('I', record_length)
163163
line += struct.pack('Q', frequency)
164164
line += struct.pack('Q', frequency + sample_rate)
165-
line += struct.pack('<' + 'f' * fft_size, *pwr)
165+
line += struct.pack('<' + 'f' * fft_size, *dbph)
166166

167167
current_device_data['file'].write(line)
168168

@@ -172,39 +172,39 @@ def sweep_callback(object device, cnp.ndarray[cnp.int8_t, ndim=1] buffer, int bu
172172
'timestamp': time_str,
173173
'start_frequency': frequency,
174174
'stop_frequency': frequency + sample_rate // 4,
175-
'array': pwr[pwr_1_start:pwr_1_stop]
175+
'array': dbph[fft_1_start:fft_1_stop]
176176
})
177177
current_device_data['queue'].put({
178178
'timestamp': time_str,
179179
'start_frequency': frequency + sample_rate // 2,
180180
'stop_frequency': frequency + (sample_rate * 3) // 4,
181-
'array': pwr[pwr_2_start:pwr_2_stop]
181+
'array': dbph[fft_2_start:fft_2_stop]
182182
})
183183

184184
else:
185185
current_device_data['queue'].put({
186186
'timestamp': time_str,
187187
'start_frequency': frequency,
188188
'stop_frequency': frequency + sample_rate,
189-
'array': pwr
189+
'array': dbph
190190
})
191191

192192
else:
193193
if sweep_style == pyhackrf.py_sweep_style.INTERLEAVED:
194194
line = f'{time_str}, {frequency}, {frequency + sample_rate // 4}, {sample_rate / fft_size}, {fft_size}, '
195-
pwr_1 = pwr[pwr_1_start:pwr_1_stop]
196-
for i in range(len(pwr_1)):
197-
line += f'{pwr_1[i]:.10f}, '
195+
dbph_1 = dbph[fft_1_start:fft_1_stop]
196+
for i in range(len(dbph_1)):
197+
line += f'{dbph_1[i]:.10f}, '
198198
line += f'\n{time_str}, {frequency + sample_rate // 2}, {frequency + (sample_rate * 3) // 4}, {sample_rate / fft_size}, {fft_size}, '
199-
pwr_2 = pwr[pwr_2_start:pwr_2_stop]
200-
for i in range(len(pwr_2)):
201-
line += f'{pwr_2[i]:.10f}, '
199+
dbph_2 = dbph[fft_2_start:fft_2_stop]
200+
for i in range(len(dbph_2)):
201+
line += f'{dbph_2[i]:.10f}, '
202202
line = line[:len(line) - 2] + '\n'
203203

204204
else:
205205
line = f'{time_str}, {frequency}, {frequency + sample_rate}, {sample_rate / fft_size}, {fft_size}, '
206-
for i in range(len(pwr)):
207-
line += f'{pwr[i]:.2f}, '
206+
for i in range(len(dbph)):
207+
line += f'{dbph[i]:.2f}, '
208208
line = line[:len(line) - 2] + '\n'
209209

210210
current_device_data['file'].write(line)

0 commit comments

Comments
 (0)