This R Markdown file demonstrates healthcare signal processing and monitoring using sample ECG (Electrocardiogram) data. It covers: - Generating synthetic ECG data - Signal filtering - Feature extraction (heart rate) - Visualization - Simple monitoring/alert logic
set.seed(123)
time <- seq(0, 10, by = 0.002)
# Simulate ECG-like waveform
ecg <- 1.5 * sin(2 * pi * 1.2 * time) + 0.5 * sin(2 * pi * 3 * time) + rnorm(length(time), 0, 0.2)
ecgsig <- ecg + 0.2 * sin(2 * pi * 50 * time) # Add 50Hz noise
data <- tibble(time = time, ecg = ecgsig)
head(data)
## # A tibble: 6 × 2
## time ecg
## <dbl> <dbl>
## 1 0 -0.112
## 2 0.002 0.113
## 3 0.004 0.585
## 4 0.006 0.329
## 5 0.008 0.309
## 6 0.01 0.550
ggplot(data, aes(x = time, y = ecg)) +
geom_line(color = 'steelblue') +
labs(title = 'Raw ECG Signal', x = 'Time (s)', y = 'Amplitude') +
theme_minimal()
fs <- 500 # Sampling frequency (Hz)
nyq <- fs / 2
bf <- butter(4, c(0.5, 40) / nyq, type = "pass")
data$ecg_filt <- filtfilt(bf, data$ecg)
ggplot(data, aes(x = time, y = ecg_filt)) +
geom_line(color = 'darkgreen') +
labs(title = 'Filtered ECG Signal', x = 'Time (s)', y = 'Amplitude') +
theme_minimal()
# Detect R-peaks (simple thresholding for demo)
peaks <- which(diff(sign(diff(data$ecg_filt))) == -2 & data$ecg_filt[-c(1, length(data$ecg_filt))] > 1)
peak_times <- data$time[peaks]
rr_intervals <- diff(peak_times)
heart_rate <- 60 / mean(rr_intervals)
cat("Estimated Heart Rate:", round(heart_rate), "bpm\n")
## Estimated Heart Rate: 448 bpm
if (heart_rate < 50) {
alert <- "Bradycardia detected (Low heart rate)!"
} else if (heart_rate > 100) {
alert <- "Tachycardia detected (High heart rate)!"
} else {
alert <- "Heart rate normal."
}
cat(alert)
## Tachycardia detected (High heart rate)!
Electroencephalogram (EEG) and Electrocardiogram (ECG) are both biomedical signals but represent different physiological processes:
Below, we generate synthetic EEG and ECG signals, visualize them, and
compare their characteristics using a dynamic, interactive plot with the
dygraphs package.
# Generate synthetic EEG signal (higher frequency, lower amplitude)
time <- seq(0, 10, by = 0.002)
eeg <- 0.1 * sin(2 * pi * 10 * time) + 0.05 * sin(2 * pi * 20 * time) + rnorm(length(time), 0, 0.02)
# ECG signal already generated as data$ecg_filt
# Combine for comparison
df_compare <- data.frame(
time = time,
ECG = data$ecg_filt,
EEG = eeg
)
# Convert time to POSIXct for xts compatibility
start_time <- as.POSIXct("2024-01-01 00:00:00")
df_compare$datetime <- start_time + df_compare$time
df_xts <- xts(df_compare[, c("ECG", "EEG")], order.by = df_compare$datetime)
dygraph(df_xts, main = "Comparison of ECG and EEG Signals") %>%
dySeries("ECG", color = "darkgreen") %>%
dySeries("EEG", color = "purple") %>%
dyRangeSelector() %>%
dyOptions(stackedGraph = FALSE, fillGraph = FALSE)
The interactive plot above allows you to zoom and pan to explore the differences in signal morphology and frequency content.
This example demonstrates basic healthcare signal processing and monitoring in R. For real-world applications, use validated algorithms and real patient data.