forked from aoandrade/PDPack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMGMarkdown.Rmd
150 lines (99 loc) · 4.46 KB
/
MMGMarkdown.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
---
title: "MMGExample"
author: "Alice Rueda"
date: "February 19, 2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## MMG Signal Processing
This file reads the MMG signal from *.txt files and display the raw signal before processing. It provides the following functions:
- Read the raw data file
- Plot the raw and processed signal
- Pre-processing the necessary signal including detrendng, filtering, and segment the signals into activity windows with use of PULSE signals
- Extract features from the clean segmented signals
```{r packages}
source("MMGLib.R")
source("PlotSignals.R")
source("TREMSENToolbox.R")
#subjectCode: get it from the directory
#pathData ="D:/Adriano/OneDrive/Projetos/2017-Brazil-Canada/DATA/EJRR/09Nov2018/EMG/Excel"
pathData = "/media/alice/DATA/2019 Adriano/2019 Adriano MMG/Data/Coletas 14_11/MMG_Tremsen/COLETAS 14_11"
# defining subject and filetype
#subjectCode = "RCS"
signalType = "MMG"
fileType = "txt"
```
## Reading MMG file
The data files are stored in txt files with tab as separators. This part lists all files with *.txt in the data file directory.
```{r Read File}
# list all relevant files
#pathFile <- paste(pathData,subjectCode,'/', sep='')
filename <- sort(list.files(path=pathData, full.names=TRUE, pattern="*.txt"))
#filename <- basename(filename)
i <- 1
df <- LoadTREMSENFile(filename[i])
```
## Visualize Raw Signal
We are only interested in the first 2 sets of accelerometers. Gyroscope signal and magnetometers are not of our interest. This part plots the triaxial signals from the 2 accelerometers.
```{r Visualize Raw Signal}
# Calling utility function from PlotSignal.R to plot the raw signal
ggplotAccelerometer(df)
```
```{r Signal Preprocessing 1}
# Detrend and remove DC offset --------------------------------------------
df.nonlineardetrended <- nonLineardetrendTremsenData(df) #Remove both linear and nonlinear trends
ggplotAccelerometer(df.nonlineardetrended)
```
```{r Signal Processing 2}
# Event detection ---------------------------------------------------
# Using detrend dataframe for event detection
dfdetrended<-df.nonlineardetrended
eventDetect = detectMMGEvent(dfdetrended, percentage = 0.05, PLOT=TRUE)
scaling = 0.5
channel = dfdetrended$X.A1.X.
dfplot1<-data.frame(time = dfdetrended$X.Time., channel, dfdetrended$X.PULSE*max(channel)*scaling, eventDetect*max(channel)*scaling)
dygraph(dfplot1, main = "Accelerometer x-axis") %>%
dyAxis("x", label="Time (s)") %>%
dyAxis("y", label="Angular velocity") %>%
dyOptions(colors = c('black', 'blue', 'red')) %>%
dyLegend(width = 130) %>%
dyRangeSelector()
```
```{r Lowpass Filter}
filteredLP <- butterLowPassFilter(channel, cutoff = 2, Fs = 50) # Motion artifact
filteredHP <- butterHighPassFilter(channel, cutoff = 2, Fs = 50) # MMG signals
residual <- channel - filteredLP # Just to show that the MMG signal is the same as subtracting the motion artifacts from the original signal
#dfplot<-data.frame(time = df$X.Time., eventDetect*max(channel), filtered, channel, (channel-filtered))
dfplot<-data.frame(time = dfdetrended$X.Time., channel, filteredLP, eventDetect*max(channel))
dygraph(dfplot, main = 'With Low Pass Filter')%>%
dyOptions(colors = c('black', 'blue', 'red')) %>%
dyRangeSelector()
#dfplot<-data.frame(time = df$X.Time., eventDetect*max(channel), filtered, channel, (channel-filtered))
dfplot<-data.frame(time = dfdetrended$X.Time., residual, filteredHP, eventDetect*max(residual))
dygraph(dfplot, main = 'With Low Pass Filter')%>%
dyOptions(colors = c('black', 'blue', 'red')) %>%
dyRangeSelector()
differences <- filteredHP - residual
#dfplot<-data.frame(time = df$X.Time., eventDetect*max(channel), filtered, channel, (channel-filtered))
dfplot<-data.frame(time = dfdetrended$X.Time., differences, eventDetect*max(residual))
dygraph(dfplot, main = 'With Low Pass Filter')%>%
dyOptions(colors = c('black', 'blue', 'red')) %>%
dyRangeSelector()
```
```{r Segmentation}
# Partition signal -----------------------------------------------
sep <- findSep(eventDetect)
partChannel <- partitionChannel(filteredHP,sep)
partOrignal <- partitionChannel(channel,sep)
numSegment <- size(partChannel,2) # The activities are stored in even number list of partChannel
pinchMMG <- partChannel$'2'
pinchOrignal <- partOrignal$'2'
handMMG <- partChannel$'4'
handOrignal <- partOrignal$'4'
supMMG <- partChannel$'6'
supOrignal <- partOrignal$'6'
flexMMG <- partChannel$'8'
flexOrignal <- partOrignal$'8'
```