---
title: "Signal Segmentation"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Signal Segmentation}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(EGM)
```

The concept of *beat segmentation* is important in cardiac electrical signal analysis. 
There are many approaches that can be used, based on the underlying rhythm. 
The simplest is to use a **sinus** rhythm as the baseline, while more complex would be rapid **macro-reentry**. 

Windowing or segmenting signals helps with identify characteristics of individual beats or events. These can subsequently be leveraged in many ways, such as...

- Machine learning approaches on single beat data
- Signal averaging to create template beats
- Visualizing windowed beats

# Sinus rhythm

The initial approach will be to use sinus rhythm, which can most easily be evaluated using a rule-based approach:

1. Between an $QRS_{i}$ (index QRS complex) and $QRS_{i+1}$ (following QRS complex), there must be a T wave
1. Between the $QRS_{i}$ and the $QRS_{i-1}$ (previous QRS complex), there must be P wave ≥ 1
1. There should not be additional depolarization signals between the $P_{i}$ and $QRS_{i}$

```{r}
#| eval: false
ecg <- read_wfdb(record = 'muse-sinus',
                 record_dir = system.file('extdata', package = 'egm'),
                 annotator = 'ecgpuwave')
# Example data
ecg
```

This file represent an ECG data set obtained from MUSE v9 that contains 12-leads of data over 10 seconds.
