diff --git a/README.md b/README.md index d32df88..b029be5 100644 --- a/README.md +++ b/README.md @@ -10,3 +10,7 @@ What we have included so far: When working with [Floating-point arithmetic](https://en.wikipedia.org/wiki/Floating-point_arithmetic) we soon realize, that addition is not necessarily [associative](https://en.wikipedia.org/wiki/Associative_property). Whenever we need to add numbers with an ever decreasing magnitude to an sum (of the previous numbers) with an ever increasing magnitude, the [Kahan summation algorithm](https://en.wikipedia.org/wiki/Kahan_summation_algorithm) comes in handy and helps keeping the error growth small. +### [window.hh](window.hh) + +Some [Window functions](https://en.wikipedia.org/wiki/Window_function) + diff --git a/window.hh b/window.hh new file mode 100644 index 0000000..ef5f7e4 --- /dev/null +++ b/window.hh @@ -0,0 +1,57 @@ +/* +Some window functions + +Copyright 2018 Ahmet Inan +*/ + +#ifndef WINDOW_HH +#define WINDOW_HH + +namespace DSP { + +template +class Hann +{ + TYPE w[TAPS]; +public: + Hann() + { + for (int n = 0; n < TAPS; ++n) + w[n] = TYPE(0.5) * (TYPE(1) - std::cos(TYPE(2) * TYPE(M_PI) * TYPE(n) / TYPE(TAPS - 1))); + } + inline TYPE operator () (int n) { return n >= 0 && n < TAPS ? w[n] : 0; } + inline operator const TYPE * () const { return w; } +}; + +template +class Hamming +{ + TYPE w[TAPS]; +public: + Hamming() + { + for (int n = 0; n < TAPS; ++n) + w[n] = TYPE(0.54) - TYPE(0.46) * std::cos(TYPE(2) * TYPE(M_PI) * TYPE(n) / TYPE(TAPS - 1)); + } + inline TYPE operator () (int n) { return n >= 0 && n < TAPS ? w[n] : 0; } + inline operator const TYPE * () const { return w; } +}; + +template +class Gauss +{ + TYPE w[TAPS]; +public: + Gauss(TYPE o) + { + for (int n = 0; n < TAPS; ++n) + w[n] = std::exp(- TYPE(0.5) * std::pow((TYPE(n) - TYPE(TAPS - 1) / TYPE(2)) / (o * TYPE(TAPS - 1) / TYPE(2)), TYPE(2))); + } + inline TYPE operator () (int n) { return n >= 0 && n < TAPS ? w[n] : 0; } + inline operator const TYPE * () const { return w; } +}; + +} + +#endif +