Anomaly-Driven Outlier Extractor — Acoustic Anomaly Extraction
This script extracts the most acoustically unusual moments from a sound. Praat builds a frame-by-frame feature table; a Python backend scores each frame for "outlier-ness" and returns a new Sound containing only the anomalous material.
What this does
This script implements anomaly-driven outlier extraction. Praat measures a frame-level feature table on one common time grid, hands it to the Python backend together with the audio, and receives back a Sound containing only the acoustical outliers — the frames the detector could not explain from the rest of the file.
Key Features:
- 6 Presets — Transient hunt, Pitch instability, Broadband oddity, Granular cloud, Anomaly texture, Anomaly memory
- 3 Detection Algorithms — IsolationForest, Mahalanobis, Autoencoder
- 5 Concatenation Modes — chronological, sorted, granular, texture, memory
- 5 Acoustic Features — Pitch, Intensity, Spectral centroid, Spectral flux, Formants F1/F2/F3
- Texture / Memory modes — output length decoupled from anomaly duration
- Self-calibrating features — all features normalised per file
- Visualisation — input waveform with outlier regions, anomaly score curve, segment map, output waveform, spectrograms
Outlier_threshold is a fraction of frames, not a score cut. 0.05 keeps the top 5% most anomalous frames. Raw anomaly scores are not comparable across algorithms or files, so an absolute score cut would behave differently for every input. The threshold is applied as a quantile: score_cut = quantile(scores, 1 - threshold).
Quick start
- In Praat, select exactly one Sound object.
- Run script… →
Anomaly_Outlier_Extractor.praat. - Choose a preset from the dropdown (6 options, plus Custom).
- If Custom, set Features (space-separated keywords: pitch intensity centroid flux formants deltas all), Window_step_s (e.g., "0.025 0.010"), Pitch_range_Hz (e.g., "75 600").
- Select Algorithm (IsolationForest, Mahalanobis, Autoencoder).
- Set Outlier_threshold (top fraction of frames to extract).
- Choose Concat_mode (chronological, sorted, granular, texture, memory).
- For texture/memory modes, set Texture_length_s (output duration).
- Click OK — Praat extracts features, Python runs detection, assembles output.
pip install numpy scipy pandas soundfile scikit-learn. Autoencoder uses PyTorch if installed, otherwise a numpy MLP fallback. The analysis is always mono (To Spectrum and other commands require mono), but the exported audio preserves the original channel count. The texture/memory modes decouple output length from anomaly duration — a 70 ms anomaly can populate a 30-second canvas.
6 Presets
| Preset | Features | Window/Step (ms) | Algorithm | Threshold | Mode | Character |
|---|---|---|---|---|---|---|
| Transient hunt | intensity, centroid, flux, deltas | 20 / 5 | IsolationForest | 0.03 | chronological | Short frames, delta-heavy — catches attacks |
| Pitch instability | pitch, intensity, formants, deltas | 40 / 10 | Mahalanobis | 0.08 | chronological | Voice-source features, Mahalanobis |
| Broadband oddity | intensity, centroid, flux, formants, deltas | 30 / 10 | Autoencoder | 0.05 | sorted | Full spectral feature set, autoencoder |
| Granular cloud | pitch, intensity, centroid, flux, deltas | 25 / 10 | IsolationForest | 0.15 | granular | Permissive threshold, short grains, OLA |
| Anomaly texture | pitch, intensity, centroid, flux, deltas | 25 / 10 | IsolationForest | 0.12 | texture | Weighted stochastic cloud, fixed canvas length |
| Anomaly memory | pitch, intensity, centroid, flux, deltas | 25 / 10 | IsolationForest | 0.08 | memory | Each outlier returns as a motif across the canvas |
3 Detection Algorithms
IsolationForest
Scikit-learn ensemble — isolates outliers by random partitioning. Frames that are easy to isolate (short path length) are anomalous. Score = negated average path length, mapped to [0,1].
Use: General-purpose, robust to many feature types. Fast on medium-sized datasets.
Mahalanobis
Shrinkage covariance — squared Mahalanobis distance under a regularised covariance matrix, mapped to [0,1] by log-compression. The chi² tail probability is reported as a diagnostic.
Use: When features are approximately Gaussian. Good for pitch/formant analysis.
Autoencoder
Neural reconstruction error — a small MLP autoencoder learns to reconstruct normal frames. Frames with high reconstruction error are outliers.
Use: Non-linear feature relationships. PyTorch if installed, otherwise numpy MLP fallback.
Score transformation
All algorithms produce raw scores that are min-max mapped to [0,1]:
A(t) = (raw - min(raw)) / (max(raw) - min(raw))
Higher values = more anomalous. The threshold is applied as a quantile: score_cut = quantile(A, 1 - threshold).
Log-compression: For Mahalanobis and Autoencoder, raw distances are log-compressed before scaling so that one extreme frame doesn't compress all others toward 0.
5 Concatenation Modes
chronological sequential
Outlier slices are placed in the order they appear in the original file. The output is a "highlight reel" of anomalies in time order.
sorted by score
Outlier slices are placed in order of anomaly score (quietest anomaly to loudest). A crescendo of weirdness.
granular cloud
Outlier slices are overlap-added with random jitter, creating a continuous, cloud-like texture.
texture corpus
Outliers become a corpus. Slices are drawn with replacement, weighted by anomaly score, until the canvas is full. Output length is a parameter.
memory motif
Each outlier gets a recurrence budget from its score, and its occurrences are spread evenly across the canvas. A sharp anomaly comes back as a motif.
Applications
Outlier highlight reel (chronological)
Use case: Extract all the "interesting" moments from a long recording — anomalies in order.
Settings: Transient hunt preset, chronological mode. The output is a concatenation of the most unusual frames.
Anomaly crescendo (sorted)
Use case: Hear anomalies from least to most unusual — a compositional arc.
Settings: Broadband oddity preset, sorted mode. The output builds from subtle to extreme outliers.
Anomaly cloud / texture (texture, memory)
Use case: Generate a long texture from a few short anomalies — create a "memory" of the outliers.
Settings: Anomaly texture preset, texture_length_s = 30.0. The output is a 30-second weighted stochastic cloud of the anomalies. Anomaly memory spreads each outlier as a recurring motif.
Workflow: Speech recording → Pitch instability outliers
Source: Spoken word recording.
Settings: Pitch instability preset, chronological mode, threshold=0.08.
Result: The output contains only the frames with pitch instability — the "unusual" vocal moments, extracted in order.
Workflow: Field recording → Anomaly texture
Source: Long field recording (birds, water, wind).
Settings: Anomaly texture preset, texture_length_s = 60.0, bias=1.5.
Result: A 60-second texture made from the unusual moments in the recording — the anomalies become a continuous soundscape.
Workflow: Instrumental solo → Granular cloud
Source: Instrumental solo (guitar, voice, etc.).
Settings: Granular cloud preset, threshold=0.15, granular_overlap=0.70.
Result: The anomalous frames are overlap-added into a granular cloud — a continuous, shimmering texture.
• No outliers extracted: Lower the threshold (e.g., 0.05 → 0.15). If the sound is very uniform, there may be few anomalies. Try a different algorithm or feature set.
• Output is very short: In chronological/sorted/granular modes, output length depends on anomaly duration. Use texture/memory modes to specify a fixed output length.
• Texture output is repetitive: Reduce Anomaly_bias (lower = more uniform) or increase Anti_repeat (cooldown for recently used slices).
• Memory output loops too regularly: Increase Granular_jitter (drift) to spread occurrences more randomly.
• Autoencoder is slow: The numpy MLP fallback is used if PyTorch is not installed. Install PyTorch for faster training:
pip install torch.• Features not recognised: Use keywords: pitch, intensity, centroid, flux, formants, deltas, all. The script ignores unknown words.
Visualisation
- Input waveform — with outlier regions marked (red bars at the bottom, vertical lines at boundaries).
- Input spectrogram — full frequency content.
- Anomaly score curve A(t) — the score over time, with the cut threshold shown (red line).
- Segment map — colour-coded bars showing where each outlier came from in the original file (blue = weaker, red = stronger).
- Output waveform — the assembled outlier sound.
- Output spectrogram — spectral content of the outliers.
- Summary panel — frames, features, algorithm, threshold, flagged frames, segments, duration change, RMS.