Joã*_*oão 6 r machine-learning time-series accelerometer iot
我对传感器数据非常着迷.我使用我的iPhone和一个名为SensorLog的应用程序捕获加速度计数据,同时我站立并推动我的双腿跳跃.
我的目标是使用R创建一个模型,该模型可以识别跳跃以及我在空中的时间.我不确定如何进行这样的挑战.我有加速计数据的时间序列.
https://drive.google.com/file/d/0ByWxsCBUWbqRcGlLVTVnTnZIVVk/view?usp=sharing
一些问题:

下面是用于创建上图的R代码,这是我站立并进行简单的跳转.
谢谢!
# Training set
sample <- read.csv("sample-data.csv")
# Sum gravity
sample$total_gravity <- sqrt(sample$accelerometerAccelerationX^2+sample$accelerometerAccelerationY^2+sample$accelerometerAccelerationZ^2)
# Smooth our total gravity to remove noise
f <- rep(1/4,4)
sample$total_gravity_smooth <- filter(sample$total_gravity, f, sides=2)
# Removes rows with NA from smoothing
sample<-sample[!is.na(sample$total_gravity_smooth),]
#sample$test<-rollmaxr(sample$total_gravity_smooth, 10, fill = NA, align = "right")
# Plot gravity
plot(sample$total_gravity, type="l", col=grey(.2), xlab="Series", ylab="Gravity", main="Accelerometer Gravitational Force")
lines(sample$total_gravity_smooth, col="red")
stdevs <- mean(sample$total_gravity_smooth)+c(-2,-1,+1,+2)*sd(sample$total_gravity_smooth)
abline(h=stdevs)
Run Code Online (Sandbox Code Playgroud)
这可能不是完美的解决方案,但它可能足以让您入门。第一部分依赖于对Gazetools包中的 find_peaks 函数的一个小修改。
find_maxima <- function(x, threshold)
{
ranges <- find_peak_ranges(x, threshold)
peaks <- NULL
if (!is.null(ranges)) {
for (i in 1:nrow(ranges)) {
rnge <- ranges[i, 1]:ranges[i, 2]
r <- x[rnge]
peaks <- c(peaks, rnge[which(r == max(r))])
}
}
peaks
}
find_minima <- function(x, threshold)
{
ranges <- find_peak_ranges(x, threshold)
peaks <- NULL
if (!is.null(ranges)) {
for (i in 1:nrow(ranges)) {
rnge <- ranges[i, 1]:ranges[i, 2]
r <- x[rnge]
peaks <- c(peaks, rnge[which(r == min(r))])
}
}
peaks
}
Run Code Online (Sandbox Code Playgroud)
为了让 find_maxima 和 find_minima 函数为我们提供我们正在寻找的东西,我们需要进一步平滑total_gravity 数据:
spline <- smooth.spline(sample$loggingSample, y = sample$total_gravity, df = 30)
Run Code Online (Sandbox Code Playgroud)
注意:我将总重力“归零”( sample$total_gravity <- sample$total_gravity - 1)
接下来,提取平滑后的 x 和 y 值:
out <- as.data.frame(cbind(spline$x,spline$y))
Run Code Online (Sandbox Code Playgroud)
然后找到我们的局部最大值和最小值
max <- find_maxima(out$y, threshold = 0.4)
min <- find_minima(out$y, threshold = -0.4)
Run Code Online (Sandbox Code Playgroud)
然后绘制数据以确保一切看起来合法:
plot(out$y, type="l", col=grey(.2), xlab="Series", ylab="Gravity", main="Accelerometer Gravitational Force")
lines(out$y, col="red")
stdevs <- mean(out$y)+c(-2,-1,+1,+2)*sd(out$y)
abline(h=stdevs)
abline(v=max[1], col = 'green')
abline(v=max[2], col = 'green')
abline(v=min[1], col = 'blue')
Run Code Online (Sandbox Code Playgroud)
最后,我们可以看到您离开地面的时间。
print(hangtime <- min[1] - max[1])
[1] 20
Run Code Online (Sandbox Code Playgroud)
您可以降低阈值以获得额外的数据点(加速度的变化)。
希望这可以帮助!