00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 """
00033 timeseries.py
00034
00035 Miscellaneous functions for dealing with time series data
00036
00037 """
00038 import roslib
00039 roslib.load_manifest('starmac_tools')
00040 from scipy.interpolate import interp1d
00041 import numpy as np
00042 import tf.transformations as tft
00043
00044 def uniform_resample(inputs, dt):
00045 """
00046 In general time series data t, x will have non-uniform sampling interval and further
00047 in a large system there may be several sources of data with different sampling times.
00048
00049 This function takes a tuple of tuples as input. Each of the inner tuples must contain as its
00050 first element a 'kind' argument (per the kind argument of scipy.interpolate.interp1d; you
00051 probably want to use either 'linear' or 'zero'). The second element of the tuple is a numpy array
00052 of input sample times, and as its remaining elements, 1-D arrays of the
00053 data to be resampled.
00054
00055 The second argument, dt, is the desired output sample period.
00056
00057 The output will first a vector of the new sample times, and second, a tuple of tuples,
00058 the outer tuple of which is the same length as the input one.
00059 Each inner tuple will have the data, resampled as requested.
00060 """
00061
00062 input_times = [input[1] for input in inputs]
00063 t_min = np.min(np.concatenate(input_times))
00064 t_max = np.max(np.concatenate(input_times))
00065
00066 outputs = []
00067 t_out = np.arange(t_min, t_max+dt/10, dt)
00068 for input in inputs:
00069 kind, t_in = input[:2]
00070 out_series = []
00071 for series in input[2:]:
00072 f = interp1d(t_in, series, kind, bounds_error=False, fill_value=0.0)
00073 out_series.append(f(t_out))
00074 outputs.append(out_series)
00075 return t_out, outputs
00076
00077 def quaternion_to_eulerypr(quat_series):
00078 """
00079 Convert a series of quaternions to the equivalent Euler angle (yaw-pitch-roll) representation.
00080
00081 Input: tuple of 1-d vectors (w, x, y, z)
00082 Output: tuple of 1-d vectors (yaw, pitch, roll) in radians
00083 """
00084
00085 yaw = np.empty_like(quat_series[0])
00086 pitch = np.empty_like(quat_series[0])
00087 roll = np.empty_like(quat_series[0])
00088 for i in range(len(quat_series[0])):
00089 yaw[i], pitch[i], roll[i] = \
00090 tft.euler_from_quaternion((quat_series[1][i], quat_series[2][i], quat_series[3][i], quat_series[0][i]),
00091 axes='rzyx')
00092 return yaw, pitch, roll
00093
00094 if __name__ == "__main__":
00095 t1 = np.linspace(0, 10, 10)
00096 x1 = np.sin(t1)
00097 t2 = np.linspace(0.1,12.5,20)
00098 x2 = np.cos(t2)
00099 tout, data_out = uniform_resample((('linear', t1, x1), ('zero', t2, x2)), 0.01)
00100
00101 N=10
00102 quats = (np.empty(N), np.empty(N), np.empty(N), np.empty(N))
00103 for i in range(len(quats[0])):
00104 quats[1][i], quats[2][i], quats[3][i], quats[0][i] = \
00105 tft.quaternion_from_euler(np.radians(180*i/N), np.radians(-90*i/N), np.radians(45*i/N), 'rzyx')
00106 yaw, pitch, roll = quaternion_to_eulerypr(quats)
00107