我必须分析 EMG 数据,但我不太擅长使用 R:我有一个包含 9 列的 data.frame:一列指定时间,另外 8 列指定我的通道。我想过滤我的 emg 数据,但我只能按通道执行此操作,但我想一次对数据帧的所有通道执行此操作,因此我不必将其应用于每个通道。
# This example computes the LE-envelope using the lowpass routine
# Coerce a data.frame into an 'emg' object
x <- as.emg(extensor_raw$channel1, 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 = "Original channel")
plot(x_rect, main = "Rectified channel")
plot(y, main = "LE-envelope")
# reset graphical parameters
par(op)
因此,我可以在此处放入诸如 extensor_raw$i 之类的东西并在其周围循环,而不是使用 extensor_raw$channel1 吗?或者有什么方法可以将这段代码应用于每个通道(即 9 列数据帧的 8 列,不包括指定时间的第一列?)
这是我的解决方案。首先,由于您的问题没有数据,我使用了 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 和其他频道一样。我希望这可以帮助您解决您的问题。
在第二部分,您在评论中询问:如果您将文件分开,让我们将其分开。将一一阅读,然后绘制它。为了实现这一点,我们将使用嵌套的 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))
}
}
我希望这会有所帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句