Advertisement
inf926k

Untitled

Jan 18th, 2018
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.42 KB | None | 0 0
  1.     private func getAcc(_ accData: [AccelerometerDataPoint]) -> Double {
  2.         let xs = accData.map( { $0.x * 1e5 } )
  3.         let ys = accData.map( { $0.y * 1e5 } )
  4.         let zs = accData.map( { $0.z * 1e5 } )
  5.        
  6.         var score = 0.0
  7.         for i in 0 ..< xs.count - 1 {
  8.             var stepScore = 0.0
  9.             let zipped = zip([xs[i], ys[i], zs[i]],
  10.                              [xs[i + 1], ys[i + 1], zs[i + 1]])
  11.             for (elem0, elem1) in zipped {
  12.                 let alpha = 1.0 * (elem1 - elem0) / TIME
  13.                 let beta = elem0
  14.                 let distance = integrate(alpha: alpha, beta: beta)
  15.                 stepScore = stepScore + distance * distance
  16.             }
  17.             score = score + sqrt(stepScore) * 3.0
  18.         }
  19.         score = score / Double(xs.count - 1)
  20.        
  21.         return score
  22.     }
  23.    
  24.     private func integrate(alpha: Double,
  25.                            beta: Double,
  26.                            v0: Double = 0,
  27.                            x0: Double = 0,
  28.                            x1: Double = 0.05,
  29.                            num: Int = 1_00) -> Double {
  30.        
  31.         func linspace(from: Double, to: Double, count: Int) -> [Double] {
  32.             assert(count >= 2)
  33.             let step = (to - from) / Double(count - 1)
  34.             var array = [Double]()
  35.             for i in 0 ..< count {
  36.                 let value = from + step * Double(i)
  37.                 array.append(value)
  38.             }
  39.            
  40.             return array
  41.         }
  42.        
  43.         let step = x1 / Double(num)
  44.         let linspaceArray = linspace(from: x0, to: x1, count: num + 1).dropLast()
  45.         let mapped = linspaceArray.map { (x) -> Double in
  46.             let sum = pow(alpha * x, 2) / 2 + beta * x + v0
  47.             return abs(sum) * step
  48.         }
  49.        
  50.         return mapped.sum()
  51.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement