qm-dsp
1.8
|
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ 00002 00003 /* 00004 QM DSP library 00005 Centre for Digital Music, Queen Mary, University of London. 00006 This file Copyright 2006 Chris Cannam. 00007 00008 This program is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU General Public License as 00010 published by the Free Software Foundation; either version 2 of the 00011 License, or (at your option) any later version. See the file 00012 COPYING included with this distribution for more information. 00013 */ 00014 00015 #ifndef _WINDOW_H_ 00016 #define _WINDOW_H_ 00017 00018 #include <cmath> 00019 #include <iostream> 00020 #include <map> 00021 #include <vector> 00022 00023 enum WindowType { 00024 RectangularWindow, 00025 BartlettWindow, 00026 HammingWindow, 00027 HanningWindow, 00028 BlackmanWindow, 00029 BlackmanHarrisWindow, 00030 00031 FirstWindow = RectangularWindow, 00032 LastWindow = BlackmanHarrisWindow 00033 }; 00034 00039 template <typename T> 00040 class Window 00041 { 00042 public: 00050 Window(WindowType type, int size) : m_type(type), m_size(size) { encache(); } 00051 Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); } 00052 Window &operator=(const Window &w) { 00053 if (&w == this) return *this; 00054 m_type = w.m_type; 00055 m_size = w.m_size; 00056 encache(); 00057 return *this; 00058 } 00059 virtual ~Window() { delete[] m_cache; } 00060 00061 void cut(T *src) const { cut(src, src); } 00062 void cut(const T *src, T *dst) const { 00063 for (int i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i]; 00064 } 00065 00066 WindowType getType() const { return m_type; } 00067 int getSize() const { return m_size; } 00068 00069 std::vector<T> getWindowData() const { 00070 std::vector<T> d; 00071 for (int i = 0; i < m_size; ++i) { 00072 d.push_back(m_cache[i]); 00073 } 00074 return d; 00075 } 00076 00077 protected: 00078 WindowType m_type; 00079 int m_size; 00080 T *m_cache; 00081 00082 void encache(); 00083 }; 00084 00085 template <typename T> 00086 void Window<T>::encache() 00087 { 00088 int n = m_size; 00089 T *mult = new T[n]; 00090 int i; 00091 for (i = 0; i < n; ++i) mult[i] = 1.0; 00092 00093 switch (m_type) { 00094 00095 case RectangularWindow: 00096 for (i = 0; i < n; ++i) { 00097 mult[i] = mult[i] * 0.5; 00098 } 00099 break; 00100 00101 case BartlettWindow: 00102 if (n == 2) { 00103 mult[0] = mult[1] = 0; // "matlab compatible" 00104 } else if (n == 3) { 00105 mult[0] = 0; 00106 mult[1] = mult[2] = 2./3.; 00107 } else if (n > 3) { 00108 for (i = 0; i < n/2; ++i) { 00109 mult[i] = mult[i] * (i / T(n/2)); 00110 mult[i + n - n/2] = mult[i + n - n/2] * (1.0 - (i / T(n/2))); 00111 } 00112 } 00113 break; 00114 00115 case HammingWindow: 00116 if (n > 1) { 00117 for (i = 0; i < n; ++i) { 00118 mult[i] = mult[i] * (0.54 - 0.46 * cos(2 * M_PI * i / n)); 00119 } 00120 } 00121 break; 00122 00123 case HanningWindow: 00124 if (n > 1) { 00125 for (i = 0; i < n; ++i) { 00126 mult[i] = mult[i] * (0.50 - 0.50 * cos(2 * M_PI * i / n)); 00127 } 00128 } 00129 break; 00130 00131 case BlackmanWindow: 00132 if (n > 1) { 00133 for (i = 0; i < n; ++i) { 00134 mult[i] = mult[i] * (0.42 - 0.50 * cos(2 * M_PI * i / n) 00135 + 0.08 * cos(4 * M_PI * i / n)); 00136 } 00137 } 00138 break; 00139 00140 case BlackmanHarrisWindow: 00141 if (n > 1) { 00142 for (i = 0; i < n; ++i) { 00143 mult[i] = mult[i] * (0.35875 00144 - 0.48829 * cos(2 * M_PI * i / n) 00145 + 0.14128 * cos(4 * M_PI * i / n) 00146 - 0.01168 * cos(6 * M_PI * i / n)); 00147 } 00148 } 00149 break; 00150 } 00151 00152 m_cache = mult; 00153 } 00154 00155 #endif