diff --git a/process_chunks.py b/process_chunks.py index c24a526..26c8a9b 100755 --- a/process_chunks.py +++ b/process_chunks.py @@ -10,6 +10,28 @@ import shutil OUTDIR = "chunks_unprocessed" PROCESSED_DIR = "chunks_processed" +CHUNK_SECONDS = 1 +TOLERANCE = 1 +OVERTONE_TOLERANCE = TOLERANCE * 2 +THRESHOLD_BASE = 0.5 +THRESHOLD_OCT = THRESHOLD_BASE / 10 +CLIP_PADDING_BEFORE = 1 +CLIP_PADDING_AFTER = 6 +TARGET_FREQ = 211 +OVERTONE_FREQ = TARGET_FREQ * 2 +NFFT = 32768 +SKIP_SECONDS = 10 + +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 def process_chunk(filename): input_path = os.path.join(OUTDIR, filename) @@ -20,33 +42,11 @@ def process_chunk(filename): if data.ndim > 1: data = data[:, 0] # nur Kanal 1 - CHUNK_SECONDS = 1 - TOLERANCE = 1 - THRESHOLD_BASE = 0.5 - THRESHOLD_OCT = THRESHOLD_BASE / 10 - CLIP_PADDING_BEFORE = 1 - CLIP_PADDING_AFTER = 6 - TARGET_FREQ = 211 - OVERTONE_FREQ = TARGET_FREQ * 2 - NFFT = 32768 - SKIP_SECONDS = 10 - chunk_samples = int(CHUNK_SECONDS * samplerate) skip_samples = int(SKIP_SECONDS * samplerate) 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 - TOLERANCE) & (freqs <= OVERTONE_FREQ + 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):