lpfilter.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2016, Los Alamos National Security, LLC
3 All rights reserved.
4 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.
5 
6 Additionally, redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 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.
9 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.
10 
11 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.
12 
13 Author: Alex von Sternberg
14 */
15 
16 #include <lpfilter.h>
17 
18 LPFilter::LPFilter(double deltaT, double cutoffFrequency, int numElements):
19  initialized(false),
20  noElements(0),
21  omega_a(0.0),
22  a0(0.0),
23  a1(0.0),
24  a2(0.0),
25  b1(0.0),
26  b2(0.0)
27 {
28  initialized = true;
29  if(numElements<=0)
30  {
31  ROS_ERROR_STREAM("LPFilter was passed invalid number of elements. Not filtering.");
32  initialized = false;
33  }
34  else
35  {
36  noElements = numElements;
37  }
38  if(deltaT<=0)
39  {
40  ROS_ERROR_STREAM("LPFilter was passed invalid deltaT. Not Filtering.");
41  initialized = false;
42  }
43  if(cutoffFrequency <=0)
44  {
45  ROS_ERROR_STREAM("LPFilter was passed invalid cuttoffFrequency. Not Filtering.");
46  initialized = false;
47  }
48  else
49  {
50  cutoffFrequency *= (2*M_PI);
51  omega_a = tan(cutoffFrequency*deltaT/2.0);
52  double den = omega_a*omega_a+sqrt(2.0*omega_a+1.0);
53  a0 = (omega_a*omega_a)/den;
54  a1 = (2.0*omega_a*omega_a)/den;
55  a2 = a0;
56  b1 = 2.0*(omega_a*omega_a-1.0)/den;
57  b2 = (omega_a*omega_a-sqrt(2.0)*omega_a+1)/den;
58  in1.resize(noElements);
59  in2.resize(noElements);
60  out1.resize(noElements);
61  out2.resize(noElements);
62  ROS_INFO_STREAM("cutoffFrequency: " << cutoffFrequency << ". omega_a: " << omega_a << ". den: " << den << ". a0: " << a0 << ". a1: " << a1 << ". a2: " << a2 << ". b1: " << b1 << ". b2: " << b2);
63  }
64 }
65 
66 bool LPFilter::update(std::vector<double> input, std::vector<double>& output)
67 {
68  if(!initialized)
69  {
70  ROS_ERROR_STREAM("LPFilter was not initialized correctly. Not filtering data.");
71  return false;
72  }
73  if(input.size() != in1.size() || output.size() != out1.size())
74  {
75  ROS_ERROR_STREAM("LPFilter incorrect input or output size");
76  return false;
77  }
78  for(int i=0; i<noElements; i++)
79  {
80  output.at(i) = a0*input.at(i) + a1*in1.at(i) + a2*in2.at(i) - b1*out1.at(i) - b2*out2.at(i);
81  out2.at(i) = out1.at(i);
82  out1.at(i) = output.at(i);
83  in2.at(i) = in1.at(i);
84  in1.at(i) = input.at(i);
85  }
86  return true;
87 }
bool update(std::vector< double > input, std::vector< double > &output)
Definition: lpfilter.cpp:66
std::vector< double > out1
Definition: lpfilter.h:17
double a2
Definition: lpfilter.h:19
double b2
Definition: lpfilter.h:19
double a0
Definition: lpfilter.h:19
std::vector< double > in2
Definition: lpfilter.h:17
double b1
Definition: lpfilter.h:19
double omega_a
Definition: lpfilter.h:18
std::vector< double > out2
Definition: lpfilter.h:17
bool initialized
Definition: lpfilter.h:15
#define ROS_INFO_STREAM(args)
int noElements
Definition: lpfilter.h:16
double a1
Definition: lpfilter.h:19
LPFilter(double deltaT, double cutoffFrequency, int numElements)
Definition: lpfilter.cpp:18
#define ROS_ERROR_STREAM(args)
std::vector< double > in1
Definition: lpfilter.h:17


netft_utils
Author(s): Alex von Sternberg , Derek King, Andy Zelenak
autogenerated on Tue Mar 2 2021 03:15:08