baseline_correction: Baseline correction

View source: R/baseline_correction.R

baseline_correctionR Documentation

Baseline correction

Description

Function for computing amplitude corrected to the selected baseline.

Usage

baseline_correction(data, baseline_range, type = "absolute")

Arguments

data

A data frame, tibble or a database table with input data, required columns: subject, sensor, time, signal, epoch.

baseline_range

A numeric vector of time points used as the baseline (e.g., baseline_range = 125:250).

type

A character indicating the type of baseline correction. Only the value "absolute" is allowed at this moment (other values will trigger an error).

Details

If the values from baseline_range vector are out of the range of the time column, the baseline calculation proceeds as follows:

  1. If a part of the baseline_range vector is in the time column and part is outside its range, the baseline correction is computed only from the part inside a time range.

  2. If the whole baseline_range vector is out of the time range, the baseline and also the signal_base values of the output are NA's. In both cases the function returns a warning message along with the output data frame or tibble.

Note: If there are NA values in the signal column, matching rows are ignored in the baseline calculation (which may bias the results) and the function prints a warning message.

Value

A data frame/tibble with added columns:

signal_base

Signal corrected by subtracting the baseline for each epoch.

baseline

A baseline value used for correction.

Examples

# Computing baseline correction on first 10 points, sensor "E1"
# a) Prepare data and compute
data01 <- epochdata |> dplyr::filter(.data$subject == 1 & .data$sensor == "E1")
basedata <- baseline_correction(data01, baseline_range = 1:10, type = "absolute")

# b) Plot raw (black line) and corrected (red line) signal for epoch 1
epoch1 <- basedata |> dplyr::filter(.data$epoch == 1)
plot(epoch1$signal, type = "l", ylim = c(-20, 30), main = "Raw (black) vs Corrected (red) Signal",
xlab = "time point", ylab = "amplitude")
lines(epoch1$signal_base, col = "red")


# Set baseline_range outside of time range
# results in NA's in baseline and signal_base columns
# also returns a warning message
basedata <- baseline_correction(data01, baseline_range = 70:80, type = "absolute")
head(basedata)


diegr documentation built on Nov. 5, 2025, 5:25 p.m.