annotate base/KaiserWindow.h @ 349:b247af4c23d2

Add Kaiser window (and some tests for it)
author Chris Cannam <c.cannam@qmul.ac.uk>
date Fri, 04 Oct 2013 18:46:32 +0100
parents
children 0d3b3c66652b
rev   line source
c@349 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@349 2
c@349 3 /*
c@349 4 QM DSP library
c@349 5 Centre for Digital Music, Queen Mary, University of London.
c@349 6
c@349 7 This program is free software; you can redistribute it and/or
c@349 8 modify it under the terms of the GNU General Public License as
c@349 9 published by the Free Software Foundation; either version 2 of the
c@349 10 License, or (at your option) any later version. See the file
c@349 11 COPYING included with this distribution for more information.
c@349 12 */
c@349 13
c@349 14 #ifndef KAISER_WINDOW_H
c@349 15 #define KAISER_WINDOW_H
c@349 16
c@349 17 #include <vector>
c@349 18 #include <cmath>
c@349 19
c@349 20 class KaiserWindow
c@349 21 {
c@349 22 public:
c@349 23 struct Parameters {
c@349 24 int length;
c@349 25 double beta;
c@349 26 };
c@349 27
c@349 28 /**
c@349 29 * Construct a Kaiser windower with the given length and beta
c@349 30 * parameter.
c@349 31 */
c@349 32 KaiserWindow(Parameters p) : m_length(p.length), m_beta(p.beta) { init(); }
c@349 33
c@349 34 /**
c@349 35 * Construct a Kaiser windower with the given attenuation in dB
c@349 36 * and transition width in samples.
c@349 37 */
c@349 38 static KaiserWindow byTransitionWidth(double attenuation,
c@349 39 double transition) {
c@349 40 return KaiserWindow
c@349 41 (parametersForTransitionWidth(attenuation, transition));
c@349 42 }
c@349 43
c@349 44 /**
c@349 45 * Construct a Kaiser windower with the given attenuation in dB
c@349 46 * and transition bandwidth in Hz for the given samplerate.
c@349 47 */
c@349 48 static KaiserWindow byBandwidth(double attenuation,
c@349 49 double bandwidth,
c@349 50 double samplerate) {
c@349 51 return KaiserWindow
c@349 52 (parametersForBandwidth(attenuation, bandwidth, samplerate));
c@349 53 }
c@349 54
c@349 55 /**
c@349 56 * Obtain the parameters necessary for a Kaiser window of the
c@349 57 * given attenuation in dB and transition width in samples.
c@349 58 */
c@349 59 static Parameters parametersForTransitionWidth(double attenuation,
c@349 60 double transition);
c@349 61
c@349 62 /**
c@349 63 * Obtain the parameters necessary for a Kaiser window of the
c@349 64 * given attenuation in dB and transition bandwidth in Hz for the
c@349 65 * given samplerate.
c@349 66 */
c@349 67 static Parameters parametersForBandwidth(double attenuation,
c@349 68 double bandwidth,
c@349 69 double samplerate) {
c@349 70 return parametersForTransitionWidth
c@349 71 (attenuation, (bandwidth * 2 * M_PI) / samplerate);
c@349 72 }
c@349 73
c@349 74 int getLength() const {
c@349 75 return m_length;
c@349 76 }
c@349 77
c@349 78 const double *getWindow() const {
c@349 79 return m_window.data();
c@349 80 }
c@349 81
c@349 82 void cut(double *src) const {
c@349 83 cut(src, src);
c@349 84 }
c@349 85
c@349 86 void cut(const double *src, double *dst) const {
c@349 87 for (int i = 0; i < m_length; ++i) {
c@349 88 dst[i] = src[i] * m_window[i];
c@349 89 }
c@349 90 }
c@349 91
c@349 92 private:
c@349 93 int m_length;
c@349 94 double m_beta;
c@349 95 std::vector<double> m_window;
c@349 96
c@349 97 void init();
c@349 98 };
c@349 99
c@349 100 #endif