Deben Oldert 9 роки тому
батько
коміт
49e80503c5
2 змінених файлів з 28 додано та 19 видалено
  1. 25 17
      code/head.R
  2. 3 2
      code/state.R

+ 25 - 17
code/head.R

@@ -1,3 +1,4 @@
+print("START ANALYZING HEAD")
 #color <- rainbow(length(STATE))
 color <- c("red", "blue", "blue", "green")
 
@@ -12,27 +13,34 @@ for(i in 1:nrow(DATA)){
 }
 
 
-#Check if patient is walking straight enough
-differ_ <- 0.4
+#Check if patient is walking fast enough
+differ_ <- 0.1
 
-start <- min(WALKING)
-stop <- max(WALKING)
+# Algorithm to calculate walk phase
+f <- function(x) (sin((x - 5) / 2.7) / 30) + WALKBASE
 
+# Draw Optimum walk
+lines(WALKING, f(WALKING), col = "red", type = "l")
 
-segments(start, WALKBASE, stop, col = "black")
+# Check if each point is close to optimum track
+WALK <- data.frame(index=WALKING)
+WALK$probability <- 0
 
-#base <- mean(c(DATA[start,]$Head.y, DATA[stop,]$Head.y))
-#size_ <- 5
-# for(i in seq(start, stop, size_)){
-#
-#   step <- mean(DATA[i:(i+size_),]$Head.y) - base
-#
-#   for(j in 0:(size_ -1)){
-#     if(DATA[i+j,]$state != 1) next
-#     points(i+j, DATA[i+j,]$Head.y - step, p = "*")
-#   }
-#
-# }
+for(i in WALKING){
+   prob_ <- 1 - abs((abs(DATA[i,]$Head.y - f(i))) / differ_)
+   #print(DATA[i,]$Head.y)
+   #print(f(i))
+   #print(prob_)
+   #print("======")
+   WALK[WALK$index==i,]$probability <- if(prob_ > 1) 1 else if(prob_ < 0) 0 else prob_
+}
+
+# points(WALK[WALK$probability == min(WALK$probability),]$index, 0.3)
+# points(WALK[WALK$probability == max(WALK$probability),]$index, 0.3)
+
+# Print out possible fall chance
+print(paste("FALL CHANCE BASED ON STEPS: ", as.character(specify_decimal(100-(mean(WALK$probability)*100), 2)), "%", sep = ""))
 
+print("DONE ANALYZING HEAD")
 # http://stats.stackexchange.com/questions/30975/how-to-add-non-linear-trend-line-to-a-scatter-plot-in-r
 # http://www.mathsisfun.com/geometry/parabola.html

+ 3 - 2
code/state.R

@@ -68,14 +68,15 @@ DATA$index = as.integer(rownames(DATA))
 
 WALKING <- strtoi(rownames(DATA[DATA$state==1,]))
 
-# DRAW PARABOLA (FOR CALCULATING STRAIGHT PATH)
+# CALCULATE STRAIGHT WALKING PATH
 
 yPrediction <-lm(Head.y ~ I(index^2)+index, data=DATA[min(WALKING):max(WALKING),])
-#lines(WALKING, predict(lm2, data.frame(index=WALKING)), type = "l", col = "orange")
+#lines(WALKING, predict(yPrediction, data.frame(index=WALKING)), type = "l", col = "orange")
 
 yPredicted <- as.vector(predict(yPrediction, data.frame(index=WALKING)))
 
 WALKBASE <- mean(c(yPredicted[1], tail(yPredicted, n=1)))
+#WALKBASE <- mean(c(DATA[WALKING[1],]$Head.y, DATA[tail(WALKING, n=1),]$Head.y))
 
 for(i in WALKING[1]:(length(WALKING) + WALKING[1] - 1)){
   DATA[i,]$Head.y <- DATA[i,]$Head.y - (yPredicted[i-WALKING[1]+1] - WALKBASE)