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 QM_DSP_WINDOW_H
16 #define QM_DSP_WINDOW_H
17 
18 #include <cmath>
19 #include <iostream>
20 #include <map>
21 #include <vector>
22 
23 enum WindowType {
24     RectangularWindow,
25     BartlettWindow,
26     HammingWindow,
27     HanningWindow,
28     BlackmanWindow,
29     BlackmanHarrisWindow,
30 
31     FirstWindow = RectangularWindow,
32     LastWindow = BlackmanHarrisWindow
33 };
34 
35 /**
36  * Various shaped windows for sample frame conditioning, including
37  * cosine windows (Hann etc) and triangular and rectangular windows.
38  */
39 template <typename T>
40 class Window
41 {
42 public:
43     /**
44      * Construct a windower of the given type and size.
45      *
46      * Note that the cosine windows are periodic by design, rather
47      * than symmetrical. (A window of size N is equivalent to a
48      * symmetrical window of size N+1 with the final element missing.)
49      */
Window(WindowType type,int size)50     Window(WindowType type, int size) : m_type(type), m_size(size) { encache(); }
Window(const Window & w)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     }
~Window()59     virtual ~Window() { delete[] m_cache; }
60 
cut(T * src)61     void cut(T *src) const { cut(src, src); }
cut(const T * src,T * dst)62     void cut(const T *src, T *dst) const {
63         for (int i = 0; i < m_size; ++i) {
64             dst[i] = src[i] * m_cache[i];
65         }
66     }
67 
getType()68     WindowType getType() const { return m_type; }
getSize()69     int getSize() const { return m_size; }
70 
getWindowData()71     std::vector<T> getWindowData() const {
72         std::vector<T> d;
73         for (int i = 0; i < m_size; ++i) {
74             d.push_back(m_cache[i]);
75         }
76         return d;
77     }
78 
79 protected:
80     WindowType m_type;
81     int m_size;
82     T *m_cache;
83 
84     void encache();
85 };
86 
87 template <typename T>
encache()88 void Window<T>::encache()
89 {
90     int n = m_size;
91     T *mult = new T[n];
92     int i;
93     for (i = 0; i < n; ++i) mult[i] = 1.0;
94 
95     switch (m_type) {
96 
97     case RectangularWindow:
98         for (i = 0; i < n; ++i) {
99             mult[i] = mult[i] * 0.5;
100         }
101         break;
102 
103     case BartlettWindow:
104         if (n == 2) {
105             mult[0] = mult[1] = 0; // "matlab compatible"
106         } else if (n == 3) {
107             mult[0] = 0;
108             mult[1] = mult[2] = 2./3.;
109         } else if (n > 3) {
110             for (i = 0; i < n/2; ++i) {
111                 mult[i] = mult[i] * (i / T(n/2));
112                 mult[i + n - n/2] = mult[i + n - n/2] * (1.0 - (i / T(n/2)));
113             }
114         }
115         break;
116 
117     case HammingWindow:
118         if (n > 1) {
119             for (i = 0; i < n; ++i) {
120                 mult[i] = mult[i] * (0.54 - 0.46 * cos(2 * M_PI * i / n));
121             }
122         }
123         break;
124 
125     case HanningWindow:
126         if (n > 1) {
127             for (i = 0; i < n; ++i) {
128                 mult[i] = mult[i] * (0.50 - 0.50 * cos(2 * M_PI * i / n));
129             }
130         }
131         break;
132 
133     case BlackmanWindow:
134         if (n > 1) {
135             for (i = 0; i < n; ++i) {
136                 mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n)
137                                      + 0.08 * cos(4 * M_PI * i / n));
138             }
139         }
140         break;
141 
142     case BlackmanHarrisWindow:
143         if (n > 1) {
144             for (i = 0; i < n; ++i) {
145                 mult[i] = mult[i] * (0.35875
146                                      - 0.48829 * cos(2 * M_PI * i / n)
147                                      + 0.14128 * cos(4 * M_PI * i / n)
148                                      - 0.01168 * cos(6 * M_PI * i / n));
149             }
150         }
151         break;
152     }
153 
154     m_cache = mult;
155 }
156 
157 #endif
158