A MATLAB pipeline for analyzing neural calcium imaging data through autocorrelation analysis, oscillation detection, and statistical validation using block-bootstrapping.
This pipeline processes calcium imaging traces to identify oscillatory patterns in neural activity. It performs detrending, autocorrelation analysis, frequency domain analysis via FFT, and rigorous statistical testing with false discovery rate (FDR) correction.
- Automated batch processing of multiple trace files
- Interactive neuron selection based on custom tags
- Flexible time window selection for analysis
- Polynomial detrending to remove slow drift
- Autocorrelation analysis to detect rhythmic activity
- FFT-based frequency analysis for oscillation characterization
- Block-bootstrapping for robust statistical testing
- FDR correction for multiple comparisons
- Automatic figure generation and export
- MATLAB R2020b or later (uses argument validation syntax)
- Statistics and Machine Learning Toolbox (for
mafdr) - Signal Processing Toolbox (for autocorrelation and FFT functions)
- Clone this repository:
git clone https://github.com/yourusername/autocorrelation-pipeline.git- Add the repository to your MATLAB path:
addpath(genpath('path/to/autocorrelation-pipeline'))autocorrelation-calcium/
├── Main/
│ ├── Main_Autocorrelogram_script_standard_wrapped.m # Main pipeline script for multifile analysis
│
├── Functions/
│ ├── data_extraction.m # Load trace data and metadata
│ ├── select_neurons.m # Interactive neuron selection
│ ├── time_selection.m # Interactive time window selection
│ ├── polyfit_detrending.m # Polynomial detrending
│ ├── mp_autocorrelogram.m # Autocorrelation analysis
│ ├── autocorrelogram_freq_fft.m # FFT frequency analysis of the autocorrelogram
│ ├── shiftlinearcorr.m # Block-bootstrapping statistics
│ └── blockbootstrp.m # Block bootstrap resampling
└── README.md # This file
- Organize your data files with the naming convention:
*traces.csv - Run the main script:
Main_Autocorrelogram_script_standard_wrapped.m - Select the folder containing your trace files
- Follow the interactive prompts to select neurons and time windows
The script expects CSV files with the following structure:
| Time | Neuron_1 | Neuron_2 | Neuron_3 | ... |
|---|---|---|---|---|
| [Header Row] | tag_A | tag_B | tag_A | ... |
| 0.0 | 0.15 | 0.23 | 0.18 | ... |
| 0.033 | 0.17 | 0.25 | 0.19 | ... |
| ... | ... | ... | ... | ... |
- Row 1: Variable names (neuron identifiers) - becomes table header
- Row 2: Neuron tags/categories for filtering
- Row 3+: Time series data (first column = time, subsequent columns = neural traces)
For each processed file, the pipeline generates:
Contains the following columns:
Names- Neuron identifiersmax_autocorr_coeff- Maximum autocorrelation coefficientoscillation_freq- Dominant oscillation frequency (Hz)oscillation_freq_fft- FFT-derived oscillation frequency (Hz)outside_CI- Bootstrap confidence interval test result1= above upper CI bound-1= below lower CI bound0= within CI
lower_CI- Lower confidence interval boundaryupper_CI- Upper confidence interval boundaryp_value- Bootstrap p-valueBH_correction- Benjamini-Hochberg FDR significance (true/false)
All figures are automatically saved as PDFs in the same folder as input data:
traces.pdf- Raw and selected time windows- Detrending visualization
- Autocorrelogram plots
- FFT frequency analysis plots
Edit these variables in Main_Autocorrelogram_script.m:
% File selection
file_pattern = '**\*traces.csv'; % File search pattern
% Statistical parameters
fdr_correction = true; % Enable/disable FDR correction
fdr_q = 0.05; % FDR significance threshold (default: 5%)Modify parameters in function calls for fine-tuning:
[outside_CI(j), boundaries_CI(j,:), p_values(j)] = shiftlinearcorr(...
detrended(:,j+1), detrended(:,1), coeff(j), ...
"blocksize", 50, ... % Block size for bootstrap
"pValueCorrection", false); % Conservative p-value correctionAdditional parameters available in shiftlinearcorr.m:
CI- Confidence interval percentiles (default:[99 1])iterations- Number of bootstrap iterations (default:1000)
Modify in polyfit_detrending.m function call
Modify in mp_autocorrelogram.m function call
Modify in autocorrelogram_freq_fft.m function call
Input CSV Files
↓
[data_extraction]
↓
[select_neurons] - Interactive tag-based filtering
↓
[time_selection] - Interactive time window selection
↓
[polyfit_detrending] - Remove slow drift
↓
[mp_autocorrelogram] - Compute autocorrelation
↓
[autocorrelogram_freq_fft] - Frequency analysis
↓
[shiftlinearcorr] - Block-bootstrap testing
↓
[mafdr] - FDR correction
↓
Export Results & Figures
- Computes autocorrelation to detect rhythmic patterns
- Identifies dominant oscillation frequencies
- Normalizes coefficients for comparison across neurons
- Assesses statistical significance of autocorrelation coefficients
- Preserves temporal structure through block resampling
- Default settings:
- Block size: 50 samples
- Iterations: 1000
- Confidence interval: 99th/1st percentiles
- P-value calculation:
- Standard:
p = sum(|bootstrap| >= |coeff|) / N - Conservative (optional):
p = (sum(|bootstrap| >= |coeff|) + 1) / (N + 1)
- Standard:
- Benjamini-Hochberg procedure for multiple comparison correction
- Controls expected proportion of false positives
- Default q-value: 0.05 (5% FDR)
- Can be disabled by setting
fdr_correction = falsed in each file's directory
Loads CSV data and extracts neuron tags.
- Input: Path to CSV file
- Output: Data table and tag array
Interactive dialog for neuron selection based on tags.
- Input: Trace data and tag array
- Output: Selected data, names, breakpoints, and time vector
Interactive selection of time windows for analysis.
- Input: Data, time vector, and breakpoints
- Output: Selected data and updated time vector
Removes polynomial trends from data.
- Input: Time series data
- Output: Detrended data, trend, and figure handle
Computes autocorrelation and identifies oscillations.
- Input: Detrended data
- Output: Coefficients, frequencies, correlogram, and figure
FFT-based frequency analysis of autocorrelation.
- Input: Correlogram data
- Output: FFT frequencies, frequency domain data, and figure
Block-bootstrap statistical testing.
- Input: Signal, time, coefficient, and bootstrap options
- Output: CI test results, boundaries, and p-value
Block bootstrap resampling preserving temporal structure.
- Input: Signal, block size, number of samples
- Output: Bootstrapped samples
"No files found matching pattern"
- Verify files end with
traces.csv - Check the selected folder path
- Ensure the file pattern matches your naming convention
"Table variable names must be nonempty text"
- Verify Row 1 contains valid column headers
- Ensure no empty cells in the header row
- Check for numeric-only headers (will be converted to strings)
"No tag selected. Using all neurons."
- This is a warning, not an error
- All neurons will be included in the analysis
- To filter, select tags in the dialog
Memory errors with large datasets
- Reduce bootstrap iterations (edit line ~30)
- Process smaller time windows
- Process files individually instead of batch
- Close other MATLAB figures/variables
Figures not saving
- Check folder write permissions
- Ensure sufficient disk space
- Verify
SavingFolderparameter is set correctly