1 #ifndef __SDR_FIRFILTER_HH__
2 #define __SDR_FIRFILTER_HH__
16 static inline void coeffs(std::vector<double> &alpha,
double Fl,
double Fu,
double Fs)
18 size_t N = alpha.size();
19 double w = 2*M_PI*Fu/Fs;
20 double M = double(N)/2;
22 for (
size_t i=0; i<N; i++) {
23 if (N == 2*i) { alpha[i] = 4*w/M_PI; }
24 else { alpha[i] = std::sin(w*(i-M))/(w*(i-M)); }
26 alpha[i] *= (0.42 - 0.5*cos((2*M_PI*i)/N) + 0.08*cos((4*M_PI*i)/N));
28 norm += std::abs(alpha[i]);
31 for (
size_t i=0; i<N; i++) { alpha[i] /= norm; }
42 static inline void coeffs(std::vector<double> &alpha,
double Fl,
double Fu,
double Fs)
44 size_t N = alpha.size();
45 double w = 2*M_PI*Fl/Fs;
46 double M = double(N)/2;
48 for (
size_t i=0; i<N; i++) {
49 if (N == 2*i) { alpha[i] = -std::sin(w*(i-M))/(w*(i-N)); }
50 else { alpha[i] = 1-4*w/M_PI; }
52 alpha[i] *= (0.42 - 0.5*cos((2*M_PI*i)/N) + 0.08*cos((4*M_PI*i)/N));
53 norm += std::abs(alpha[i]);
56 for (
size_t i=0; i<N; i++) { alpha[i] /= norm; }
67 static inline void coeffs(std::vector<double> &alpha,
double Fl,
double Fu,
double Fs)
69 size_t N = alpha.size();
70 double wl = 2*M_PI*Fl/Fs;
71 double wu = 2*M_PI*Fu/Fs;
72 double M = double(N)/2;
74 for (
size_t i=0; i<N; i++) {
75 if (N == 2*i) { alpha[i] = 4*(wl-wu)/M_PI; }
76 else { alpha[i] = std::sin(wl*(i-M)/(wl*(i-M))) - std::sin(wu*(i-M)/(wu*(i-M))); }
78 alpha[i] *= (0.42 - 0.5*cos((2*M_PI*i)/N) + 0.08*cos((4*M_PI*i)/N));
79 norm += std::abs(alpha[i]);
82 for (
size_t i=0; i<N; i++) { alpha[i] /= norm; }
93 static inline void coeffs(std::vector<double> &alpha,
double Fl,
double Fu,
double Fs)
95 size_t N = alpha.size();
96 double wl = 2*M_PI*Fl/Fs;
97 double wu = 2*M_PI*Fl/Fs;
98 double M = double(N)/2;
100 for (
size_t i=0; i<N; i++) {
101 if (N == 2*i) { alpha[i] = 1-4*(wl-wu)/M_PI; }
102 else { alpha[i] = std::sin(wu*(i-M)/(wu*(i-M))) - std::sin(wl*(i-M)/(wl*(i-M))); }
104 alpha[i] *= (0.42 - 0.5*cos((2*M_PI*i)/N) + 0.08*cos((4*M_PI*i)/N));
105 norm += std::abs(alpha[i]);
108 for (
size_t i=0; i<N; i++) { alpha[i] /= norm; }
116 template <
class Scalar,
class FilterCoeffs>
144 order = std::max(
size_t(1), order);
145 if (order ==
_order) {
return; }
176 if (Config::typeId<Scalar>() != src_cfg.
type()) {
178 err <<
"Can not configure FIRLowPass: Invalid type " << src_cfg.
type()
179 <<
", expected " << Config::typeId<Scalar>();
198 msg <<
"Configured FIRFilter:" << std::endl
199 <<
" type " << src_cfg.
type() << std::endl
200 <<
" sample rate " <<
_Fs << std::endl
201 <<
" buffer size " << src_cfg.
bufferSize() << std::endl
213 if (0 == buffer.
size()) { }
215 if (!
_enabled) { this->
send(buffer, allow_overwrite);
return; }
218 if (allow_overwrite) {
_process(buffer, buffer); }
223 msg <<
"FIR Filter: Drop buffer, output buffer still in use.";
233 for (
size_t i=0; i<in.
size(); i++) {
240 for (
size_t j=0; j<
_order; j++, idx++) {
241 if (idx == _order) { idx = 0; }
275 template <
class Scalar>
292 template <
class Scalar>
310 template <
class Scalar>
323 template <
class Scalar>
336 #endif // FIRFILTER_HH
Band-stop FIR filter specialization.
Definition: firfilter.hh:324
virtual ~FIRBandStop()
Destructor.
Definition: firfilter.hh:331
A collection of configuration information that is send by a source to all connected sinks to properga...
Definition: node.hh:35
virtual ~FIRFilter()
Destructor.
Definition: firfilter.hh:129
Band-pass FIR filter specialization.
Definition: firfilter.hh:311
static void coeffs(std::vector< double > &alpha, double Fl, double Fu, double Fs)
Calculates the filter coefficients.
Definition: firfilter.hh:93
double _Fu
The upper edge frequency.
Definition: firfilter.hh:259
static void coeffs(std::vector< double > &alpha, double Fl, double Fu, double Fs)
Calculates the filter coefficients.
Definition: firfilter.hh:67
Implements the calculation of the filter coefficients for the use in the FIRFilter template class...
Definition: firfilter.hh:12
double _Fs
Current sample rate.
Definition: firfilter.hh:261
virtual void send(const RawBuffer &buffer, bool allow_overwrite=false)
Sends the given buffer to all connected sinks.
Definition: node.cc:67
void _process(const Buffer< Scalar > &in, const Buffer< Scalar > &out)
performs the actual computation
Definition: firfilter.hh:231
Typed sink.
Definition: node.hh:192
double freq() const
Returns the filter frequency.
Definition: firfilter.hh:302
Definition: autocast.hh:8
bool isEmpty() const
Returns true if the buffer is invalid/empty.
Definition: buffer.hh:77
Definition: operators.hh:9
FIRHighPass(size_t order, double Fc)
Constructor.
Definition: firfilter.hh:297
bool hasSampleRate() const
If true, the configuration has a sample rate.
Definition: node.hh:75
Generic source class.
Definition: node.hh:213
Implements the calculation of the filter coefficients for the use in the FIRFilter template class...
Definition: firfilter.hh:89
virtual ~FIRLowPass()
Destructor.
Definition: firfilter.hh:283
virtual ~FIRHighPass()
Destructor.
Definition: firfilter.hh:300
static void coeffs(std::vector< double > &alpha, double Fl, double Fu, double Fs)
Calculates the filter coefficients.
Definition: firfilter.hh:16
size_t size() const
Returns the number of elements of type T in this buffer.
Definition: buffer.hh:166
bool hasType() const
If true, the configuration has a type.
Definition: node.hh:69
double lowerFreq() const
Returns the lower edge frequency.
Definition: firfilter.hh:156
virtual void setConfig(const Config &config)
Stores the configuration and propergates it if the configuration has been changed.
Definition: node.cc:98
Buffer< Scalar > _ring
A "ring-buffer" used to perform the filtering.
Definition: firfilter.hh:265
Buffer< T > head(size_t n) const
Returns a new view on this buffer.
Definition: buffer.hh:237
FIRLowPass(size_t order, double Fc)
Constructor.
Definition: firfilter.hh:280
Implements the calculation of the filter coefficients for the use in the FIRFilter template class...
Definition: firfilter.hh:63
Implements the calculation of the filter coefficients for the use in the FIRFilter template class...
Definition: firfilter.hh:38
bool _enabled
If true, the filtering is enabled.
Definition: firfilter.hh:253
Generic FIR filter class.
Definition: firfilter.hh:117
void enable(bool enable)
Enable/Disable the filter.
Definition: firfilter.hh:137
FIRBandStop(size_t order, double Fl, double Fu)
Constructor.
Definition: firfilter.hh:328
virtual ~FIRBandPass()
Destructor.
Definition: firfilter.hh:318
void log(const LogMessage &message)
Logs a message.
Definition: logger.cc:100
Type type() const
Returns the type.
Definition: node.hh:71
double freq() const
Returns the filter frequency.
Definition: firfilter.hh:285
FIRBandPass(size_t order, double Fl, double Fu)
Constructor.
Definition: firfilter.hh:315
static void coeffs(std::vector< double > &alpha, double Fl, double Fu, double Fs)
Calculates the filter coefficients.
Definition: firfilter.hh:42
static Logger & get()
Returns the singleton instance of the logger.
Definition: logger.cc:89
The configuration error class.
Definition: exception.hh:24
A log message.
Definition: logger.hh:22
size_t bufferSize() const
Returns the max.
Definition: node.hh:83
size_t _ring_offset
Offset of the "ring-buffer".
Definition: firfilter.hh:267
virtual void setOrder(size_t order)
Sets the order of the filter and updates the filter coefficients.
Definition: firfilter.hh:142
size_t order() const
Returns the order of the filter.
Definition: firfilter.hh:140
double uppertFreq() const
Returns the upper edge frequency.
Definition: firfilter.hh:164
Buffer< Scalar > _buffer
The output buffer, unused if filtering is performed in-place.
Definition: firfilter.hh:269
size_t _order
The order of the filter.
Definition: firfilter.hh:255
virtual void config(const Config &src_cfg)
Configures the filter.
Definition: firfilter.hh:172
bool enabled() const
Returns true if the filter is enabled.
Definition: firfilter.hh:135
void setFreq(double freq)
Sets the filter frequency.
Definition: firfilter.hh:304
std::vector< double > _alpha
The current filter coefficients.
Definition: firfilter.hh:263
void unref()
Dereferences the buffer.
Definition: buffer.cc:63
High-pass FIR filter specialization.
Definition: firfilter.hh:293
bool isUnused() const
We assume here that buffers are owned by one object: A buffer is therefore "unused" if the owner hold...
Definition: buffer.hh:87
void setFreq(double freq)
Sets the filter frequency.
Definition: firfilter.hh:287
void setLowerFreq(double Fl)
Sets the lower edge frequency.
Definition: firfilter.hh:158
double _Fl
The lower edge frequency.
Definition: firfilter.hh:257
void setUpperFreq(double Fu)
Sets the upper edge frequency.
Definition: firfilter.hh:166
Low-pass FIR filter specialization.
Definition: firfilter.hh:276
bool hasBufferSize() const
If true, the configuration has a buffer size.
Definition: node.hh:81
double sampleRate() const
Returns the sample rate.
Definition: node.hh:77
virtual void process(const Buffer< Scalar > &buffer, bool allow_overwrite)
Performs the filtering.
Definition: firfilter.hh:211
FIRFilter(size_t order, double Fl, double Fu)
Constructor.
Definition: firfilter.hh:121