EMG Analysis¶
Tools for analyzing electromyography (EMG) signals, including frequency analysis and gape detection.
Overview¶
The EMG analysis pipeline uses BSA/STFT (Bayesian Spectrum Analysis and Short-Time Fourier Transform) for frequency analysis.
Shared Setup¶
emg_filter.py¶
Filter EMG signals before analysis.
Usage:
Filtering Steps:
- Differencing EMG channels within CAR groups (if multiple channels)
- Highpass filtering at 300 Hz (2nd order Butterworth)
- Rectification (absolute value)
- Lowpass filtering at 15 Hz for envelope extraction
Output:
- Filtered EMG signals saved to HDF5 file
- Filter parameters logged
BSA/STFT Branch¶
Frequency-based analysis of EMG signals.
emg_freq_setup.py¶
Configure parameters for frequency analysis.
Usage:
Parameters:
- Frequency bands of interest
- Window sizes
- Overlap parameters
- Output directories
Parallel Processing¶
Run frequency analysis in parallel:
Note: This script is automatically generated by emg_freq_setup.py and uses GNU parallel for distributed processing.
The script:
- Divides trials across processors
- Runs BSA/STFT on each subset
- Saves intermediate results
emg_freq_post_process.py¶
Aggregate and process frequency analysis results.
Usage:
Processing Steps:
- Combine results from parallel jobs
- Normalize power spectra
- Calculate summary statistics
- Identify significant frequency changes
emg_freq_plot.py¶
Generate visualizations of frequency analysis.
Usage:
Plots Generated:
- Spectrograms
- Power spectrum time courses
- Frequency band comparisons
- Trial-averaged responses
EMG Data Structure¶
HDF5 Organization¶
data.h5
├── emg_data/
│ ├── dig_in_<N>/
│ │ ├── emg_array # Raw EMG data
│ │ └── processed_emg/
│ │ ├── <car>_emg_filt # Highpass filtered signal
│ │ └── <car>_emg_env # Envelope (lowpass filtered)
│ ├── ind_electrode_map # Electrode mapping
│ └── emg_sig_trials # Significant trial indicators
├── emg_BSA_results/ # BSA/STFT frequency analysis
│ ├── omega # Frequency values
│ ├── <car>/
│ │ └── taste<N>_p # Power spectrum per taste
│ ├── gapes # Detected gape events
│ ├── licking # Detected licking events
│ └── emg_BSA_results_final # Combined results
Configuration¶
emg_params.json¶
EMG analysis parameters:
{
"emg_env": "/path/to/conda/envs/emg_env",
"stft_params": {
"max_freq": 20,
"time_range_tuple": [0, 7],
"Fs": 1000,
"signal_window": 400,
"window_overlap": 399
},
"use_BSA": true
}
emg_env: Path to conda environment for EMG processingstft_params: Short-time Fourier transform parametersmax_freq: Maximum frequency to analyze (Hz)time_range_tuple: Time window for analysis (seconds)Fs: Sampling frequency (Hz)signal_window: Window size for STFT (samples)window_overlap: Overlap between windows (samples)
use_BSA: Whether to use Bayesian Spectrum Analysis (true) or STFT (false)
Usage Examples¶
Complete BSA/STFT Workflow¶
# Filter EMG signals
python emg/emg_filter.py
# Setup frequency analysis
python emg/emg_freq_setup.py
# Run parallel analysis
bash blech_emg_jetstream_parallel.sh
# Post-process results
python emg/emg_freq_post_process.py
# Generate plots
python emg/emg_freq_plot.py
Programmatic Access¶
import tables
import numpy as np
# Load filtered EMG data
with tables.open_file('data.h5', 'r') as hf5:
emg_filtered = hf5.root.emg.filtered[:]
# Load gape events
with tables.open_file('data.h5', 'r') as hf5:
gape_times = hf5.root.emg.gapes.onset_times[:]
gape_durations = hf5.root.emg.gapes.durations[:]
# Analyze gape timing
print(f"Detected {len(gape_times)} gapes")
print(f"Mean duration: {np.mean(gape_durations):.2f} ms")
Analysis Tips¶
Frequency Analysis¶
- Use appropriate frequency bands for your species/preparation
- Adjust window size based on temporal resolution needs
- Consider trial-to-trial variability
Quality Control¶
- Inspect filtered signals visually
- Check for artifacts
- Validate detected events manually for subset of trials
References¶
- Mukherjee, N., Wachutka, J., & Katz, D. B. (2019). Impact of precisely-timed inhibition of gustatory cortex on taste behavior depends on single-trial ensemble dynamics. eLife, 8, e45968.
- Li JX, Maier JX, Reid EE, Katz DB. Sensory Cortical Activity Is Related to the Selection of a Rhythmic Motor Action Pattern. J Neurosci. 2016 May 18;36(20):5596-607. doi: 10.1523/JNEUROSCI.3949-15.2016. PMID: 27194338; PMCID: PMC4871991.