我必须分析肌电数据,但我不太擅长使用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$channel1,而不是像extensor_raw$i这样的东西,然后循环它吗?或者,有没有办法将这一位代码应用于每个通道(即9列数据帧中的8列,不包括指定时间的第一列?)
发布于 2021-06-05 02:05:21
这是我的解决方案。首先,由于你的问题没有数据,我使用了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))
}
}我希望这会对你有所帮助。
发布于 2021-06-05 00:55:41
如果它是按列存储的,请使用lapply并存储为list,并假定所有列都需要更改。(请注意,这并未经过测试。plot中的par可能需要更改)
lst1 <- lapply(extensor_raw, \(vec) {
x <- as.emg(vec, samplingrate = 1000, units = "mV")
# 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)
})https://stackoverflow.com/questions/67841029
复制相似问题