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