这是我的解决方案。首先,由于您的问题没有数据,我使用了 UCI 机器学习存储库中的“用于手势数据集的 EMG 数据”。
链接https://archive.ics.uci.edu/ml/datasets/EMG+data+for+gestures
你使用的数据集非常相似,第一个变量是时间,之后 8 个变量是通道,最后一个是类
要为每个通道创建图表,您可以使用 FOR 循环,将您关注的列用作迭代运算符。中间代码和你的一样,最后在绘图时我对绘图标题进行了更改,使其与各自的列名相似。
library(biosignalEMG)
extensor_raw <- read.delim("01/1_raw_data_13-12_22.03.16.txt")
head(extensor_raw)
for(i in names(extensor_raw[2:9])){
print(paste("Drawing for ", i))
# Coerce a data.frame into an 'emg' object
x <- as.emg(extensor_raw[i], samplingrate = 1000, units = "mV") ##do this for every channel
# Compute the rectified signal
x_rect <- rectification(x)
# Filter the rectified signal
y <- lowpass(x_rect, cutoff = 100)
# change graphical parameters to show multiple plots
op <- par(mfrow = c(3, 1))
# plot the original channel, the filtered channel and the
# LE-envelope
plot(x, channel = 1, main = paste("Original ", i))
plot(x_rect, main = paste("Rectified", i))
plot(y, main = paste("LE-envelope", i))
}
在这段代码的末尾,您可以看到在 rstudio 的图形部分中创建了多个页面,同时绘制从 1 到 8 的每个通道
喜欢第 5 频道,也喜欢其他频道。我希望这能帮助您解决问题。
关于您在 cmets 中询问的第二部分:如果您将文件分开,让我们将其分开。将一一阅读,然后绘制它。为此,我们将使用嵌套的 FOR 循环。
首先设置您的工作目录,其中包含所有手势文件。就像我的情况一样,我的目录中有两个具有相同结构的文件。
代码改动如下:
setwd('~/Downloads/EMG_data_for_gestures-master/01')
library(biosignalEMG)
for(j in list.files()){
print(paste("reading file ",j))
extensor_raw <- read.delim(j)
head(extensor_raw)
for(i in names(extensor_raw[2:9])){
print(paste("Drawing for ", i))
# Coerce a data.frame into an 'emg' object
x <- as.emg(extensor_raw[i], samplingrate = 1000, units = "mV") ##do this for every channel
# Compute the rectified signal
x_rect <- rectification(x)
# Filter the rectified signal
y <- lowpass(x_rect, cutoff = 100)
# change graphical parameters to show multiple plots
op <- par(mfrow = c(3, 1))
# plot the original channel, the filtered channel and the LE-envelope
plot(x, channel = 1, main = paste("Original ", i," from ", j))
plot(x_rect, main = paste("Rectified", i," from ", j))
plot(y, main = paste("LE-envelope", i," from ", j))
}
}
我希望这会有所帮助。