Python tools for recording four-channel hydrophone data with Saleae Logic 2 and classifying whether an acoustic source is nearby.
The current pipeline is based on Logic 2 data recording and file format. It supports live voting, offline analysis of saved captures, and training a nearby/far Random Forest model.
Logic 2 capture
-> Logic 2 capture directory
-> HydrophoneArray
-> TOA envelope analysis
-> nearby model inference
-> result dictionaries or analysis CSV
Hydrophones are processed independently
- Saleae Logic 2 and the Saleae automation server enabled
- A Logic 2 device, or
USE_MOCK_DEVICE=Truewhen using mock mode
Install Python dependencies from the repository root:
python -m pip install -r requirements.txtEdit the configuration at the bottom of scripts/recorder.py, then run:
python scripts/recorder.pyThe recorder creates a timestamped test directory containing one Logic 2 capture directory per epoch:
data/
7.9.2026/
H0_20ft_32kHz_2026-07-09--13-39-18/
H0_20ft_32kHz_epoch_0/
... Logic 2 channel files ...
H0_20ft_32kHz_epoch_1/
...
recorder.py captures binary Logic 2 data for channels 0 through 3. Set is_mock=True in its configuration when a simulated device is required.
scripts/controller.py captures continuously, analyzes completed captures in worker processes, and stops after enough matching votes are collected.
Run it from the repository root:
python scripts/controller.pyImportant configuration values are near the top of the file:
SELECTED: hydrophones to analyze, for example[True, False, False, False]for H0 only.ANALYZERS: the configured TOA and nearby analyzers.SAMPLING_FREQ: capture sample rate.CAPTURE_TIME: duration of each capture.MAX_CONCURRENT_ANALYSIS_PROCESSES: maximum number of simultaneous sample analyses.USE_MOCK_DEVICE: use the Logic 2 simulation device instead of hardware.
The active nearby model is loaded from:
scripts/artifacts/proximity_classifier_10ft_threshold_2026-04-12--23-04-00.pkl
The Logic 2 adapter currently contains a Linux AppImage path in scripts/logic/logic2.py. Update that path for a different deployment environment before using real hardware there.
Use scripts/parser.py to analyze previously recorded test directories and write a CSV report:
- Edit
paths_to_analyzeat the bottom of the file. - Run:
python scripts/parser.pyReports are written to analysis/analysis_<timestamp>.csv.
Each row represents one epoch and includes:
- Capture path and folder-derived ground truth metadata.
- Overall validity and nearby result.
- Per-hydrophone TOA and validation reason.
- Nearby prediction and confidence.
- The three model features.
The active model uses a 10-foot boundary:
nearby: <= 10 ft
far: > 10 ft
The model uses three H0 features:
RAW_spectral_flatnessFILTERED_spectral_centroid_hzRAW_rise_time_ms
Train a new model from an analysis CSV with:
python scripts/general_utilities/train_nearby_model.pyThe training script reads the configured CSV, keeps rows where ALL_VALID is true, performs five-fold stratified cross-validation, trains a Random Forest, and saves the serialized model package to scripts/artifacts/.
Check predictions against folder-distance ground truth with:
python scripts/general_utilities/check_accuracy.pyUpdate the CSV path at the bottom of that script before running it.
TOAEnvelopeAnalyzer applies a Butterworth bandpass filter, computes a Hilbert envelope, and selects the first threshold crossing as the time of arrival. It falls back to the largest envelope peak when no threshold crossing is found.
The current GarbageDetector is used by the TOA analyzer. A selected hydrophone is invalid when:
- Its signal peak is below the configured raw-signal threshold.
- Its TOA is within the front recording margin.
- Its TOA is within the end recording margin.
The default controller thresholds are a raw-signal threshold of 0.5 and timing margins of 0.1 seconds.
scripts/
controller.py Live capture, analysis, and voting
recorder.py Batch Logic 2 acquisition
parser.py Offline capture analysis and CSV output
analyzers/ TOA and nearby analysis
hydrophones/ Logic 2 loading and hydrophone data containers
logic/logic2.py Saleae Logic 2 interface
general_utilities/ Model training and accuracy checks
artifacts/ Serialized nearby models
data/ Recorded Logic 2 captures
analysis/ Generated analysis CSV files
- Logic 2 only.
- Four-channel capture support.
- Per-hydrophone analysis with simple result aggregation.
- Default runtime analysis uses H0 only.
- No cross-hydrophone timing, localization, triangulation, or array-level signal comparison yet.