lpfilter.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2016, Los Alamos National Security, LLC
00003 All rights reserved.
00004 Copyright 2016. Los Alamos National Security, LLC. This software was produced under U.S. Government contract DE-AC52-06NA25396 for Los Alamos National Laboratory (LANL), which is operated by Los Alamos National Security, LLC for the U.S. Department of Energy. The U.S. Government has rights to use, reproduce, and distribute this software.  NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce derivative works, such modified software should be clearly marked, so as not to confuse it with the version available from LANL.
00005 
00006 Additionally, redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00007 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
00008 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
00009 3. Neither the name of Los Alamos National Security, LLC, Los Alamos National Laboratory, LANL, the U.S. Government, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
00010 
00011 THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00012 
00013 Author: Alex von Sternberg
00014 */
00015 
00016 #include <lpfilter.h>
00017 
00018 LPFilter::LPFilter(double deltaT, double cutoffFrequency, int numElements):
00019   initialized(false),
00020   noElements(0),
00021   omega_a(0.0),
00022   a0(0.0),
00023   a1(0.0),
00024   a2(0.0),
00025   b1(0.0),
00026   b2(0.0)
00027 {
00028   initialized = true;
00029   if(numElements<=0)
00030   {
00031     ROS_ERROR_STREAM("LPFilter was passed invalid number of elements. Not filtering.");
00032     initialized = false;
00033   }
00034   else
00035   {
00036     noElements = numElements;
00037   }
00038   if(deltaT<=0)
00039   {
00040     ROS_ERROR_STREAM("LPFilter was passed invalid deltaT. Not Filtering.");
00041     initialized = false;
00042   }
00043   if(cutoffFrequency <=0)
00044   {
00045     ROS_ERROR_STREAM("LPFilter was passed invalid cuttoffFrequency. Not Filtering.");
00046     initialized = false;
00047   }
00048   else
00049   {
00050     cutoffFrequency *= (2*M_PI);
00051     omega_a = tan(cutoffFrequency*deltaT/2.0);
00052     double den = omega_a*omega_a+sqrt(2.0*omega_a+1.0);
00053     a0 = (omega_a*omega_a)/den;
00054     a1 = (2.0*omega_a*omega_a)/den;
00055     a2 = a0;
00056     b1 = 2.0*(omega_a*omega_a-1.0)/den;
00057     b2 = (omega_a*omega_a-sqrt(2.0)*omega_a+1)/den;
00058     in1.resize(noElements);
00059     in2.resize(noElements);
00060     out1.resize(noElements);
00061     out2.resize(noElements);
00062     ROS_INFO_STREAM("cutoffFrequency: " << cutoffFrequency << ". omega_a: " << omega_a << ". den: " << den << ". a0: " << a0 << ". a1: " << a1 << ". a2: " << a2 << ". b1: " << b1 << ". b2: " << b2);
00063   }
00064 }
00065   
00066 bool LPFilter::update(std::vector<double> input, std::vector<double>& output)
00067 {
00068   if(!initialized)
00069   {
00070     ROS_ERROR_STREAM("LPFilter was not initialized correctly. Not filtering data.");
00071     return false;
00072   }
00073   if(input.size() != in1.size() || output.size() != out1.size())
00074   {
00075     ROS_ERROR_STREAM("LPFilter incorrect input or output size");
00076     return false;
00077   }
00078   for(int i=0; i<noElements; i++)
00079   {
00080     output.at(i) = a0*input.at(i) + a1*in1.at(i) + a2*in2.at(i) - b1*out1.at(i) - b2*out2.at(i);
00081     out2.at(i) = out1.at(i);
00082     out1.at(i) = output.at(i);
00083     in2.at(i) = in1.at(i);
00084     in1.at(i) = input.at(i);
00085   }
00086   return true;
00087 } 


netft_utils
Author(s): Alex von Sternberg , Derek King, Andy Zelenak
autogenerated on Sat Jul 15 2017 02:45:58