1
1
from copy import copy
2
2
from typing import List , Optional , Any
3
-
4
- from numpy import hanning , log , maximum , fft , array
3
+ import numpy as np
5
4
6
5
from .enums import FrequencyBand
7
6
from .signature import DecodedMessage , FrequencyPeak
8
7
9
- HANNING_MATRIX = hanning (2050 )[1 :- 1 ] # Wipe trailing and leading zeroes
8
+ HANNING_MATRIX = np . hanning (2050 )[1 :- 1 ] # Wipe trailing and leading zeroes
10
9
11
10
12
11
class RingBuffer (list ):
@@ -154,10 +153,10 @@ def do_fft(self, batch_of_128_s16le_mono_samples):
154
153
# The pre multiplication of the array is for applying a windowing function before the DFT
155
154
# (slight rounded Hanning without zeros at edges)
156
155
157
- fft_results : array = fft .rfft (HANNING_MATRIX * excerpt_from_ring_buffer )
156
+ fft_results : np . array = np . fft .rfft (HANNING_MATRIX * excerpt_from_ring_buffer )
158
157
159
158
fft_results = (fft_results .real ** 2 + fft_results .imag ** 2 ) / (1 << 17 )
160
- fft_results = maximum (fft_results , 0.0000000001 )
159
+ fft_results = np . maximum (fft_results , 0.0000000001 )
161
160
162
161
self .fft_outputs .append (fft_results )
163
162
@@ -168,39 +167,40 @@ def do_peak_spreading_and_recognition(self):
168
167
self .do_peak_recognition ()
169
168
170
169
def do_peak_spreading (self ):
171
-
172
170
origin_last_fft : List [float ] = self .fft_outputs [self .fft_outputs .position - 1 ]
173
171
174
- spread_last_fft : List [float ] = list (origin_last_fft )
175
-
176
- for position in range (1025 ):
177
-
178
- # Perform frequency-domain spreading of peak values
179
-
180
- if position < 1023 :
181
- spread_last_fft [position ] = max (
182
- spread_last_fft [position : position + 3 ]
183
- )
184
-
185
- # Perform time-domain spreading of peak values
172
+ temporary_array_1 = np .tile (origin_last_fft , 3 ).reshape ((3 , - 1 ))
173
+ temporary_array_1 [1 ] = np .roll (temporary_array_1 [1 ], - 1 )
174
+ temporary_array_1 [2 ] = np .roll (temporary_array_1 [2 ], - 2 )
186
175
187
- max_value = spread_last_fft [position ]
176
+ origin_last_fft_np = np .hstack (
177
+ [temporary_array_1 .max (axis = 0 )[:- 3 ], origin_last_fft [- 3 :]]
178
+ )
188
179
189
- for former_fft_num in [ - 1 , - 3 , - 6 ]:
190
- former_fft_output = self .spread_fft_output [
191
- ( self .spread_fft_output .position + former_fft_num )
192
- % self . spread_fft_output . buffer_size
193
- ]
180
+ i1 , i2 , i3 = [
181
+ ( self .spread_fft_output . position + former_fft_num )
182
+ % self .spread_fft_output .buffer_size
183
+ for former_fft_num in [ - 1 , - 3 , - 6 ]
184
+ ]
194
185
195
- former_fft_output [position ] = max_value = max (
196
- former_fft_output [position ], max_value
197
- )
186
+ temporary_array_2 = np .vstack (
187
+ [
188
+ origin_last_fft_np ,
189
+ self .spread_fft_output [i1 ],
190
+ self .spread_fft_output [i2 ],
191
+ self .spread_fft_output [i3 ],
192
+ ]
193
+ )
198
194
199
- # Save output locally
195
+ temporary_array_2 [1 ] = np .max (temporary_array_2 [:2 , :], axis = 0 )
196
+ temporary_array_2 [2 ] = np .max (temporary_array_2 [:3 , :], axis = 0 )
197
+ temporary_array_2 [3 ] = np .max (temporary_array_2 [:4 , :], axis = 0 )
200
198
201
- self .spread_fft_output .append (spread_last_fft )
199
+ self .spread_fft_output [i1 ] = temporary_array_2 [1 ].tolist ()
200
+ self .spread_fft_output [i2 ] = temporary_array_2 [2 ].tolist ()
201
+ self .spread_fft_output [i3 ] = temporary_array_2 [3 ].tolist ()
202
202
203
- pass
203
+ self . spread_fft_output . append ( list ( origin_last_fft_np ))
204
204
205
205
def do_peak_recognition (self ):
206
206
@@ -256,14 +256,15 @@ def do_peak_recognition(self):
256
256
fft_number = self .spread_fft_output .num_written - 46
257
257
258
258
peak_magnitude = (
259
- log (max (1 / 64 , fft_minus_46 [bin_position ])) * 1477.3 + 6144
259
+ np .log (max (1 / 64 , fft_minus_46 [bin_position ])) * 1477.3
260
+ + 6144
260
261
)
261
262
peak_magnitude_before = (
262
- log (max (1 / 64 , fft_minus_46 [bin_position - 1 ])) * 1477.3
263
+ np . log (max (1 / 64 , fft_minus_46 [bin_position - 1 ])) * 1477.3
263
264
+ 6144
264
265
)
265
266
peak_magnitude_after = (
266
- log (max (1 / 64 , fft_minus_46 [bin_position + 1 ])) * 1477.3
267
+ np . log (max (1 / 64 , fft_minus_46 [bin_position + 1 ])) * 1477.3
267
268
+ 6144
268
269
)
269
270
0 commit comments