Ardour  9.0-pre0-384-ga76afae0e9
Window.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  QM DSP library
5  Centre for Digital Music, Queen Mary, University of London.
6  This file Copyright 2006 Chris Cannam.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version. See the file
12  COPYING included with this distribution for more information.
13 */
14 
15 #ifndef _WINDOW_H_
16 #define _WINDOW_H_
17 
18 #include <cmath>
19 #include <iostream>
20 #include <map>
21 #include <vector>
22 
23 enum WindowType {
30 
33 };
34 
39 template <typename T>
40 class Window
41 {
42 public:
50  Window(WindowType type, int size) : m_type(type), m_size(size) { encache(); }
51  Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); }
52  Window &operator=(const Window &w) {
53  if (&w == this) return *this;
54  m_type = w.m_type;
55  m_size = w.m_size;
56  encache();
57  return *this;
58  }
59  virtual ~Window() { delete[] m_cache; }
60 
61  void cut(T *src) const { cut(src, src); }
62  void cut(const T *src, T *dst) const {
63  for (int i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i];
64  }
65 
66  WindowType getType() const { return m_type; }
67  int getSize() const { return m_size; }
68 
69  std::vector<T> getWindowData() const {
70  std::vector<T> d;
71  for (int i = 0; i < m_size; ++i) {
72  d.push_back(m_cache[i]);
73  }
74  return d;
75  }
76 
77 protected:
79  int m_size;
80  T *m_cache;
81 
82  void encache();
83 };
84 
85 template <typename T>
87 {
88  int n = m_size;
89  T *mult = new T[n];
90  int i;
91  for (i = 0; i < n; ++i) mult[i] = 1.0;
92 
93  switch (m_type) {
94 
95  case RectangularWindow:
96  for (i = 0; i < n; ++i) {
97  mult[i] = mult[i] * 0.5;
98  }
99  break;
100 
101  case BartlettWindow:
102  if (n == 2) {
103  mult[0] = mult[1] = 0; // "matlab compatible"
104  } else if (n == 3) {
105  mult[0] = 0;
106  mult[1] = mult[2] = 2./3.;
107  } else if (n > 3) {
108  for (i = 0; i < n/2; ++i) {
109  mult[i] = mult[i] * (i / T(n/2));
110  mult[i + n - n/2] = mult[i + n - n/2] * (1.0 - (i / T(n/2)));
111  }
112  }
113  break;
114 
115  case HammingWindow:
116  if (n > 1) {
117  for (i = 0; i < n; ++i) {
118  mult[i] = mult[i] * (0.54 - 0.46 * cos(2 * M_PI * i / n));
119  }
120  }
121  break;
122 
123  case HanningWindow:
124  if (n > 1) {
125  for (i = 0; i < n; ++i) {
126  mult[i] = mult[i] * (0.50 - 0.50 * cos(2 * M_PI * i / n));
127  }
128  }
129  break;
130 
131  case BlackmanWindow:
132  if (n > 1) {
133  for (i = 0; i < n; ++i) {
134  mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n)
135  + 0.08 * cos(4 * M_PI * i / n));
136  }
137  }
138  break;
139 
141  if (n > 1) {
142  for (i = 0; i < n; ++i) {
143  mult[i] = mult[i] * (0.35875
144  - 0.48829 * cos(2 * M_PI * i / n)
145  + 0.14128 * cos(4 * M_PI * i / n)
146  - 0.01168 * cos(6 * M_PI * i / n));
147  }
148  }
149  break;
150  }
151 
152  m_cache = mult;
153 }
154 
155 #endif
WindowType
Definition: Window.h:23
@ HanningWindow
Definition: Window.h:27
@ HammingWindow
Definition: Window.h:26
@ FirstWindow
Definition: Window.h:31
@ RectangularWindow
Definition: Window.h:24
@ BlackmanHarrisWindow
Definition: Window.h:29
@ BartlettWindow
Definition: Window.h:25
@ BlackmanWindow
Definition: Window.h:28
@ LastWindow
Definition: Window.h:32
Definition: Window.h:41
WindowType getType() const
Definition: Window.h:66
Window & operator=(const Window &w)
Definition: Window.h:52
Window(WindowType type, int size)
Definition: Window.h:50
Window(const Window &w)
Definition: Window.h:51
void encache()
Definition: Window.h:86
WindowType m_type
Definition: Window.h:78
T * m_cache
Definition: Window.h:80
void cut(T *src) const
Definition: Window.h:61
int getSize() const
Definition: Window.h:67
std::vector< T > getWindowData() const
Definition: Window.h:69
int m_size
Definition: Window.h:79
virtual ~Window()
Definition: Window.h:59
void cut(const T *src, T *dst) const
Definition: Window.h:62