IT++ Logo
window.cpp
Go to the documentation of this file.
1 
30 #include <itpp/base/itcompat.h>
31 #include <itpp/signal/window.h>
32 #include <itpp/signal/poly.h>
33 #include <itpp/base/specmat.h>
34 #include <itpp/base/converters.h>
36 #include <itpp/signal/transforms.h>
37 #include <itpp/base/operators.h>
38 
39 namespace itpp
40 {
41 
42 
43 vec hamming(int n)
44 {
45  vec t(n);
46 
47  if (n == 1)
48  t(0) = 0.08;
49  else
50  for (int i = 0;i < n;i++)
51  t[i] = (0.54 - 0.46 * std::cos(2.0 * pi * i / (n - 1)));
52 
53  return t;
54 }
55 
56 vec hanning(int n)
57 {
58  vec t(n);
59 
60  for (int i = 0;i < n;i++)
61  t(i) = 0.5 * (1.0 - std::cos(2.0 * pi * (i + 1) / (n + 1)));
62 
63  return t;
64 }
65 
66 // matlab version
67 vec hann(int n)
68 {
69  vec t(n);
70 
71  for (int i = 0;i < n;i++)
72  t(i) = 0.5 * (1.0 - std::cos(2.0 * pi * i / (n - 1)));
73 
74  return t;
75 }
76 
77 vec blackman(int n)
78 {
79  vec t(n);
80 
81  for (int i = 0;i < n;i++)
82  t(i) = 0.42 - 0.5 * std::cos(2.0 * pi * i / (n - 1)) + 0.08 * std::cos(4.0 * pi * i / (n - 1));
83 
84  return t;
85 }
86 
87 vec triang(int n)
88 {
89  vec t(n);
90 
91  if (n % 2) { // Odd
92  for (int i = 0; i < n / 2; i++)
93  t(i) = t(n - i - 1) = 2.0 * (i + 1) / (n + 1);
94  t(n / 2) = 1.0;
95  }
96  else
97  for (int i = 0; i < n / 2; i++)
98  t(i) = t(n - i - 1) = (2.0 * i + 1) / n;
99 
100  return t;
101 }
102 
103 vec sqrt_win(int n)
104 {
105  vec t(n);
106 
107  if (n % 2) { // Odd
108  for (int i = 0; i < n / 2; i++)
109  t(i) = t(n - i - 1) = std::sqrt(2.0 * (i + 1) / (n + 1));
110  t(n / 2) = 1.0;
111  }
112  else
113  for (int i = 0; i < n / 2; i++)
114  t(i) = t(n - i - 1) = std::sqrt((2.0 * i + 1) / n);
115 
116  return t;
117 }
118 
119 vec chebwin(int n, double at)
120 {
121  it_assert((n > 0), "chebwin(): need a positive order n!");
122 
123  if (n == 1) {
124  return vec("1");
125  }
126 
127  at = at < 0 ? -at : at;
128  // compute the parameter beta
129  double beta = std::cosh(::acosh(pow10(at / 20)) / (n - 1));
130  vec k = (pi / n) * linspace(0, n - 1, n);
131  vec cos_k = cos(k);
132  // find the window's DFT coefficients
133  vec p = cheb(n - 1, beta * cos_k);
134 
135  vec w(n); // the window vector
136  // Appropriate IDFT and filling up depending on even/odd n
137  if (is_even(n)) {
138  w = ifft_real(to_cvec(elem_mult(p, cos_k), elem_mult(p, -sin(k))));
139  int half_length = n / 2 + 1;
140  w = w.left(half_length) / w(1);
141  w = concat(reverse(w), w.right(n - half_length));
142  }
143  else {
144  w = ifft_real(to_cvec(p));
145  int half_length = (n + 1) / 2;
146  w = w.left(half_length) / w(0);
147  w = concat(reverse(w), w.right(n - half_length));
148  }
149  return w;
150 }
151 
152 
153 } // namespace itpp
vec cosh(const vec &x)
Cosine hyperbolic function.
Definition: trig_hyp.h:93
vec hamming(int n)
Hamming window.
Definition: window.cpp:43
Definitions of window functions.
Vec< T > reverse(const Vec< T > &in)
Reverse the input vector.
Definition: matfunc.h:777
bool is_even(int x)
Return true if x is an even integer.
Definition: misc.h:122
vec chebwin(int n, double at)
Dolph-Chebyshev window.
Definition: window.cpp:119
Mat< Num_T > elem_mult(const Mat< Num_T > &m1, const Mat< Num_T > &m2)
Element wise multiplication of two matrices.
Definition: mat.h:1582
vec triang(int n)
Triangular window.
Definition: window.cpp:87
Polynomial functions.
Vec< Num_T > right(int nr) const
Get the right nr elements from the vector.
Definition: vec.h:1228
ITPP_EXPORT void ifft_real(const cvec &in, vec &out)
Inverse Real Fast Fourier Transform.
#define it_assert(t, s)
Abort if t is not true.
Definition: itassert.h:94
vec hann(int n)
Hanning window compatible with matlab.
Definition: window.cpp:67
double cheb(int n, double x)
Chebyshev polynomial of the first kindChebyshev polynomials of the first kind can be defined as follo...
Definition: poly.cpp:195
vec sin(const vec &x)
Sine function.
Definition: trig_hyp.h:54
Definitions of converters between different vector and matrix types.
Fourier, Hadamard, Walsh-Hadamard, and 2D Hadamard transforms - header file.
const double pi
Constant Pi.
Definition: misc.h:103
vec blackman(int n)
Blackman window.
Definition: window.cpp:77
Trigonometric and hyperbolic functions - header file.
vec acosh(const vec &x)
Inverse cosine hyperbolic function.
Definition: trig_hyp.cpp:40
Definitions of operators for vectors and matricies of different types.
itpp namespace
Definition: itmex.h:36
vec sqrt_win(int n)
Square root window.
Definition: window.cpp:103
double pow10(double x)
Calculate ten to the power of x (10^x)
Definition: log_exp.h:68
IT++ compatibility types and functions.
Definitions of special vectors and matrices.
vec linspace(double from, double to, int points)
linspace (works in the same way as the MATLAB version)
Definition: specmat.cpp:106
vec hanning(int n)
Hanning window.
Definition: window.cpp:56
vec sqrt(const vec &x)
Square root of the elements.
Definition: elem_math.h:123
cvec to_cvec(const Vec< T > &v)
Converts a Vec<T> to cvec.
Definition: converters.h:107
vec cos(const vec &x)
Cosine function.
Definition: trig_hyp.h:58
const Array< T > concat(const Array< T > &a, const T &e)
Append element e to the end of the Array a.
Definition: array.h:486

Generated on Thu Jun 21 2018 16:06:18 for IT++ by Doxygen 1.8.13