# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: MIT-0 import math import numpy as np import pywt import socket import requests import json def euler_from_quaternion(x, y, z, w): """ Convert a quaternion into euler angles (roll, pitch, yaw) roll is rotation around x in radians (counterclockwise) pitch is rotation around y in radians (counterclockwise) yaw is rotation around z in radians (counterclockwise) """ t0 = +2.0 * (w * x + y * z) t1 = +1.0 - 2.0 * (x * x + y * y) roll_x = math.atan2(t0, t1) t2 = +2.0 * (w * y - z * x) t2 = +1.0 if t2 > +1.0 else t2 t2 = -1.0 if t2 < -1.0 else t2 pitch_y = math.asin(t2) t3 = +2.0 * (w * z + x * y) t4 = +1.0 - 2.0 * (y * y + z * z) yaw_z = math.atan2(t3, t4) return roll_x, pitch_y, yaw_z # in radians def wavelet_denoise(data, wavelet, noise_sigma): ''' Filter accelerometer data using wavelet denoising Modification of F. Blanco-Silva's code at: https://goo.gl/gOQwy5 ''' wavelet = pywt.Wavelet(wavelet) levels = min(5, (np.floor(np.log2(data.shape[0]))).astype(int)) # Francisco's code used wavedec2 for image data wavelet_coeffs = pywt.wavedec(data, wavelet, level=levels) threshold = noise_sigma*np.sqrt(2*np.log2(data.size)) new_wavelet_coeffs = map(lambda x: pywt.threshold(x, threshold, mode='soft'), wavelet_coeffs) return pywt.waverec(list(new_wavelet_coeffs), wavelet) def create_dataset(X, time_steps=1, step=1): """ This encodes a list of readings into the correct shape expected by the model. It uses the concept of a sliding window """ Xs = [] for i in range(0, len(X) - time_steps, step): v = X[i:(i + time_steps)] Xs.append(v) return np.array(Xs)