mirror of
https://github.com/f4exb/sdrangel.git
synced 2024-11-23 16:38:37 -05:00
121 lines
2.5 KiB
C
121 lines
2.5 KiB
C
|
#ifndef INCLUDE_BANDPASS_H
|
||
|
#define INCLUDE_BANDPASS_H
|
||
|
|
||
|
#define _USE_MATH_DEFINES
|
||
|
#include <math.h>
|
||
|
#include "dsp/dsptypes.h"
|
||
|
|
||
|
template <class Type> class Bandpass {
|
||
|
public:
|
||
|
Bandpass() { }
|
||
|
|
||
|
void create(int nTaps, double sampleRate, double lowCutoff, double highCutoff)
|
||
|
{
|
||
|
std::vector<Real> taps_lp;
|
||
|
std::vector<Real> taps_hp;
|
||
|
double wcl = 2.0 * M_PI * lowCutoff;
|
||
|
double Wcl = wcl / sampleRate;
|
||
|
double wch = 2.0 * M_PI * highCutoff;
|
||
|
double Wch = wch / sampleRate;
|
||
|
int i;
|
||
|
|
||
|
// check constraints
|
||
|
if(!(nTaps & 1)) {
|
||
|
qDebug("Bandpass filter has to have an odd number of taps");
|
||
|
nTaps++;
|
||
|
}
|
||
|
|
||
|
// make room
|
||
|
m_samples.resize(nTaps);
|
||
|
for(int i = 0; i < nTaps; i++)
|
||
|
m_samples[i] = 0;
|
||
|
m_ptr = 0;
|
||
|
m_taps.resize(nTaps / 2 + 1);
|
||
|
taps_lp.resize(nTaps / 2 + 1);
|
||
|
taps_hp.resize(nTaps / 2 + 1);
|
||
|
|
||
|
// generate Sinc filter core
|
||
|
for(i = 0; i < nTaps / 2 + 1; i++) {
|
||
|
if(i == (nTaps - 1) / 2) {
|
||
|
taps_lp[i] = Wch / M_PI;
|
||
|
taps_hp[i] = -(Wcl / M_PI);
|
||
|
}
|
||
|
else {
|
||
|
taps_lp[i] = sin(((double)i - ((double)nTaps - 1.0) / 2.0) * Wch) / (((double)i - ((double)nTaps - 1.0) / 2.0) * M_PI);
|
||
|
taps_hp[i] = -sin(((double)i - ((double)nTaps - 1.0) / 2.0) * Wcl) / (((double)i - ((double)nTaps - 1.0) / 2.0) * M_PI);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
taps_hp[(nTaps - 1) / 2] += 1;
|
||
|
|
||
|
// apply Hamming window and combine lowpass and highpass
|
||
|
for(i = 0; i < nTaps / 2 + 1; i++) {
|
||
|
taps_lp[i] *= 0.54 + 0.46 * cos((2.0 * M_PI * ((double)i - ((double)nTaps - 1.0) / 2.0)) / (double)nTaps);
|
||
|
taps_hp[i] *= 0.54 + 0.46 * cos((2.0 * M_PI * ((double)i - ((double)nTaps - 1.0) / 2.0)) / (double)nTaps);
|
||
|
m_taps[i] = -(taps_lp[i]+taps_hp[i]);
|
||
|
}
|
||
|
|
||
|
m_taps[(nTaps - 1) / 2] += 1;
|
||
|
|
||
|
// normalize
|
||
|
Real sum = 0;
|
||
|
|
||
|
for(i = 0; i < (int)m_taps.size() - 1; i++) {
|
||
|
sum += m_taps[i] * 2;
|
||
|
}
|
||
|
|
||
|
sum += m_taps[i];
|
||
|
|
||
|
for(i = 0; i < (int)m_taps.size(); i++) {
|
||
|
m_taps[i] /= sum;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Type filter(Type sample)
|
||
|
{
|
||
|
Type acc = 0;
|
||
|
int a = m_ptr;
|
||
|
int b = a - 1;
|
||
|
int i;
|
||
|
|
||
|
m_samples[m_ptr] = sample;
|
||
|
|
||
|
while(b < 0) {
|
||
|
b += m_samples.size();
|
||
|
}
|
||
|
|
||
|
for(i = 0; i < (int)m_taps.size() - 1; i++)
|
||
|
{
|
||
|
acc += (m_samples[a] + m_samples[b]) * m_taps[i];
|
||
|
a++;
|
||
|
|
||
|
while(a >= (int)m_samples.size()) {
|
||
|
a -= m_samples.size();
|
||
|
}
|
||
|
|
||
|
b--;
|
||
|
|
||
|
while(b < 0) {
|
||
|
b += m_samples.size();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
acc += m_samples[a] * m_taps[i];
|
||
|
|
||
|
m_ptr++;
|
||
|
|
||
|
while(m_ptr >= (int)m_samples.size()) {
|
||
|
m_ptr -= m_samples.size();
|
||
|
}
|
||
|
|
||
|
return acc;
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
std::vector<Real> m_taps;
|
||
|
std::vector<Type> m_samples;
|
||
|
int m_ptr;
|
||
|
};
|
||
|
|
||
|
#endif // INCLUDE_BANDPASS_H
|