used black
This commit is contained in:
@@ -40,7 +40,9 @@ class SignatureGenerator:
|
||||
|
||||
# Used when processing input:
|
||||
|
||||
self.ring_buffer_of_samples: RingBuffer[int] = RingBuffer(buffer_size=2048, default_value=0)
|
||||
self.ring_buffer_of_samples: RingBuffer[int] = RingBuffer(
|
||||
buffer_size=2048, default_value=0
|
||||
)
|
||||
|
||||
self.fft_outputs: RingBuffer[List[float]] = RingBuffer(
|
||||
buffer_size=256, default_value=[0.0 * 1025]
|
||||
@@ -91,12 +93,15 @@ class SignatureGenerator:
|
||||
self.next_signature.number_samples / self.next_signature.sample_rate_hz
|
||||
< self.MAX_TIME_SECONDS
|
||||
or sum(
|
||||
len(peaks) for peaks in self.next_signature.frequency_band_to_sound_peaks.values()
|
||||
len(peaks)
|
||||
for peaks in self.next_signature.frequency_band_to_sound_peaks.values()
|
||||
)
|
||||
< self.MAX_PEAKS
|
||||
):
|
||||
self.process_input(
|
||||
self.input_pending_processing[self.samples_processed : self.samples_processed + 128]
|
||||
self.input_pending_processing[
|
||||
self.samples_processed : self.samples_processed + 128
|
||||
]
|
||||
)
|
||||
self.samples_processed += 128
|
||||
|
||||
@@ -107,7 +112,9 @@ class SignatureGenerator:
|
||||
self.next_signature.number_samples = 0
|
||||
self.next_signature.frequency_band_to_sound_peaks = {}
|
||||
|
||||
self.ring_buffer_of_samples: RingBuffer[int] = RingBuffer(buffer_size=2048, default_value=0)
|
||||
self.ring_buffer_of_samples: RingBuffer[int] = RingBuffer(
|
||||
buffer_size=2048, default_value=0
|
||||
)
|
||||
self.fft_outputs: RingBuffer[List[float]] = RingBuffer(
|
||||
buffer_size=256, default_value=[0.0 * 1025]
|
||||
)
|
||||
@@ -124,7 +131,9 @@ class SignatureGenerator:
|
||||
self.do_peak_spreading_and_recognition()
|
||||
|
||||
def do_fft(self, batch_of_128_s16le_mono_samples):
|
||||
type_ring = self.ring_buffer_of_samples.position + len(batch_of_128_s16le_mono_samples)
|
||||
type_ring = self.ring_buffer_of_samples.position + len(
|
||||
batch_of_128_s16le_mono_samples
|
||||
)
|
||||
self.ring_buffer_of_samples[
|
||||
self.ring_buffer_of_samples.position : type_ring
|
||||
] = batch_of_128_s16le_mono_samples
|
||||
@@ -159,10 +168,13 @@ class SignatureGenerator:
|
||||
temporary_array_1[1] = np.roll(temporary_array_1[1], -1)
|
||||
temporary_array_1[2] = np.roll(temporary_array_1[2], -2)
|
||||
|
||||
origin_last_fft_np = np.hstack([temporary_array_1.max(axis=0)[:-3], origin_last_fft[-3:]])
|
||||
origin_last_fft_np = np.hstack(
|
||||
[temporary_array_1.max(axis=0)[:-3], origin_last_fft[-3:]]
|
||||
)
|
||||
|
||||
i1, i2, i3 = [
|
||||
(self.spread_fft_output.position + former_fft_num) % self.spread_fft_output.buffer_size
|
||||
(self.spread_fft_output.position + former_fft_num)
|
||||
% self.spread_fft_output.buffer_size
|
||||
for former_fft_num in [-1, -3, -6]
|
||||
]
|
||||
|
||||
@@ -234,27 +246,38 @@ class SignatureGenerator:
|
||||
fft_number = self.spread_fft_output.num_written - 46
|
||||
|
||||
peak_magnitude = (
|
||||
np.log(max(1 / 64, fft_minus_46[bin_position])) * 1477.3 + 6144
|
||||
np.log(max(1 / 64, fft_minus_46[bin_position])) * 1477.3
|
||||
+ 6144
|
||||
)
|
||||
peak_magnitude_before = (
|
||||
np.log(max(1 / 64, fft_minus_46[bin_position - 1])) * 1477.3 + 6144
|
||||
np.log(max(1 / 64, fft_minus_46[bin_position - 1])) * 1477.3
|
||||
+ 6144
|
||||
)
|
||||
peak_magnitude_after = (
|
||||
np.log(max(1 / 64, fft_minus_46[bin_position + 1])) * 1477.3 + 6144
|
||||
np.log(max(1 / 64, fft_minus_46[bin_position + 1])) * 1477.3
|
||||
+ 6144
|
||||
)
|
||||
|
||||
peak_variation_1 = (
|
||||
peak_magnitude * 2 - peak_magnitude_before - peak_magnitude_after
|
||||
peak_magnitude * 2
|
||||
- peak_magnitude_before
|
||||
- peak_magnitude_after
|
||||
)
|
||||
peak_variation_2 = (
|
||||
(peak_magnitude_after - peak_magnitude_before) * 32 / peak_variation_1
|
||||
(peak_magnitude_after - peak_magnitude_before)
|
||||
* 32
|
||||
/ peak_variation_1
|
||||
)
|
||||
|
||||
corrected_peak_frequency_bin = bin_position * 64 + peak_variation_2
|
||||
corrected_peak_frequency_bin = (
|
||||
bin_position * 64 + peak_variation_2
|
||||
)
|
||||
|
||||
assert peak_variation_1 > 0
|
||||
|
||||
frequency_hz = corrected_peak_frequency_bin * (16000 / 2 / 1024 / 64)
|
||||
frequency_hz = corrected_peak_frequency_bin * (
|
||||
16000 / 2 / 1024 / 64
|
||||
)
|
||||
|
||||
if 250 < frequency_hz < 520:
|
||||
band = FrequencyBand.hz_250_520
|
||||
@@ -267,7 +290,10 @@ class SignatureGenerator:
|
||||
else:
|
||||
continue
|
||||
|
||||
if band not in self.next_signature.frequency_band_to_sound_peaks:
|
||||
if (
|
||||
band
|
||||
not in self.next_signature.frequency_band_to_sound_peaks
|
||||
):
|
||||
self.next_signature.frequency_band_to_sound_peaks[band] = []
|
||||
|
||||
self.next_signature.frequency_band_to_sound_peaks[band].append(
|
||||
|
||||
Reference in New Issue
Block a user