usingNeuroAnalyzerusingCairoMakieCairoMakie.activate!(type="png")# Quarto sets a default figure size (672 × 480 px), which overrides the sizes of# NeuroAnalyzer figures: remove itdelete!(CairoMakie.Makie.CURRENT_DEFAULT_THEME, :resolution)usingDataFramesusingStatisticsNeuroAnalyzer.progress_bar =false# Makie figure and axis settings matching NeuroAnalyzer plotsfig_size = (900, 450)ax_style = (titlesize =18, xlabelsize =18, ylabelsize =18, xticklabelsize =12, yticklabelsize =12, xticks =LinearTicks(10), xminorticksvisible =true, xminorticks =IntervalsBetween(10), xautolimitmargin = (0, 0), yautolimitmargin = (0.1, 0.1))# EEG recording with an ECG channel (1000 s)eeg =load_neuro("files/eeg/eeg.hdf")
Heart Rate Variability (HRV) is the physiological phenomenon of variation in the time interval between heartbeats. It reflects the autonomic nervous system’s (ANS) regulation of heart activity and is influenced by physiological and psychological factors. HRV is widely used in clinical, research, and wellness applications to assess autonomic balance, stress, cardiovascular health, and emotional regulation.
HRV measures the fluctuations in the time intervals between consecutive heartbeats (R-R intervals in ECG).
It is not the same as heart rate (which measures the average number of beats per minute). Instead, HRV reflects the complex interplay between the sympathetic (fight-or-flight) and parasympathetic (rest-and-digest) branches of the autonomic nervous system.
HRV provides insights into:
Autonomic nervous system (ANS) function: Balance between sympathetic and parasympathetic activity.
Cardiovascular health: Predicts risk of heart disease, hypertension, and arrhythmias.
Stress and emotional regulation: Lower HRV is associated with chronic stress, anxiety, and depression.
Physical fitness: Athletes often exhibit higher HRV, indicating better recovery and performance.
Sleep quality: HRV patterns can indicate sleep stages and quality.
Aging: HRV tends to decrease with age.
In NeuroAnalyzer, HRV analysis is performed using ECG data, where the R-peaks of the ECG signal are detected to compute NN intervals (the time between two successive normal heartbeats). Below is a detailed explanation of HRV, its measurement, and the time-domain parameters used for analysis.
HRV depends strongly on the duration of the recording, age, fitness, posture and breathing, so values should only be compared between recordings of the same duration made under the same conditions. Approximate values for healthy adults:
Parameter
Short-term (5 min, at rest)
24-hour recording
SDNN
~30–60 ms
~100–180 ms
RMSSD
~20–60 ms
~20–50 ms
pNN50
~5–30%
~5–25%
For a comprehensive HRV analysis, time-domain metrics are often combined with:
LF/HF Ratio: Traditionally interpreted as the sympatho-vagal balance, although this interpretation is disputed (LF power reflects mainly baroreflex activity).
Non-Linear Metrics:
Poincaré Plot: Visualizes short-term and long-term HRV.
Sample Entropy (SampEn): Measures complexity of HRV.
Detrended Fluctuation Analysis (DFA): Assesses fractal properties of HRV.
HRV Analysis in NeuroAnalyzer
Detecting R-peaks
hrv_detect() finds the ECG channel of the object, detects the R-peaks (peaks higher than the mean + 2 SD of the signal, at least 250 ms apart) and returns their positions (r_idx, in samples) and the intervals between them (nn_seg, in ms):
All R-peaks are detected correctly. There is no QRS complex at about 7 seconds (a gap in the signal or a non-conducted beat), so the interval between the peaks at 6.1 and 7.9 s is about twice as long as the others.
Checking the Intervals
Missing or falsely detected beats and ectopic beats produce intervals that do not reflect the normal (sinus) rhythm. They are seen in the tachogram (the series of intervals):
# median of the 11 neighbouring intervalsnb(i) =max(1, i -5):min(length(nn_seg), i +5)local_med = [median(nn_seg[nb(i)]) for i ineachindex(nn_seg)]# intervals differing by > 20% from the local median are not normalok =abs.(nn_seg .- local_med) .<=0.2.* local_medprintln("Rejected intervals: ", count(!, ok), " of ", length(nn_seg))fig, ax, _ =scatterlines(nn_seg; color =:black, markersize =3, figure = (size = fig_size,), axis = (ax_style..., xlabel ="Interval", ylabel ="NN [ms]", title ="Tachogram"))scatter!(ax, findall(!, ok), nn_seg[.!ok]; color =:red, markersize =8)fig
Rejected intervals: 55 of 1059
The tachogram shows two kinds of non-normal intervals (red): short intervals (about 570 ms) followed by long ones (about 1300 ms), the typical pattern of premature (ectopic) beats followed by a compensatory pause, and isolated intervals about twice as long as normal (a missing beat). They are removed before the analysis; even a few of them inflate the variability measures considerably. Removing intervals is a simple correction; for frequency-domain analyses, which require an evenly sampled series, the rejected intervals are usually interpolated instead.
Time-domain Measures
hrv_analyze() calculates the time-domain measures from the intervals; NN50 and NN20 are counts, pNN50 and pNN20 are proportions (multiply by 100 for %):
With all intervals, RMSSD and SDNN are inflated several times by the detection errors; after removing them, the mean heart rate is 60000 / MENN ≈ 64 beats per minute, and the short-term variability (RMSSD ≈ 24 ms) is within the typical range at rest.