Chris@49: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@52: Sonic Visualiser Chris@52: An audio file viewer and annotation editor. Chris@52: Centre for Digital Music, Queen Mary, University of London. Chris@52: This file copyright 2006 Chris Cannam. Chris@0: Chris@52: This program is free software; you can redistribute it and/or Chris@52: modify it under the terms of the GNU General Public License as Chris@52: published by the Free Software Foundation; either version 2 of the Chris@52: License, or (at your option) any later version. See the file Chris@52: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: #ifndef _WINDOW_H_ Chris@0: #define _WINDOW_H_ Chris@0: Chris@0: #include Chris@0: #include Chris@350: #include Chris@0: #include Chris@0: Chris@0: enum WindowType { Chris@0: RectangularWindow, Chris@0: BartlettWindow, Chris@0: HammingWindow, Chris@0: HanningWindow, Chris@0: BlackmanWindow, Chris@0: GaussianWindow, Chris@142: ParzenWindow, Chris@142: NuttallWindow, Chris@142: BlackmanHarrisWindow Chris@0: }; Chris@0: Chris@0: template Chris@0: class Window Chris@0: { Chris@0: public: Chris@0: /** Chris@0: * Construct a windower of the given type. Chris@0: */ Chris@0: Window(WindowType type, size_t size) : m_type(type), m_size(size) { encache(); } Chris@0: Window(const Window &w) : m_type(w.m_type), m_size(w.m_size) { encache(); } Chris@0: Window &operator=(const Window &w) { Chris@52: if (&w == this) return *this; Chris@0: m_type = w.m_type; Chris@0: m_size = w.m_size; Chris@0: encache(); Chris@0: return *this; Chris@0: } Chris@142: virtual ~Window() { delete[] m_cache; } Chris@0: Chris@0: void cut(T *src) const { cut(src, src); } Chris@0: void cut(T *src, T *dst) const { Chris@0: for (size_t i = 0; i < m_size; ++i) dst[i] = src[i] * m_cache[i]; Chris@0: } Chris@0: Chris@162: T getArea() { return m_area; } Chris@140: T getValue(size_t i) { return m_cache[i]; } Chris@140: Chris@0: WindowType getType() const { return m_type; } Chris@0: size_t getSize() const { return m_size; } Chris@0: Chris@350: // The names used by these functions are un-translated, for use in Chris@350: // e.g. XML I/O. Use Preferences::getPropertyValueLabel if you Chris@350: // want translated names for use in the user interface. Chris@350: static std::string getNameForType(WindowType type); Chris@350: static WindowType getTypeForName(std::string name); Chris@350: Chris@0: protected: Chris@0: WindowType m_type; Chris@0: size_t m_size; Chris@0: T *m_cache; Chris@162: T m_area; Chris@0: Chris@0: void encache(); Chris@142: void cosinewin(T *, T, T, T, T); Chris@0: }; Chris@0: Chris@0: template Chris@0: void Window::encache() Chris@0: { Chris@141: int n = int(m_size); Chris@0: T *mult = new T[n]; Chris@141: int i; Chris@0: for (i = 0; i < n; ++i) mult[i] = 1.0; Chris@0: Chris@0: switch (m_type) { Chris@0: Chris@0: case RectangularWindow: Chris@0: for (i = 0; i < n; ++i) { Chris@141: mult[i] *= 0.5; Chris@0: } Chris@0: break; Chris@0: Chris@0: case BartlettWindow: Chris@0: for (i = 0; i < n/2; ++i) { Chris@141: mult[i] *= (i / T(n/2)); Chris@141: mult[i + n/2] *= (1.0 - (i / T(n/2))); Chris@0: } Chris@0: break; Chris@0: Chris@0: case HammingWindow: Chris@142: cosinewin(mult, 0.54, 0.46, 0.0, 0.0); Chris@0: break; Chris@0: Chris@0: case HanningWindow: Chris@142: cosinewin(mult, 0.50, 0.50, 0.0, 0.0); Chris@0: break; Chris@0: Chris@0: case BlackmanWindow: Chris@142: cosinewin(mult, 0.42, 0.50, 0.08, 0.0); Chris@0: break; Chris@0: Chris@0: case GaussianWindow: Chris@0: for (i = 0; i < n; ++i) { Chris@142: mult[i] *= pow(2, - pow((i - (n-1)/2.0) / ((n-1)/2.0 / 3), 2)); Chris@0: } Chris@0: break; Chris@0: Chris@0: case ParzenWindow: Chris@141: { Chris@141: int N = n-1; Chris@141: for (i = 0; i < N/4; ++i) { Chris@141: T m = 2 * pow(1.0 - (T(N)/2 - i) / (T(N)/2), 3); Chris@141: mult[i] *= m; Chris@141: mult[N-i] *= m; Chris@141: } Chris@141: for (i = N/4; i <= N/2; ++i) { Chris@141: int wn = i - N/2; Chris@141: T m = 1.0 - 6 * pow(wn / (T(N)/2), 2) * (1.0 - abs(wn) / (T(N)/2)); Chris@141: mult[i] *= m; Chris@141: mult[N-i] *= m; Chris@141: } Chris@142: break; Chris@142: } Chris@142: Chris@142: case NuttallWindow: Chris@142: cosinewin(mult, 0.3635819, 0.4891775, 0.1365995, 0.0106411); Chris@0: break; Chris@142: Chris@142: case BlackmanHarrisWindow: Chris@142: cosinewin(mult, 0.35875, 0.48829, 0.14128, 0.01168); Chris@142: break; Chris@0: } Chris@0: Chris@0: m_cache = mult; Chris@162: Chris@162: m_area = 0; Chris@162: for (int i = 0; i < n; ++i) { Chris@162: m_area += m_cache[i]; Chris@162: } Chris@162: m_area /= n; Chris@0: } Chris@0: Chris@142: template Chris@142: void Window::cosinewin(T *mult, T a0, T a1, T a2, T a3) Chris@142: { Chris@142: int n = int(m_size); Chris@142: for (int i = 0; i < n; ++i) { Chris@142: mult[i] *= (a0 Chris@262: - a1 * cos((2 * M_PI * i) / n) Chris@262: + a2 * cos((4 * M_PI * i) / n) Chris@262: - a3 * cos((6 * M_PI * i) / n)); Chris@142: } Chris@142: } Chris@142: Chris@350: template Chris@350: std::string Chris@350: Window::getNameForType(WindowType type) Chris@350: { Chris@350: switch (type) { Chris@350: case RectangularWindow: return "rectangular"; Chris@350: case BartlettWindow: return "bartlett"; Chris@350: case HammingWindow: return "hamming"; Chris@350: case HanningWindow: return "hanning"; Chris@350: case BlackmanWindow: return "blackman"; Chris@350: case GaussianWindow: return "gaussian"; Chris@350: case ParzenWindow: return "parzen"; Chris@350: case NuttallWindow: return "nuttall"; Chris@350: case BlackmanHarrisWindow: return "blackman-harris"; Chris@350: } Chris@350: Chris@350: std::cerr << "WARNING: Window::getNameForType: unknown type " Chris@350: << type << std::endl; Chris@350: Chris@350: return "unknown"; Chris@350: } Chris@350: Chris@350: template Chris@350: WindowType Chris@350: Window::getTypeForName(std::string name) Chris@350: { Chris@350: if (name == "rectangular") return RectangularWindow; Chris@350: if (name == "bartlett") return BartlettWindow; Chris@350: if (name == "hamming") return HammingWindow; Chris@350: if (name == "hanning") return HanningWindow; Chris@350: if (name == "blackman") return BlackmanWindow; Chris@350: if (name == "gaussian") return GaussianWindow; Chris@350: if (name == "parzen") return ParzenWindow; Chris@350: if (name == "nuttall") return NuttallWindow; Chris@350: if (name == "blackman-harris") return BlackmanHarrisWindow; Chris@350: Chris@350: std::cerr << "WARNING: Window::getTypeForName: unknown name \"" Chris@350: << name << "\", defaulting to \"hanning\"" << std::endl; Chris@350: Chris@350: return HanningWindow; Chris@350: } Chris@350: Chris@0: #endif