HARDT - The Ham Radio DSP Toolkit
hfir.h
1 #ifndef __HFIR_H
2 #define __HFIR_H
3 
5 template <class T>
6 class HFir {
7 
8  private:
9 
10  float *_coefficients;
11  int _length;
12  T *_taps;
13  int _head;
14  int _spacing;
15  int _advance;
16  int _skip;
17 
22  void Init(float* coefficients) {
23 
24  // Copy coefficients
25  _coefficients = new float[_length];
26  SetCoefficients(coefficients, _length);
27  HLog("Copied filter coefficients");
28 
29  // Create delay line
30  _taps = new T[_length];
31  memset((void *) _taps, 0, _length * sizeof(T));
32  HLog("Allocated and initialized delay buffer for %d taps", _length);
33  }
34 
36  inline T Filter(T value) {
37 
38  // Add new sample to the head of the delay line
39  _taps[_head] = value;
40 
41  // Sum all taps
42  float result = 0;
43  for( int k = 0; k < _length; k++ ) {
44  result += _taps[_head++] * _coefficients[k];
45  if( _head == _length ) {
46  _head = 0;
47  }
48  }
49 
50  // Move tip of the delay line ringbuffer
51  _head = _head == 0 ? _length : _head;
52  _head--;
53 
54  // Return the result
55  return result;
56  }
57 
65  inline T Filter(T* value, int advance) {
66 
67  T result = Filter(*value);
68 
69  for( int i = 1; i < advance; i ++ ) {
70 
71  // Add new sample to the head of the delay line
72  _taps[_head] = value[i * _skip];
73 
74  // Move tip of the delay line ringbuffer
75  _head = _head == 0 ? _length : _head;
76  _head--;
77  }
78 
79  // Return the result
80  return result;
81  }
82 
83  public:
84 
90  HFir(float *coefficients, int length) : _length(length), _head(0), _spacing(1), _advance(1), _skip(1) {
91 
92  HLog("HFir(float*, %d)", length);
93  Init(coefficients);
94  }
95 
113  HFir(float *coefficients, int length, int spacing, int advance, int skip):
114  _length(length), _head(0), _spacing(spacing), _advance(advance), _skip(skip) {
115 
116  HLog("HFir(float*, length=%d, spacing=%d, advance=%d, skip=%d)", length, spacing, advance, skip);
117  Init(coefficients);
118  }
119 
123  ~HFir() {
124  HLog("~HFir()");
125  delete[] _taps;
126  delete[] _coefficients;
127  }
128 
135  inline void Filter(T *values, T* result, size_t blocksize) {
136 
137  int j = 0;
138  for( int i = 0; i < blocksize; i += _advance * _skip ) {
139  result[j] = Filter(&values[i], _advance);
140  j += (_spacing - 1) + (_advance * _skip);
141  }
142  }
143 
149  void SetCoefficients(float *coefficients, int length) {
150 
151  // Sanity check
152  if (length != _length) {
153  HError("It is not possible to assign a set of coefficients of different length than the length used at construction (%d)",
154  _length);
155  throw new HFilterInitializationException("Length of coefficients differs from construction length");
156  }
157 
158  // Copy coefficients
159  memcpy(_coefficients, coefficients, length * sizeof(float));
160  }
161 
164  std::vector<float> GetCoefficients() {
165 
166  std::vector<float> coefficients;
167  for (int i = 0; i < _length; i++) {
168  coefficients.push_back(_coefficients[i]);
169  }
170  return coefficients;
171  }
172 };
173 
174 #endif
HFir::GetCoefficients
std::vector< float > GetCoefficients()
Definition: hfir.h:164
HFir::HFir
HFir(float *coefficients, int length, int spacing, int advance, int skip)
Definition: hfir.h:113
HFir::HFir
HFir(float *coefficients, int length)
Definition: hfir.h:90
HFir::~HFir
~HFir()
Definition: hfir.h:123
HFir
Definition: hfir.h:6
HFir::Filter
void Filter(T *values, T *result, size_t blocksize)
Definition: hfir.h:135
HFir::SetCoefficients
void SetCoefficients(float *coefficients, int length)
Definition: hfir.h:149
HFilterInitializationException
Definition: hexceptions.h:165