686d023c6b8324fc4e6bdd26d702d883d2ab0971
[ardour.git] / libs / qm-dsp / base / Window.h
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
22 enum WindowType {
23     RectangularWindow,
24     BartlettWindow,
25     HammingWindow,
26     HanningWindow,
27     BlackmanWindow,
28     GaussianWindow,
29     ParzenWindow
30 };
31
32 template <typename T>
33 class Window
34 {
35 public:
36     /**
37      * Construct a windower of the given type.
38      */
39     Window(WindowType type, size_t size) : m_type(type), m_size(size) { encache(); }
40     Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); }
41     Window &operator=(const Window &w) {
42         if (&w == this) return *this;
43         m_type = w.m_type;
44         m_size = w.m_size;
45         encache();
46         return *this;
47     }
48     virtual ~Window() { delete[] m_cache; }
49
50     void cut(T *src) const { cut(src, src); }
51     void cut(const T *src, T *dst) const {
52         for (size_t i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i];
53     }
54
55     WindowType getType() const { return m_type; }
56     size_t getSize() const { return m_size; }
57
58 protected:
59     WindowType m_type;
60     size_t m_size;
61     T *m_cache;
62
63     void encache();
64 };
65
66 template <typename T>
67 void Window<T>::encache()
68 {
69     size_t n = m_size;
70     T *mult = new T[n];
71     size_t i;
72     for (i = 0; i < n; ++i) mult[i] = 1.0;
73
74     switch (m_type) {
75                 
76     case RectangularWindow:
77         for (i = 0; i < n; ++i) {
78             mult[i] = mult[i] * 0.5;
79         }
80         break;
81         
82     case BartlettWindow:
83         for (i = 0; i < n/2; ++i) {
84             mult[i] = mult[i] * (i / T(n/2));
85             mult[i + n/2] = mult[i + n/2] * (1.0 - (i / T(n/2)));
86         }
87         break;
88         
89     case HammingWindow:
90         for (i = 0; i < n; ++i) {
91             mult[i] = mult[i] * (0.54 - 0.46 * cos(2 * M_PI * i / n));
92         }
93         break;
94         
95     case HanningWindow:
96         for (i = 0; i < n; ++i) {
97             mult[i] = mult[i] * (0.50 - 0.50 * cos(2 * M_PI * i / n));
98         }
99         break;
100         
101     case BlackmanWindow:
102         for (i = 0; i < n; ++i) {
103             mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n)
104                                  + 0.08 * cos(4 * M_PI * i / n));
105         }
106         break;
107         
108     case GaussianWindow:
109         for (i = 0; i < n; ++i) {
110             mult[i] = mult[i] * exp((-1.0 / (n*n)) * ((T(2*i) - n) *
111                                                       (T(2*i) - n)));
112         }
113         break;
114         
115     case ParzenWindow:
116         for (i = 0; i < n; ++i) {
117             mult[i] = mult[i] * (1.0 - fabs((T(2*i) - n) / T(n + 1)));
118         }
119         break;
120     }
121         
122     m_cache = mult;
123 }
124
125 #endif