diff --git a/process_chunks.py b/process_chunks.py index ab431cb..aac9cc1 100755 --- a/process_chunks.py +++ b/process_chunks.py @@ -22,6 +22,17 @@ OVERTONE_FREQ = TARGET_FREQ * 2 NFFT = 32768 SKIP_SECONDS = 10 +def detect_event(chunk, samplerate): + freqs, times, Sxx = scipy.signal.spectrogram(chunk, samplerate, nperseg=NFFT) + idx_base = np.where((freqs >= TARGET_FREQ - TOLERANCE) & (freqs <= TARGET_FREQ + TOLERANCE))[0] + idx_oct = np.where((freqs >= OVERTONE_FREQ - OVERTONE_TOLERANCE) & (freqs <= OVERTONE_FREQ + OVERTONE_TOLERANCE))[0] + if len(idx_base) == 0 or len(idx_oct) == 0: + return False + base_energy = np.mean(Sxx[idx_base]) + oct_energy = np.mean(Sxx[idx_oct]) + total_energy = np.mean(Sxx, axis=0).max() + return base_energy > THRESHOLD_BASE * total_energy and oct_energy > THRESHOLD_OCT * total_energy + def process_chunk(filename): input_path = os.path.join(OUTDIR, filename) print(f"🔍 Verarbeite {input_path}...") @@ -36,22 +47,11 @@ def process_chunk(filename): padding_before = int(CLIP_PADDING_BEFORE * samplerate) padding_after = int(CLIP_PADDING_AFTER * samplerate) - def detect_event(chunk): - freqs, times, Sxx = scipy.signal.spectrogram(chunk, samplerate, nperseg=NFFT) - idx_base = np.where((freqs >= TARGET_FREQ - TOLERANCE) & (freqs <= TARGET_FREQ + TOLERANCE))[0] - idx_oct = np.where((freqs >= OVERTONE_FREQ - OVERTONE_TOLERANCE) & (freqs <= OVERTONE_FREQ + OVERTONE_TOLERANCE))[0] - if len(idx_base) == 0 or len(idx_oct) == 0: - return False - base_energy = np.mean(Sxx[idx_base]) - oct_energy = np.mean(Sxx[idx_oct]) - total_energy = np.mean(Sxx, axis=0).max() - return base_energy > THRESHOLD_BASE * total_energy and oct_energy > THRESHOLD_OCT * total_energy - i = 0 last_event = -skip_samples while i + chunk_samples <= len(data): chunk = data[i:i+chunk_samples] - if i - last_event >= skip_samples and detect_event(chunk): + if i - last_event >= skip_samples and detect_event(chunk, samplerate): clip_start = max(0, i - padding_before) clip_end = min(len(data), i + chunk_samples + padding_after) clip = data[clip_start:clip_end]