filters.py
Go to the documentation of this file.
00001 #
00002 # Copyright (c) 2009, Georgia Tech Research Corporation
00003 # All rights reserved.
00004 #
00005 # Redistribution and use in source and binary forms, with or without
00006 # modification, are permitted provided that the following conditions are met:
00007 #     * Redistributions of source code must retain the above copyright
00008 #       notice, this list of conditions and the following disclaimer.
00009 #     * Redistributions in binary form must reproduce the above copyright
00010 #       notice, this list of conditions and the following disclaimer in the
00011 #       documentation and/or other materials provided with the distribution.
00012 #     * Neither the name of the Georgia Tech Research Corporation nor the
00013 #       names of its contributors may be used to endorse or promote products
00014 #       derived from this software without specific prior written permission.
00015 #
00016 # THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS IS'' AND
00017 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 # DISCLAIMED. IN NO EVENT SHALL GEORGIA TECH BE LIABLE FOR ANY DIRECT, INDIRECT,
00020 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00021 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00022 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00023 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00024 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00025 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 #
00027 
00028 #  \author Advait Jain (Healthcare Robotics Lab, Georgia Tech.)
00029 
00030 
00031 import numpy as np
00032 import circular_buffer as cb
00033 
00034 # Code copied and modified from: http://www.scipy.org/Cookbook/KalmanFiltering
00035 class Kalman1D():
00036     # Q - process variance
00037     # R - measurement variance
00038     # P - variance in my estimate
00039     def __init__(self, P, Q, R):
00040         self.P_orig = float(P)
00041         self.Q_orig = float(Q)
00042         self.R_orig = float(R)
00043         self.reset()
00044 
00045     # rest the filter.
00046     def reset(self):
00047         self.P = self.P_orig
00048         self.Q = self.Q_orig
00049         self.R = self.R_orig
00050         self.xhat = None
00051 
00052     def predict(self, z):
00053         if self.xhat == None:
00054             self.xhat = z
00055         #time update
00056         xhatminus = self.xhat
00057         Pminus = self.P + self.Q
00058         #measurement update
00059         K = Pminus / (Pminus + self.R)
00060         self.xhat = xhatminus + K * (z-xhatminus)
00061         self.P = (1-K) * Pminus
00062         return self.xhat
00063         
00064 
00065 class Mean():
00066     # shape - () for scalar, (m,n) for mxn array
00067     def __init__(self, size, shape):
00068         self.buf = cb.CircularBuffer(size, shape)
00069 
00070     def reset(self):
00071         self.buf.clear()
00072 
00073     def predict(self, z):
00074         self.buf.append(z)
00075         return np.mean(self.buf.to_list(), 0)
00076 
00077 class Median():
00078     # shape - () for scalar, (m,n) for mxn array
00079     def __init__(self, size, shape):
00080         self.buf = cb.CircularBuffer(size, shape)
00081 
00082     def reset(self):
00083         self.buf.clear()
00084 
00085     def predict(self, z):
00086         self.buf.append(z)
00087         return np.median(self.buf.to_list(), 0)
00088 
00089 
00090 
00091 # x - 1D np array
00092 def filter_array(x, filt):
00093     x_filt = []
00094     for z in x:
00095         x_filt.append(filt.predict(z))
00096 #    print 'final value of P:', kf.P
00097     return np.array(x_filt)
00098 
00099 
00100 
00101 
00102 


hrl_lib
Author(s): Cressel Anderson, Travis Deyle, Advait Jain, Hai Nguyen, Advisor: Prof. Charlie Kemp, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:34:06