[chronojump] Smoothing tests. WIP



commit 27ba0514eec1bf61bae4c943db2f654572de3e0c
Author: Xavier Padullés <x padulles gmail com>
Date:   Tue Apr 14 17:31:53 2015 +0200

    Smoothing tests. WIP

 encoder/tests/smooth-2015.R |   83 +++++++++++++++++++++++++++++++++---------
 1 files changed, 65 insertions(+), 18 deletions(-)
---
diff --git a/encoder/tests/smooth-2015.R b/encoder/tests/smooth-2015.R
index 7ca9408..af3ad35 100644
--- a/encoder/tests/smooth-2015.R
+++ b/encoder/tests/smooth-2015.R
@@ -3,11 +3,67 @@
 
 source("../util.R")
 
-getSpeedByPosition <- function(displacement, smoothing) {
-       position <- cumsum(displacement)
-       position_smooth <- smooth.spline( 1:length(position), position, spar=smoothing)
+#Modified from uncompress from capture.R. This try to eliminate the zeros of the signal and returns a 
position.
+#A timestamp is needed
+#converts data: "0*5 1 0 -1*3 2"
+#into points like: (0, 0) , (5, 1) , (6, 1) , (7, 0) , (8, -1) , (9, -2), (10, 0)
+getPositionFromChanges <- function(curveCompressed)
+{
+  chunks = unlist(strsplit(curveCompressed, " "))
+  points = matrix(c(0,0), ncol=2)
+  x = 0
+  y = 0
+  for(i in 1:length(chunks)) 
+  {
+    if(grepl("\\*",chunks[i])) {
+      chunk = as.numeric(unlist(strsplit(chunks[i], "\\*"))) #from "0*1072" to: 0 1072 (as integers)
+      if(chunk[1] == 0){
+        x = x + chunk[2]
+      } else {
+        for( j in 1:chunk[2]){
+          y = y + chunk[1]
+          point = matrix(c(x, y), ncol=2)
+          points <- rbind(points, point)
+          x = x + 1
+        }
+      }
+    } else {
+      y = y + as.numeric(chunks[i])
+      point = matrix(c(x, y), ncol=2)
+      points <- rbind(points, point)
+      x = x + 1
+    }
+  }
+  return (points)
+}
+
+uncompress <- function(curveSent)
+{
+  chunks = unlist(strsplit(curveSent, " "))
+  ints = NULL
+  for(i in 1:length(chunks)) 
+  {
+    if(grepl("\\*",chunks[i])) {
+      chunk = as.numeric(unlist(strsplit(chunks[i], "\\*"))) #from "0*1072" to: 0 1072 (as integers)
+      chunk = rep(chunk[1],chunk[2])
+    } else {
+      chunk=chunks[i]
+    }
+    ints = c(ints,chunk)
+  }
+  return (as.numeric(ints))
+}
+
+getPositionFromCompressed <- function(curve, smoothing){
+  positionDiscarded <- getPositionFromChanges(curve)
+  positionDiscardedSmoothed <- smooth.spline(positionDiscarded, spar=smoothing)
+  position <- predict(positionDiscardedSmoothed, 0:positionDiscarded[length(positionDiscarded[,1]),1])
+  return(position$y)
+}
+
+getSpeedByPosition <- function(position) {
        speed = list()
-       speed$y <- diff(position_smooth$y)
+       speed$y <- diff(position)
        speed$x <- 1:length(speed$y)
        return(speed)
        
@@ -17,20 +73,11 @@ getSpeedByPosition <- function(displacement, smoothing) {
        #return(speed)
 }
 
+#Real displacement and the  resulting compressed curve
 displacement = c(0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 
0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 
1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 
0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 
1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1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 0 1 0 1 0 1 0 1 0 1*2 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 
1 0 1 0*2 1 0 1 0*2 1 0*2 1 0*3 1 0*5 1 0*10"
 
-#fer reduceCurveBySpeed (que te un getSpeed)
-#de fet el que cal provar es dos metodes diferents de getSpeed
-
-speed = getSpeed(displacement, 0.7)
-
-speed2_02 = getSpeedByPosition(displacement, 0.2)
-speed2_05 = getSpeedByPosition(displacement, 0.5)
-speed2_07 = getSpeedByPosition(displacement, 0.7)
-
-png("speeds.png", width=1024, height=700)
+position <- getPositionFromCompressed(curve, 0.7)
+plot(position, type="l")
+speed <- getSpeedByPosition(position)
 plot(speed$y, type="l")
-lines(speed2_02, col="red")
-lines(speed2_05, col="green")
-points(speed2_07, col="blue")
-dev.off()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]