diff --git a/coeffs.hh b/coeffs.hh new file mode 100644 index 0000000..d84118c --- /dev/null +++ b/coeffs.hh @@ -0,0 +1,49 @@ +/* +Coefficient array helper + +Copyright 2018 Ahmet Inan +*/ + +#ifndef COEFFS_HH +#define COEFFS_HH + +namespace DSP { + +template +struct CoeffsFunc +{ + virtual TYPE operator () (int, int) = 0; + virtual ~CoeffsFunc() = default; +}; + +template +class Coeffs +{ + TYPE w[TAPS]; +public: + Coeffs(CoeffsFunc *func) + { + for (int n = 0; n < TAPS; ++n) + w[n] = (*func)(n, TAPS); + } + Coeffs(CoeffsFunc *func0, CoeffsFunc *func1) + { + for (int n = 0; n < TAPS; ++n) + w[n] = (*func0)(n, TAPS) * (*func1)(n, TAPS); + } + void normalize(TYPE divisor = 1) + { + TYPE sum(0); + for (int n = 0; n < TAPS; ++n) + sum += w[n]; + for (int n = 0; n < TAPS; ++n) + w[n] /= divisor * std::abs(sum); + } + inline TYPE operator () (int n) { return n >= 0 && n < TAPS ? w[n] : 0; } + inline operator const TYPE * () const { return w; } +}; + +} + +#endif + diff --git a/window.hh b/window.hh index a3c7e5c..c01bf88 100644 --- a/window.hh +++ b/window.hh @@ -9,46 +9,18 @@ Copyright 2018 Ahmet Inan #include "const.hh" #include "kahan.hh" +#include "coeffs.hh" namespace DSP { template -struct WinFunc -{ - virtual TYPE operator () (int, int) = 0; - virtual ~WinFunc() = default; -}; - -template -class Window -{ - TYPE w[TAPS]; -public: - Window(WinFunc *func) - { - for (int n = 0; n < TAPS; ++n) - w[n] = (*func)(n, TAPS); - } - void normalize(TYPE divisor = 1) - { - TYPE sum(0); - for (int n = 0; n < TAPS; ++n) - sum += w[n]; - for (int n = 0; n < TAPS; ++n) - w[n] /= divisor * std::abs(sum); - } - inline TYPE operator () (int n) { return n >= 0 && n < TAPS ? w[n] : 0; } - inline operator const TYPE * () const { return w; } -}; - -template -struct Rect : public WinFunc +struct Rect : public CoeffsFunc { TYPE operator () (int n, int N) { return n >= 0 && n < N ? 1 : 0; } }; template -struct Hann : public WinFunc +struct Hann : public CoeffsFunc { TYPE operator () (int n, int N) { @@ -57,7 +29,7 @@ struct Hann : public WinFunc }; template -struct Hamming : public WinFunc +struct Hamming : public CoeffsFunc { TYPE operator () (int n, int N) { @@ -66,7 +38,7 @@ struct Hamming : public WinFunc }; template -class Lanczos : public WinFunc +class Lanczos : public CoeffsFunc { static TYPE sinc(TYPE x) { @@ -80,7 +52,7 @@ public: }; template -class Blackman : public WinFunc +class Blackman : public CoeffsFunc { TYPE a0, a1, a2; public: @@ -95,7 +67,7 @@ public: }; template -class Gauss : public WinFunc +class Gauss : public CoeffsFunc { TYPE o; public: @@ -107,7 +79,7 @@ public: }; template -class Kaiser : public WinFunc +class Kaiser : public CoeffsFunc { TYPE a; /*