added some window functions

This commit is contained in:
Ahmet Inan 2018-03-02 15:57:07 +01:00
commit 799c09a0fb
2 changed files with 61 additions and 0 deletions

View file

@ -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)

57
window.hh Normal file
View file

@ -0,0 +1,57 @@
/*
Some window functions
Copyright 2018 Ahmet Inan <inan@aicodix.de>
*/
#ifndef WINDOW_HH
#define WINDOW_HH
namespace DSP {
template <int TAPS, typename TYPE>
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 <int TAPS, typename TYPE>
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 <int TAPS, typename TYPE>
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