File: windowfunction.h

package info (click to toggle)
wsclean 2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,196 kB
  • sloc: cpp: 34,504; ansic: 234; python: 174; makefile: 10
file content (74 lines) | stat: -rw-r--r-- 1,853 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef WINDOW_FUNCTION_H
#define WINDOW_FUNCTION_H

#include <cmath>
#include <cstring>

class WindowFunction
{
public:
	enum Type { Rectangular, BlackmanNutall, BlackmanHarris, Hann, Tukey };
	
	static Type GetType(const std::string& typeStr)
	{
		if(typeStr == "hann")
			return Hann;
		else if(typeStr == "blackman-nutall")
			return BlackmanNutall;
		else if(typeStr == "blackman-harris")
			return BlackmanHarris;
		else if(typeStr == "rectangular")
			return Rectangular;
		else if(typeStr == "tukey")
			return Tukey;
		else
			throw std::runtime_error("The window function name is not a valid. Valid windows are: rectangular, hann, blackman-harris or blackman-nutall");
	}

	static double Evaluate(Type windowFunc, size_t n, size_t i)
	{
		switch(windowFunc)
		{
			case Rectangular: return 1.0;
			case BlackmanNutall: return EvaluateBlackmanNutall(n, i);
			case BlackmanHarris: return EvaluateBlackmanHarris(n, i);
			case Tukey: throw std::runtime_error("Tukey window requires parameter");
			case Hann: return EvaluateHann(n, i);
		}
		return 0.0;
	}
	
	static double EvaluateBlackmanNutall(size_t n, size_t i)
	{
		const static double
			a0=0.3635819, a1=0.4891775, a2=0.1365995, a3=0.0106411;
		const double
			id = double(i) * 2.0 * M_PI, nd = int(n);
		return
			a0 -
			a1 * std::cos( (   id   )/nd ) +
			a2 * std::cos( (2.0 * id)/nd ) -
			a3 * std::cos( (3.0 * id)/nd );
	}
	
	static double EvaluateBlackmanHarris(size_t n, size_t i)
	{
		const static double
			a0=0.35875, a1=0.48829, a2=0.14128, a3=0.01168;
		const double
			id = double(i) * 2.0 * M_PI, nd = int(n);
		return
			a0 -
			a1 * std::cos( (   id   )/nd ) +
			a2 * std::cos( (2.0 * id)/nd ) -
			a3 * std::cos( (3.0 * id)/nd );
	}
	
	static double EvaluateHann(size_t n, size_t i)
	{
		double s = std::sin(M_PI * double(i)/(double(n)));
		return s*s;
	}
};

#endif