timeseries.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 #  Copyright (c) 2010, UC Regents
00004 #  All rights reserved.
00005 #
00006 #  Redistribution and use in source and binary forms, with or without
00007 #  modification, are permitted provided that the following conditions
00008 #  are met:
00009 #
00010 #   * Redistributions of source code must retain the above copyright
00011 #     notice, this list of conditions and the following disclaimer.
00012 #   * Redistributions in binary form must reproduce the above
00013 #     copyright notice, this list of conditions and the following
00014 #     disclaimer in the documentation and/or other materials provided
00015 #     with the distribution.
00016 #   * Neither the name of the University of California nor the names of its
00017 #     contributors may be used to endorse or promote products derived
00018 #     from this software without specific prior written permission.
00019 #
00020 #  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 #  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 #  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 #  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 #  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 #  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 #  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 #  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 #  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 #  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 #  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 #  POSSIBILITY OF SUCH DAMAGE.
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   # First find min and max time out of the whole data set
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   #print "t_min = ", t_min, " t_max = ", t_max
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   # This is surely not the most efficient way to do this...
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   


starmac_tools
Author(s): bouffard
autogenerated on Sun Jan 5 2014 11:38:35