itpp_sci  1.0.0
it++ based simulation framework for scicoslab, scilab and scipy
spuc_fir.hpp
1 /*xxx
2 //
3 * SPUC - Signal processing using C++ - A DSP library
4 // Copyright(c) 1993-1996 Tony Kirke
5 // author="Tony Kirke" *
6 // Copyright (c) 2014, Tony Kirke. License: MIT License (http://www.opensource.org/licenses/mit-license.php)
7 * \brief - SPUC::fir - (templated) class - from SPUC - with minor modifications
8 */
9 
10 #ifndef SPUC_FIR_HPP
11 #define SPUC_FIR_HPP
12 
13 #include <itpp/itbase.h>
14 
15 #include "copyright.h"
16 #include "spuc.hpp"
17 #include "sim\_sim_exception.hpp"
18 
19 namespace SPUC
20 {
21 
29 template <class Numeric> class fir
30 {
31 public:
33  int num_taps;
35  Numeric* coeff;
36 //protected:
38  Numeric* z;
40  Numeric output;
41 
42 public:
44  ~fir(void)
45  {
46  if(num_taps > 0)
47  {
48  delete [] z;
49  delete [] coeff;
50  }
51  };
52 
54  fir(void) : coeff(NULL), z(NULL)
55  {
56  ;
57  };
58 
60  fir(int n) : coeff(NULL), z(NULL), num_taps(n)
61  {
62  int i;
63  if(n > 0)
64  {
65  coeff = new Numeric[n];
66  z = new Numeric[n];
67  for(i = 0; i < n; i++) z[i] = coeff[i] = 0;
68  }
69  else
70  {
71  throw sim_exception("fir.fir size n<= 0", n);
72  }
73  };
74 
76  void set_size(int n)
77  {
78  int i;
79  num_taps = n;
80  if(n > 0)
81  {
82  coeff = new Numeric[n];
83  z = new Numeric[n];
84  for(i = 0; i < n; i++) z[i] = coeff[i] = 0;
85  }
86  else
87  {
88  throw sim_exception("fir.set_size - size n<= 0", n);
89  }
90 
91  };
92 
94  void set_taps(int i, Numeric tap)
95  {
96  if((i < num_taps) && (i >= 0))
97  {
98  coeff[i] = tap;
99  }
100  else
101  {
102  throw sim_exception("fir.set_taps - bad index ", i);
103  }
104  };
105 
107  void set_taps(Vector<Numeric> v)
108  {
109  set_size(v.length());
110  if(num_taps)
111  {
112  for(int i = 0; i < num_taps; i++) coeff[i] = v[i];
113  }
114  else
115  {
116  throw sim_exception("fir.set_taps - num_taps <= 0", num_taps);
117  }
118 
119  };
120 
122  void reset()
123  {
124  for(int i = 0; i < num_taps; i++) z[i] = 0;
125  output = 0;
126  };
128  Numeric coeff_sum()
129  {
130  int i;
131  Numeric s;
132  for(s = 0, i = 0; i < num_taps; i++) s += coeff[i];
133  return(s);
134  };
135 
137  int get_size(void)
138  {
139  return(num_taps);
140  };
141 
143  Vector<Numeric> get_taps(void)
144  {
145  Vector<Numeric> V(num_taps);
146  for(int i = 0; i < num_taps; i++) V[i] = coeff[i];
147  return(V);
148  };
149 
151  Vector<Numeric> get_input(void)
152  {
153  Vector<Numeric> V(num_taps);
154  for(int i = 0; i < num_taps; i++) V[i] = z[i];
155  return(V);
156  };
157 
158 
160  Numeric out()
161  {
162  return(output);
163  };
164 
166  Numeric check(int i)
167  {
168  if((i < num_taps) && (i >= 0))
169  {
170  return(z[i]);
171  }
172  else
173  {
174  throw sim_exception("fir.check - bad index ", i);
175  }
176 
177  };
178 
180  Numeric clock(Numeric in)
181  {
182  return(update(in));
183  };
184 
186  Numeric update(Numeric in)
187  {
188  int i;
189  // Update history of inputs
190  for(i = num_taps - 1; i > 0; i--) z[i] = z[i - 1];
191  // Add new input
192  z[0] = in;
193  // Perform FIR
194  for(output = 0, i = 0; i < num_taps; i++) output += coeff[i] * z[i];
195  return(output);
196  };
197 
198 }; // class fir
199 
200 } // namespace SPUC
201 #endif
Definition: spuc_fir.hpp:19