From 00501fa4333d8c71b317eb438f2bee744e547bc9 Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Thu, 3 Jan 2019 21:13:56 +0100 Subject: [PATCH] added FIR LPF, HPF and BPF --- README.md | 7 +++++++ filter.hh | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 filter.hh diff --git a/README.md b/README.md index 97e6cb7..7847b75 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,13 @@ Implemented are the follwing [Window functions](https://en.wikipedia.org/wiki/Wi * [Gaussian window](https://en.wikipedia.org/wiki/Window_function#Gaussian_window) * [Kaiser window](https://en.wikipedia.org/wiki/Window_function#Kaiser_window) +### [filter.hh](filter.hh) + +Implemented are the following [finite impulse response](https://en.wikipedia.org/wiki/Finite_impulse_response) [filters](https://en.wikipedia.org/wiki/Filter_(signal_processing)): +* [low-pass filter](https://en.wikipedia.org/wiki/Low-pass_filter) +* [high-pass filter](https://en.wikipedia.org/wiki/High-pass_filter) +* [band-pass filter](https://en.wikipedia.org/wiki/Band-pass_filter) + ### [const.hh](const.hh) Some constants we need diff --git a/filter.hh b/filter.hh new file mode 100644 index 0000000..dc0fc0d --- /dev/null +++ b/filter.hh @@ -0,0 +1,61 @@ +/* +Some finite impulse response filter functions + +Copyright 2018 Ahmet Inan +*/ + +#ifndef FILTER_HH +#define FILTER_HH + +#include "const.hh" +#include "utils.hh" +#include "coeffs.hh" + +namespace DSP { + +template +class LowPass : public CoeffsFunc +{ + TYPE f; +public: + LowPass(TYPE cutoff) : f(TYPE(2) * cutoff) {} + TYPE operator () (int n, int N) + { + TYPE x = TYPE(n) - TYPE(0.5) * TYPE(N - 1); + return f * sinc(f * x); + } +}; + +template +class HighPass : public CoeffsFunc +{ + TYPE f; +public: + HighPass(TYPE cutoff) : f(TYPE(2) * cutoff) {} + TYPE operator () (int n, int N) + { + TYPE x = TYPE(n) - TYPE(0.5) * TYPE(N - 1); + // if (N%1) return delta(x) - f * sinc(f * x); + return sinc(x) - f * sinc(f * x); + + } +}; + +template +class BandPass : public CoeffsFunc +{ + TYPE f0, f1; +public: + BandPass(TYPE cutoff0, TYPE cutoff1) : + f0(TYPE(2) * cutoff0), f1(TYPE(2) * cutoff1) {} + TYPE operator () (int n, int N) + { + TYPE x = TYPE(n) - TYPE(0.5) * TYPE(N - 1); + return f1 * sinc(f1 * x) - f0 * sinc(f0 * x); + } +}; + +} + +#endif +