scientific-skills/neurokit2/references/rsp.md
Respiratory signal processing in NeuroKit2 enables analysis of breathing patterns, respiratory rate, amplitude, and variability. Respiration is closely linked to cardiac activity (respiratory sinus arrhythmia), emotional state, and cognitive processes.
Automated processing of respiratory signals with peak/trough detection and feature extraction.
signals, info = nk.rsp_process(rsp_signal, sampling_rate=100, method='khodadad2018')
Pipeline steps:
Returns:
signals: DataFrame with:
RSP_Clean: Filtered respiratory signalRSP_Peaks, RSP_Troughs: Extrema markersRSP_Rate: Instantaneous breathing rate (breaths/min)RSP_Amplitude: Breath-to-breath amplitudeRSP_Phase: Inspiration (0) vs. expiration (1)RSP_Phase_Completion: Phase completion percentage (0-1)RSP_RVT: Respiratory volume per timeinfo: Dictionary with peak/trough indicesMethods:
'khodadad2018': Khodadad et al. algorithm (default, robust)'biosppy': BioSPPy-based processing (alternative)Remove noise and smooth respiratory signal.
cleaned_rsp = nk.rsp_clean(rsp_signal, sampling_rate=100, method='khodadad2018')
Methods:
1. Khodadad2018 (default):
2. BioSPPy:
3. Hampel filter:
cleaned_rsp = nk.rsp_clean(rsp_signal, sampling_rate=100, method='hampel')
Typical respiratory frequency:
Identify inhalation troughs and exhalation peaks in respiratory signal.
peaks, info = nk.rsp_peaks(cleaned_rsp, sampling_rate=100, method='khodadad2018')
Detection methods:
'khodadad2018': Optimized for clean signals'biosppy': Alternative approach'scipy': Simple scipy-based detectionReturns:
RSP_Peaks: Indices of exhalation peaks (maximum points)RSP_Troughs: Indices of inhalation troughs (minimum points)Respiratory cycle definition:
Low-level peak detection with multiple algorithm options.
peaks_dict = nk.rsp_findpeaks(cleaned_rsp, sampling_rate=100, method='scipy')
Methods:
'scipy': Scipy's find_peaksUse case:
Correct detected peak/trough anomalies (e.g., missed or false detections).
corrected_peaks = nk.rsp_fixpeaks(peaks, sampling_rate=100)
Corrections:
Compute instantaneous breathing rate (breaths per minute).
rate = nk.rsp_rate(peaks, sampling_rate=100, desired_length=None)
Method:
Typical values:
Compute breath-to-breath amplitude (peak-to-trough difference).
amplitude = nk.rsp_amplitude(cleaned_rsp, peaks)
Interpretation:
Clinical relevance:
Determine inspiration/expiration phases and completion percentage.
phase, completion = nk.rsp_phase(cleaned_rsp, peaks, sampling_rate=100)
Returns:
RSP_Phase: Binary (0 = inspiration, 1 = expiration)RSP_Phase_Completion: Continuous 0-1 indicating phase progressUse cases:
Analyze breath symmetry patterns (peak-trough balance, rise-decay timing).
symmetry = nk.rsp_symmetry(cleaned_rsp, peaks)
Metrics:
Interpretation:
Respiratory Rate Variability - analogous to heart rate variability.
rrv_indices = nk.rsp_rrv(peaks, sampling_rate=100)
Time-domain metrics:
RRV_SDBB: Standard deviation of breath-to-breath intervalsRRV_RMSSD: Root mean square of successive differencesRRV_MeanBB: Mean breath-to-breath intervalFrequency-domain metrics:
Interpretation:
Recording duration:
Respiratory Volume per Time - fMRI confound regressor.
rvt = nk.rsp_rvt(cleaned_rsp, peaks, sampling_rate=100)
Calculation:
Use cases:
Reference:
Respiratory Amplitude Variability indices.
rav = nk.rsp_rav(amplitude, sampling_rate=100)
Metrics:
Interpretation:
Automatically select event-related or interval-related analysis.
analysis = nk.rsp_analyze(signals, sampling_rate=100)
Mode selection:
Analyze respiratory responses to specific events/stimuli.
results = nk.rsp_eventrelated(epochs)
Computed metrics (per epoch):
RSP_Rate_Mean: Average breathing rate during epochRSP_Rate_Min/Max: Minimum/maximum rateRSP_Amplitude_Mean: Average breath depthRSP_Phase: Respiratory phase at event onsetUse cases:
Analyze extended respiratory recordings.
results = nk.rsp_intervalrelated(signals, sampling_rate=100)
Computed metrics:
RSP_Rate_Mean: Average breathing rateRSP_Rate_SD: Variability in rateRSP_Amplitude_Mean: Average breath depthRecording duration:
Use cases:
Generate synthetic respiratory signals for testing.
synthetic_rsp = nk.rsp_simulate(duration=60, sampling_rate=100, respiratory_rate=15,
method='sinusoidal', noise=0.1, random_state=42)
Methods:
'sinusoidal': Simple sinusoidal oscillation (fast)'breathmetrics': Advanced realistic breathing model (slower, more accurate)Parameters:
respiratory_rate: Breaths per minute (default: 15)noise: Gaussian noise levelrandom_state: Seed for reproducibilityUse cases:
Visualize processed respiratory signal.
nk.rsp_plot(signals, info, static=True)
Displays:
Interactive mode: Set static=False for Plotly visualization
Strain gauge/piezoelectric belt:
Thermistor/thermocouple:
Capnography:
Impedance pneumography:
Irregular breathing:
Shallow breathing:
Movement artifacts:
Talking/coughing:
Standard workflow:
# 1. Clean signal
cleaned = nk.rsp_clean(rsp_raw, sampling_rate=100, method='khodadad2018')
# 2. Detect peaks/troughs
peaks, info = nk.rsp_peaks(cleaned, sampling_rate=100)
# 3. Extract features
rate = nk.rsp_rate(peaks, sampling_rate=100, desired_length=len(cleaned))
amplitude = nk.rsp_amplitude(cleaned, peaks)
phase = nk.rsp_phase(cleaned, peaks, sampling_rate=100)
# 4. Comprehensive processing (alternative)
signals, info = nk.rsp_process(rsp_raw, sampling_rate=100)
# 5. Analyze
analysis = nk.rsp_analyze(signals, sampling_rate=100)
Respiratory-cardiac integration:
# Process both signals
ecg_signals, ecg_info = nk.ecg_process(ecg, sampling_rate=1000)
rsp_signals, rsp_info = nk.rsp_process(rsp, sampling_rate=100)
# Respiratory sinus arrhythmia (RSA)
rsa = nk.hrv_rsa(ecg_info['ECG_R_Peaks'], rsp_signals['RSP_Clean'], sampling_rate=1000)
# Or use bio_process for multi-signal integration
bio_signals, bio_info = nk.bio_process(ecg=ecg, rsp=rsp, sampling_rate=1000)
Psychophysiology:
Anxiety and panic disorders:
Sleep medicine:
Cardiorespiratory coupling:
Neuroimaging:
Meditation and mindfulness:
Athletic performance:
Breathing rate:
Breathing amplitude:
Respiratory patterns: