## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(musclesyneRgies)

## ----results = "hide", fig.width = 4, fig.asp = 1-----------------------------
# Load a typical activation pattern of 30 cycles (from locomotion)
data("act_pattern")

# Reduce activation pattern to the first cycle
act_sub <- act_pattern$signal[
  1:which(act_pattern$time == max(act_pattern$time))[1]
]

# Calculate FWHM and CoA
act_sub_FWHM <- FWHM(act_sub)
act_sub_CoA <- CoA(act_sub)

# Express results relative to the cycle
n <- length(act_sub)
cycle <- 100 * (seq_len(n) - 1) / n

FWHM_percent <- 100 * act_sub_FWHM / n
CoA_percent <- 100 * (act_sub_CoA - 1) / n

# Half-maximum threshold
hm <- min(act_sub) + (max(act_sub) - min(act_sub)) / 2

# Keep the half-maximum line only where activation is above threshold
hm_plot <- rep(NA_real_, n)
hm_plot[act_sub > hm] <- hm

# Plot activation pattern
plot(
  cycle,
  act_sub,
  type = "l",
  xlab = "Cycle (%)",
  ylab = "Amplitude",
  xlim = c(0, 100)
)

# FWHM: portions above half maximum
lines(
  cycle,
  hm_plot,
  lwd = 3,
  col = "red"
)

# CoA
graphics::abline(
  v = CoA_percent,
  lwd = 2,
  col = "blue"
)

# Labels
text(
  64,
  max(act_sub) * 0.95,
  paste0("FWHM = ", round(FWHM_percent, 1), "%"),
  adj = 0,
  col = "red"
)

text(
  64,
  max(act_sub) * 0.85,
  paste0("CoA = ", round(CoA_percent, 1), "%"),
  adj = 0,
  col = "blue"
)

## ----results = "hide"---------------------------------------------------------
act <- act_pattern$signal

# Calculate the local complexity or Higuchi's fractal dimension (HFD)
nonlin_HFD <- HFD(act)$Higuchi
# Calculate the global complexity or Hurst exponent (H)
nonlin_H <- Hurst(act, min_win = max(act_pattern$time))$Hurst

message("Higuchi's fractal dimension: ", round(nonlin_HFD, 3))
message("Hurst exponent: ", round(nonlin_H, 3))

