The VisAO Camera
fir_filter.cpp
Go to the documentation of this file.
1 /************************************************************
2  * fir_filter.cpp
3  *
4  * Author: Jared R. Males (jrmales@as.arizona.edu)
5  *
6  * FIR digital filter class definitions.
7  *
8  ************************************************************/
9 
10 /** \file fir_filter.cpp
11  * \author Jared R. Males
12  * \brief FIR digital filter class definitions.
13  *
14  */
15 
16 #include "fir_filter.h"
17 
18 namespace VisAO
19 {
20 
21 fir_filter::fir_filter()
22 {
23  order = 0;
24  coef = 0;
25  gain = 0;
26 }
27 
28 fir_filter::~fir_filter()
29 {
30  if(coef) delete[] coef;
31 }
32 
33 void fir_filter::set_coef(int ord, float *nc, float gn)
34 {
35  if(coef) delete[] coef;
36 
37  coef = new float[ord];
38 
39  order = ord;
40 
41  for(int i=0;i<order;i++) coef[i] = nc[i];
42 
43  gain = gn;
44 
45 }
46 
47 int fir_filter::read_coef_file(std::string fname)
48 {
49  std::ifstream fin;
50 
51  fin.open(fname.c_str());
52 
53  fin >> order;
54 
55  if(coef) delete[] coef;
56 
57  coef = new float[order];
58 
59  for(int i=0;i<order;i++) fin >> coef[i];
60 
61  fin >> gain;
62 
63  fin.close();
64 
65  return 0;
66 }
67 
68 void fir_filter::print_filter()
69 {
70  std::cout << "N: " << order << "\n";
71  for(int i=0;i<order;i++) std::cout << coef[i] << " ";
72  std::cout << "\nG: " << gain << "\n";
73 }
74 
75 float fir_filter::apply_filter(float *data, int len)
76 {
77  float sum = 0.0;
78 
79  if(order == 0) return data[len-1];
80 
81  for(int i = len-1, j=0; i >= 0 && j < order; i--, j++)
82  {
83  sum += coef[j]*data[i];
84  }
85 
86  return sum;
87 }
88 
89 } //namespace VisAO
90 
FIR digital filter class declarations.
float apply_filter(float *data, int len)
Apply the filter to the data fector of length len.
Definition: fir_filter.cpp:75
The namespace of VisAO software.