## Overview

- Data source: `SingleRecordings/<id>/{hr.csv, rr.csv, timestamps.csv}`
- Scripts:
  - `plot_meditation_data.py`: per-recording plots (raw + moving averages + segment boxplots)
  - `aggregate_segments_analysis.py`: aggregate metrics/plots across recordings with exactly 4 marks (conditions)

# Results

![Aligned Average Heart Rate (HR) with Convergence Point](Plots/Aggregate/HR_average_aligned_with_convergence.png)


| Condition         | HR (bpm) | RR (ms) | RMSSD (ms) |
|-------------------|----------|---------|------------|
| Breathing Scene 1 | 72.61    | 843.33  | 52.70      |
| Spring Scene      | 72.33    | 860.78  | 56.95      |
| Summer Scene      | 72.50    | 862.00  | 41.15      |
| Autumn Scene      | 72.89    | 850.44  | 41.89      |
| Breathing Scene 2 | 73.78    | 839.94  | 42.59      |


### Timestamp Timing Analysis

This table presents the timing analysis for each recording, focusing on durations in seconds. It includes the time from the first heart rate (HR) recording to the first timestamp marker, the intervals between consecutive timestamp markers, and the time from the last timestamp marker to the end of the HR recording.

| Recording ID | Breathing Scene 1 (s) | Spring Scene (s) | Summer Scne (s) | Autumn Scene (s) | Breathing Scene 2 (s) |
|--------------|------------------------|------------------|------------------|------------------|---------------------------|
| 01           | 214.037                | 160.618          | 168.298          | 156.118          | 79.05                     |
| 02           | 189.707                | 196.964          | 162.457          | 145.152          | 101.075                   |
| 03           | 177.139                | 184.446          | 174.979          | 160.041          | 104.475                   |
| 04           | 253.685                | 167.817          | 169.903          | 150.954          | 103.016                   |
| 05           | 220.387                | 157.653          | 184.168          | 149.795          | 138.08                    |
| 06           | 253.251                | 167.564          | 177.214          | 153.651          | 96.502                    |
| 07           | 218.86                 | 163.38           | 177.967          | 154.913          | 122.008                   |
| 08           | 184.833                | 160.343          | 180.283          | 154.76           | 152.834                   |
| 10           | 182.163                | 164.495          | 184.733          | 144.337          | 113.889                   |

### Summary Statistics for Timestamp Timing Analysis

| Metric                      | Average (s) | Min (s) | Max (s) |
|-----------------------------|-------------|---------|---------|
| Time to first marker        | 210.451     | 177.139 | 253.685 |
| Interval 1→2                | 169.253     | 157.653 | 196.964 |
| Interval 2→3                | 175.556     | 162.457 | 184.733 |
| Interval 3→4                | 152.191     | 144.337 | 160.041 |
| Time from last marker to end| 112.325     | 79.050  | 152.834 |


## Plots

### Aggregate Boxplots

![HR Boxplot Across Conditions](Plots/Aggregate/HR_boxplot_conditions.png)
![RR Boxplot Across Conditions](Plots/Aggregate/RR_boxplot_conditions.png)
![RMSSD Boxplot Across Conditions](Plots/Aggregate/RMSSD_boxplot_conditions.png)

### Aligned Average Curves

![Aligned Average Heart Rate (HR)](Plots/Aggregate/HR_average_aligned.png)
![Aligned Average RR Interval](Plots/Aggregate/RR_average_aligned.png)
![Aligned Average RMSSD](Plots/Aggregate/RMSSD_average_aligned.png)

# Outlier Detection

Scripts filter any RR values outside of the range [300, 2000] ms. Values outside of this range are replaced by interpolated values. For current samples no outliers were detected.

## How calculations are done

- HR and RR loading
  - Read `timestamp` (ms) → convert to datetime; set as index.
  - HR column: `hr` (beats per minute). RR column: `rr_ms` (milliseconds).

- Moving averages (per recording)
  - Time-based rolling means over irregular timestamps using windows: 5 s, 10 s, 30 s, 60 s.
  - X-axis is seconds from each recording’s start.

- Mark handling (per recording)
  - Vertical lines at marks from `timestamps.csv`.
  - Segment boxplots use values in: each interval between consecutive marks and the final interval (last mark → end).

- RMSSD (HRV)
  - Definition: RMSSD = sqrt(mean(diff(RR_ms)^2)) using successive RR intervals (ms).
  - Aggregate per-condition RMSSD: computed over all RR samples within each condition’s time window (recording-level); requires ≥2 RR samples.
  - Aligned time-series RMSSD: first compute a 30 s time-based rolling RMSSD per recording, then align and average across recordings (see below).

- Aggregate metrics across recordings (exactly 4 marks)
  - Conditions (English labels):
    - Breathing Scene 1 (pre-first mark)
    - Spring Scene (1st–2nd mark)
    - Summer Scene (2nd–3rd mark)
    - Autumn Scene (3rd–4th mark)
    - Breathing Scene 2 (post-last mark)
  - Per recording: compute medians for HR and RR within each condition; compute RMSSD within each condition.
  - Summary CSV aggregates these per-recording values; boxplots show distributions. The mean of each condition is marked by a black dot on the boxplots.

- Aligned average curves (HR, RR, RMSSD)
  - Only recordings with exactly 4 marks are used.
  - For each recording, durations of the five segments are measured; median segment proportions across recordings define a normalized 0–1 time axis with aligned boundaries.
  - Each recording’s series is piecewise-linearly time-normalized to this axis, interpolated to a common grid, and then averaged (mean ± 1 SD). For RMSSD, a 30 s rolling RMSSD is used before alignment.