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.
filtergen.h
Go to the documentation of this file.
1 // This file is part of LeanSDR Copyright (C) 2016-2018 <pabr@pabr.org>.
2 // See the toplevel README for more information.
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef LEANSDR_FILTERGEN_H
18 #define LEANSDR_FILTERGEN_H
19 
20 #include <math.h>
21 
22 #include "framework.h"
23 
24 namespace leansdr
25 {
26 namespace filtergen
27 {
28 
29 template <typename T>
30 void normalize_power(int n, T *coeffs, float gain = 1)
31 {
32  float s2 = 0;
33  for (int i = 0; i < n; ++i)
34  s2 = s2 + coeffs[i] * coeffs[i]; // TBD complex
35  if (s2)
36  gain /= gen_sqrt(s2);
37  for (int i = 0; i < n; ++i)
38  coeffs[i] = coeffs[i] * gain;
39 }
40 
41 template <typename T>
42 void normalize_dcgain(int n, T *coeffs, float gain = 1)
43 {
44  float s = 0;
45  for (int i = 0; i < n; ++i)
46  s = s + coeffs[i];
47  if (s)
48  gain /= s;
49  for (int i = 0; i < n; ++i)
50  coeffs[i] = coeffs[i] * gain;
51 }
52 
53 template <typename T>
54 void cancel_dcgain(int n, T *coeffs)
55 {
56  float s = 0;
57  for (int i = 0; i < n; ++i)
58  s = s + coeffs[i];
59  for (int i = 0; i < n; ++i)
60  coeffs[i] -= s / n;
61 }
62 
63 // Generate coefficients for a sinc filter.
64 // https://en.wikipedia.org/wiki/Sinc_filter
65 
66 template <typename T>
67 int lowpass(int order, float Fcut, T **coeffs, float gain = 1)
68 {
69  int ncoeffs = order + 1;
70  *coeffs = new T[ncoeffs];
71  for (int i = 0; i < ncoeffs; ++i)
72  {
73  float t = i - (ncoeffs - 1) * 0.5;
74  float sinc = 2 * Fcut * (t ? sin(2 * M_PI * Fcut * t) / (2 * M_PI * Fcut * t) : 1);
75 #if 0 // Hamming
76  float alpha = 25.0/46, beta = 21.0/46;
77  float window = alpha - beta*cos(2*M_PI*i/order);
78 #else
79  float window = 1;
80 #endif
81  (*coeffs)[i] = sinc * window;
82  }
83  normalize_dcgain(ncoeffs, *coeffs, gain);
84  return ncoeffs;
85 }
86 
87 // Generate coefficients for a RRC filter.
88 // https://en.wikipedia.org/wiki/Root-raised-cosine_filter
89 
90 template <typename T>
91 int root_raised_cosine(int order, float Fs, float rolloff, T **coeffs)
92 {
93  float B = rolloff, pi = M_PI;
94  int ncoeffs = (order + 1) | 1;
95  *coeffs = new T[ncoeffs];
96  for (int i = 0; i < ncoeffs; ++i)
97  {
98  int t = i - ncoeffs / 2;
99  float c;
100  if (t == 0)
101  c = sqrt(Fs) * (1 - B + 4 * B / pi);
102  else
103  {
104  float tT = t * Fs;
105  float den = pi * tT * (1 - (4 * B * tT) * (4 * B * tT));
106  if (!den)
107  c = B * sqrt(Fs / 2) * ((1 + 2 / pi) * sin(pi / (4 * B)) + (1 - 2 / pi) * cos(pi / (4 * B)));
108  else
109  c = sqrt(Fs) * (sin(pi * tT * (1 - B)) + 4 * B * tT * cos(pi * tT * (1 + B))) / den;
110  }
111  (*coeffs)[i] = c;
112  }
113  normalize_dcgain(ncoeffs, *coeffs);
114  return ncoeffs;
115 }
116 
117 // Dump filter coefficients for matlab/octave
118 void dump_filter(const char *name, int ncoeffs, float *coeffs);
119 
120 } // namespace filtergen
121 } // namespace leansdr
122 
123 #endif // LEANSDR_FILTERGEN_H
float sinc(float x)
Definition: misc.h:32
Fixed< IntType, IntBits > cos(Fixed< IntType, IntBits > const &x)
Definition: fixed.h:2271
void normalize_dcgain(int n, T *coeffs, float gain=1)
Definition: filtergen.h:42
#define M_PI
Definition: rdsdemod.cpp:27
int lowpass(int order, float Fcut, T **coeffs, float gain=1)
Definition: filtergen.h:67
void normalize_power(int n, T *coeffs, float gain=1)
Definition: filtergen.h:30
void dump_filter(const char *name, int ncoeffs, float *coeffs)
Definition: filtergen.cpp:11
Fixed< IntType, IntBits > sin(Fixed< IntType, IntBits > const &x)
Definition: fixed.h:2265
int32_t i
Definition: decimators.h:244
Fixed< IntType, IntBits > sqrt(Fixed< IntType, IntBits > const &x)
Definition: fixed.h:2283
T gen_sqrt(T x)
int root_raised_cosine(int order, float Fs, float rolloff, T **coeffs)
Definition: filtergen.h:91
void cancel_dcgain(int n, T *coeffs)
Definition: filtergen.h:54