bootshorn_investigation/process
2025-06-01 11:10:12 +02:00

52 lines
No EOL
1.4 KiB
Python
Executable file

#!/usr/bin/env python3
import os
import concurrent.futures
import datetime
import numpy as np
import matplotlib.pyplot as plt
import soundfile
import scipy.signal
from scipy.fft import fft, fftfreq
import shutil
RECORDINGS_DIR = "recordings"
PROCESSED_RECORDINGS_DIR = "recordings/processed"
DETECTIONS_DIR = "events"
DETECT_FREQUENCY_FROM = 208
DETECT_FREQUENCY_TO = 214
CLIP_SECONDS = 3
THRESHOLD_BASE = 0.1
OCTAVE_FACTOR = 0.1
CLIP_PADDING_BEFORE = 1
CLIP_PADDING_AFTER = 6
def process_chunk(filename):
print('processing', filename)
path = os.path.join(RECORDINGS_DIR, filename)
info = soundfile.info(path)
samplerate = info.samplerate
blocksize = int(CLIP_SECONDS * samplerate)
print(info)
for num, block in enumerate(soundfile.blocks(path, blocksize=blocksize, overlap=int(blocksize*0.8))):
strengths = fft(block)
labels = fftfreq(len(block), d=1/samplerate)
# get the frequency with the highest strength
max_freq = labels[np.argmax(np.abs(strengths))]
if DETECT_FREQUENCY_FROM <= max_freq <= DETECT_FREQUENCY_TO:
print(f'{num}: {max_freq:.1f}')
def main():
os.makedirs(RECORDINGS_DIR, exist_ok=True)
os.makedirs(PROCESSED_RECORDINGS_DIR, exist_ok=True)
for file in os.listdir(RECORDINGS_DIR):
if file.endswith(".flac"):
process_chunk(file)
if __name__ == "__main__":
main()