2019-03-17 16:31:42 -04:00
|
|
|
// This file is part of LeanSDR Copyright (C) 2016-2018 <pabr@pabr.org>.
|
|
|
|
// See the toplevel README for more information.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-02-22 16:52:49 -05:00
|
|
|
#ifndef LEANSDR_FILTERGEN_H
|
|
|
|
#define LEANSDR_FILTERGEN_H
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
#include "framework.h"
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
namespace leansdr
|
|
|
|
{
|
|
|
|
namespace filtergen
|
|
|
|
{
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void normalize_power(int n, T *coeffs, float gain = 1)
|
|
|
|
{
|
|
|
|
float s2 = 0;
|
2021-03-21 21:06:26 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < n; ++i) {
|
2021-03-28 23:25:32 -04:00
|
|
|
s2 = s2 + coeffs[i] * coeffs[i]; // TBD std::complex
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s2) {
|
2019-03-17 16:31:42 -04:00
|
|
|
gain /= gen_sqrt(s2);
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < n; ++i) {
|
2019-03-17 16:31:42 -04:00
|
|
|
coeffs[i] = coeffs[i] * gain;
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void normalize_dcgain(int n, T *coeffs, float gain = 1)
|
|
|
|
{
|
|
|
|
float s = 0;
|
2021-03-21 21:06:26 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < n; ++i) {
|
2019-03-17 16:31:42 -04:00
|
|
|
s = s + coeffs[i];
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s) {
|
2019-03-17 16:31:42 -04:00
|
|
|
gain /= s;
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < n; ++i) {
|
2019-03-17 16:31:42 -04:00
|
|
|
coeffs[i] = coeffs[i] * gain;
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
void cancel_dcgain(int n, T *coeffs)
|
|
|
|
{
|
|
|
|
float s = 0;
|
2021-03-21 21:06:26 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < n; ++i) {
|
2019-03-17 16:31:42 -04:00
|
|
|
s = s + coeffs[i];
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < n; ++i) {
|
2019-03-17 16:31:42 -04:00
|
|
|
coeffs[i] -= s / n;
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
// Generate coefficients for a sinc filter.
|
|
|
|
// https://en.wikipedia.org/wiki/Sinc_filter
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
int lowpass(int order, float Fcut, T **coeffs, float gain = 1)
|
|
|
|
{
|
|
|
|
int ncoeffs = order + 1;
|
|
|
|
*coeffs = new T[ncoeffs];
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
for (int i = 0; i < ncoeffs; ++i)
|
|
|
|
{
|
|
|
|
float t = i - (ncoeffs - 1) * 0.5;
|
|
|
|
float sinc = 2 * Fcut * (t ? sin(2 * M_PI * Fcut * t) / (2 * M_PI * Fcut * t) : 1);
|
|
|
|
#if 0 // Hamming
|
2018-02-22 16:52:49 -05:00
|
|
|
float alpha = 25.0/46, beta = 21.0/46;
|
|
|
|
float window = alpha - beta*cos(2*M_PI*i/order);
|
|
|
|
#else
|
2019-03-17 16:31:42 -04:00
|
|
|
float window = 1;
|
2018-02-22 16:52:49 -05:00
|
|
|
#endif
|
2019-03-17 16:31:42 -04:00
|
|
|
(*coeffs)[i] = sinc * window;
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
normalize_dcgain(ncoeffs, *coeffs, gain);
|
|
|
|
return ncoeffs;
|
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
// Generate coefficients for a RRC filter.
|
|
|
|
// https://en.wikipedia.org/wiki/Root-raised-cosine_filter
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
2020-04-20 19:22:50 -04:00
|
|
|
int root_raised_cosine(int order, float Fs, float rolloff, T **coeffs, float gain=1)
|
2019-03-17 16:31:42 -04:00
|
|
|
{
|
|
|
|
float B = rolloff, pi = M_PI;
|
|
|
|
int ncoeffs = (order + 1) | 1;
|
|
|
|
*coeffs = new T[ncoeffs];
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
for (int i = 0; i < ncoeffs; ++i)
|
|
|
|
{
|
|
|
|
int t = i - ncoeffs / 2;
|
|
|
|
float c;
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
if (t == 0)
|
2021-03-21 21:06:26 -04:00
|
|
|
{
|
2020-04-20 19:22:50 -04:00
|
|
|
c = (1 - B + 4*B/pi);
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
float tT = t * Fs;
|
|
|
|
float den = pi * tT * (1 - (4 * B * tT) * (4 * B * tT));
|
2021-03-21 21:06:26 -04:00
|
|
|
|
|
|
|
if (!den) {
|
2020-04-20 19:22:50 -04:00
|
|
|
c = B/sqrtf(2) * ( (1+2/pi)*sinf(pi/(4*B)) + (1-2/pi)*cosf(pi/(4*B)) );
|
2021-03-21 21:06:26 -04:00
|
|
|
} else {
|
2020-04-20 19:22:50 -04:00
|
|
|
c = ( sinf(pi*tT*(1-B)) + 4*B*tT*cosf(pi*tT*(1+B)) ) / den;
|
2021-03-21 21:06:26 -04:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2020-04-20 19:22:50 -04:00
|
|
|
(*coeffs)[i] = Fs * c * gain;
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2021-03-21 21:06:26 -04:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
return ncoeffs;
|
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
// Dump filter coefficients for matlab/octave
|
|
|
|
void dump_filter(const char *name, int ncoeffs, float *coeffs);
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
} // namespace filtergen
|
|
|
|
} // namespace leansdr
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
#endif // LEANSDR_FILTERGEN_H
|