Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private func getAcc(_ accData: [AccelerometerDataPoint]) -> Double {
- let xs = accData.map( { $0.x * 1e5 } )
- let ys = accData.map( { $0.y * 1e5 } )
- let zs = accData.map( { $0.z * 1e5 } )
- var score = 0.0
- for i in 0 ..< xs.count - 1 {
- var stepScore = 0.0
- let zipped = zip([xs[i], ys[i], zs[i]],
- [xs[i + 1], ys[i + 1], zs[i + 1]])
- for (elem0, elem1) in zipped {
- let alpha = 1.0 * (elem1 - elem0) / TIME
- let beta = elem0
- let distance = integrate(alpha: alpha, beta: beta)
- stepScore = stepScore + distance * distance
- }
- score = score + sqrt(stepScore) * 3.0
- }
- score = score / Double(xs.count - 1)
- return score
- }
- private func integrate(alpha: Double,
- beta: Double,
- v0: Double = 0,
- x0: Double = 0,
- x1: Double = 0.05,
- num: Int = 1_00) -> Double {
- func linspace(from: Double, to: Double, count: Int) -> [Double] {
- assert(count >= 2)
- let step = (to - from) / Double(count - 1)
- var array = [Double]()
- for i in 0 ..< count {
- let value = from + step * Double(i)
- array.append(value)
- }
- return array
- }
- let step = x1 / Double(num)
- let linspaceArray = linspace(from: x0, to: x1, count: num + 1).dropLast()
- let mapped = linspaceArray.map { (x) -> Double in
- let sum = pow(alpha * x, 2) / 2 + beta * x + v0
- return abs(sum) * step
- }
- return mapped.sum()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement