SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
lowpass.h
Go to the documentation of this file.
1 #ifndef INCLUDE_LOWPASS_H
2 #define INCLUDE_LOWPASS_H
3 
4 #define _USE_MATH_DEFINES
5 #include <math.h>
6 #include "dsp/dsptypes.h"
7 
8 #undef M_PI
9 #define M_PI 3.14159265358979323846
10 
11 template <class Type> class Lowpass {
12 public:
13  Lowpass() : m_ptr(0) { }
14 
15  void create(int nTaps, double sampleRate, double cutoff)
16  {
17  double wc = 2.0 * M_PI * cutoff;
18  double Wc = wc / sampleRate;
19  int i;
20 
21  // check constraints
22  if(!(nTaps & 1)) {
23  qDebug("Lowpass filter has to have an odd number of taps");
24  nTaps++;
25  }
26 
27  // make room
28  m_samples.resize(nTaps);
29  for(int i = 0; i < nTaps; i++)
30  m_samples[i] = 0;
31  m_ptr = 0;
32  m_taps.resize(nTaps / 2 + 1);
33 
34  // generate Sinc filter core
35  for(i = 0; i < nTaps / 2 + 1; i++) {
36  if(i == (nTaps - 1) / 2)
37  m_taps[i] = Wc / M_PI;
38  else
39  m_taps[i] = sin(((double)i - ((double)nTaps - 1.0) / 2.0) * Wc) / (((double)i - ((double)nTaps - 1.0) / 2.0) * M_PI);
40  }
41 
42  // apply Hamming window
43  for(i = 0; i < nTaps / 2 + 1; i++)
44  m_taps[i] *= 0.54 + 0.46 * cos((2.0 * M_PI * ((double)i - ((double)nTaps - 1.0) / 2.0)) / (double)nTaps);
45 
46  // normalize
47  Real sum = 0;
48  for(i = 0; i < (int)m_taps.size() - 1; i++)
49  sum += m_taps[i] * 2;
50  sum += m_taps[i];
51  for(i = 0; i < (int)m_taps.size(); i++)
52  m_taps[i] /= sum;
53  }
54 
55  Type filter(Type sample)
56  {
57  Type acc = 0;
58  int a = m_ptr;
59  int b = a - 1;
60  int i, n_taps, size;
61 
62  m_samples[m_ptr] = sample;
63  size = m_samples.size(); // Valgrind optim (2)
64 
65  while (b < 0)
66  {
67  b += size;
68  }
69 
70  n_taps = m_taps.size() - 1; // Valgrind optim
71 
72  for (i = 0; i < n_taps; i++)
73  {
74  acc += (m_samples[a] + m_samples[b]) * m_taps[i];
75  a++;
76 
77  while (a >= size)
78  {
79  a -= size;
80  }
81 
82  b--;
83 
84  while(b < 0)
85  {
86  b += size;
87  }
88  }
89 
90  acc += m_samples[a] * m_taps[i];
91  m_ptr++;
92 
93  while(m_ptr >= size)
94  {
95  m_ptr -= size;
96  }
97 
98  return acc;
99  }
100 
101 private:
102  std::vector<Real> m_taps;
103  std::vector<Type> m_samples;
104  int m_ptr;
105 };
106 
107 #endif // INCLUDE_LOWPASS_H
Fixed< IntType, IntBits > cos(Fixed< IntType, IntBits > const &x)
Definition: fixed.h:2271
void create(int nTaps, double sampleRate, double cutoff)
Definition: lowpass.h:15
std::vector< Type > m_samples
Definition: lowpass.h:103
Fixed< IntType, IntBits > sin(Fixed< IntType, IntBits > const &x)
Definition: fixed.h:2265
int32_t i
Definition: decimators.h:244
Lowpass()
Definition: lowpass.h:13
int m_ptr
Definition: lowpass.h:104
Type filter(Type sample)
Definition: lowpass.h:55
std::vector< Real > m_taps
Definition: lowpass.h:102
float Real
Definition: dsptypes.h:42
#define M_PI
Definition: lowpass.h:9