This commit is contained in:
CroneKorkN 2025-06-01 14:14:00 +02:00
parent d6b2fbd069
commit 6d7acd11ee
Signed by: cronekorkn
SSH key fingerprint: SHA256:v0410ZKfuO1QHdgKBsdQNF64xmTxOF8osF1LIqwTcVw

25
process
View file

@ -21,6 +21,7 @@ ADJACENCY_FACTOR = 2 # area to look for noise around the target frequency
AMPLITUDE_THRESHOLD = 200 # relative DB (rDB) (because not calibrated)
BLOCK_SECONDS = 3 # seconds (longer means more frequency resolution, but less time resolution)
DETECTION_DISTANCE = 30 # seconds (minimum time between detections)
BLOCK_OVERLAP = 0.8 # overlap between blocks (0.8 means 80% overlap)
def process_recording(filename):
@ -30,23 +31,31 @@ def process_recording(filename):
date_string_from_filename = os.path.splitext(filename)[0]
recording_date = datetime.datetime.strptime(date_string_from_filename, "%Y-%m-%d_%H-%M-%S.%f%z")
# get samplerate and blocksize
# get data and metadata from recording
path = os.path.join(RECORDINGS_DIR, filename)
info = soundfile.info(path)
samplerate = info.samplerate
block_samples = int(BLOCK_SECONDS * samplerate)
sound, samplerate = soundfile.read(path)
samples_per_block = int(BLOCK_SECONDS * samplerate)
overlap = int(samples_per_block * BLOCK_OVERLAP)
# get median amplitude for normalization
complex_amplitudes_global = rfft(sound)
median_amplitude = np.median(np.abs(complex_amplitudes_global))
print(f'median amplitude: {median_amplitude:.5f}rDB')
# initialize to a very old date
last_detection_at = datetime.datetime.min.replace(tzinfo=recording_date.tzinfo)
is_detecting = False
# iterate blocks
for num, block in enumerate(soundfile.blocks(path, blocksize=block_samples, overlap=int(block_samples*0.8))):
# read blocks of audio data with overlap from sound variable
for num in range(0, len(sound) // (samples_per_block - overlap)):
start = num * (samples_per_block - overlap)
end = start + samples_per_block
block = sound[start:end]
block_date = recording_date + datetime.timedelta(seconds=num * BLOCK_SECONDS)
labels = rfftfreq(len(block), d=1/samplerate)
complex_amplitudes = rfft(block)
absolute_amplitudes = np.abs(complex_amplitudes)
median_amplitude = np.median(absolute_amplitudes)
amplitudes = absolute_amplitudes / median_amplitude
# get amplitudes only between 100 and 1000 Hz
@ -79,7 +88,7 @@ def process_recording(filename):
last_detection_at = block_date
print("🔊")
print(f'{block_date}: {max_amplitude:.1f}rDB @ {max_freq:.1f}Hz (noise {noise:.3f}rDB, median {median_amplitude:.3f}rDB)')
print(f'{block_date}: {max_amplitude:.1f}rDB @ {max_freq:.1f}Hz (noise {noise:.3f}rDB)')
else:
is_detecting = False