annotate src/dsp/KaiserWindow.h @ 172:76b70af3a563

Merge from branch chroma-in-library
author Chris Cannam <c.cannam@qmul.ac.uk>
date Thu, 05 Feb 2015 09:53:06 +0000
parents a38d6940f8fb
children 433e3aac9e52
rev   line source
c@119 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@119 2 /*
c@119 3 Constant-Q library
c@119 4 Copyright (c) 2013-2014 Queen Mary, University of London
c@119 5
c@119 6 Permission is hereby granted, free of charge, to any person
c@119 7 obtaining a copy of this software and associated documentation
c@119 8 files (the "Software"), to deal in the Software without
c@119 9 restriction, including without limitation the rights to use, copy,
c@119 10 modify, merge, publish, distribute, sublicense, and/or sell copies
c@119 11 of the Software, and to permit persons to whom the Software is
c@119 12 furnished to do so, subject to the following conditions:
c@119 13
c@119 14 The above copyright notice and this permission notice shall be
c@119 15 included in all copies or substantial portions of the Software.
c@119 16
c@119 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
c@119 18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
c@119 19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
c@119 20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
c@119 21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
c@119 22 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
c@119 23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
c@119 24
c@119 25 Except as contained in this notice, the names of the Centre for
c@119 26 Digital Music; Queen Mary, University of London; and Chris Cannam
c@119 27 shall not be used in advertising or otherwise to promote the sale,
c@119 28 use or other dealings in this Software without prior written
c@119 29 authorization.
c@119 30 */
c@119 31
c@119 32 #ifndef KAISER_WINDOW_H
c@119 33 #define KAISER_WINDOW_H
c@119 34
c@119 35 #include <vector>
c@119 36 #include <cmath>
c@119 37
c@119 38 /**
c@119 39 * Kaiser window: A windower whose bandwidth and sidelobe height
c@119 40 * (signal-noise ratio) can be specified. These parameters are traded
c@119 41 * off against the window length.
c@119 42 */
c@119 43 class KaiserWindow
c@119 44 {
c@119 45 public:
c@119 46 struct Parameters {
c@119 47 int length;
c@119 48 double beta;
c@119 49 };
c@119 50
c@119 51 /**
c@119 52 * Construct a Kaiser windower with the given length and beta
c@119 53 * parameter.
c@119 54 */
c@119 55 KaiserWindow(Parameters p) : m_length(p.length), m_beta(p.beta) { init(); }
c@119 56
c@119 57 /**
c@119 58 * Construct a Kaiser windower with the given attenuation in dB
c@119 59 * and transition width in samples.
c@119 60 */
c@119 61 static KaiserWindow byTransitionWidth(double attenuation,
c@119 62 double transition) {
c@119 63 return KaiserWindow
c@119 64 (parametersForTransitionWidth(attenuation, transition));
c@119 65 }
c@119 66
c@119 67 /**
c@119 68 * Construct a Kaiser windower with the given attenuation in dB
c@119 69 * and transition bandwidth in Hz for the given samplerate.
c@119 70 */
c@119 71 static KaiserWindow byBandwidth(double attenuation,
c@119 72 double bandwidth,
c@119 73 double samplerate) {
c@119 74 return KaiserWindow
c@119 75 (parametersForBandwidth(attenuation, bandwidth, samplerate));
c@119 76 }
c@119 77
c@119 78 /**
c@119 79 * Obtain the parameters necessary for a Kaiser window of the
c@119 80 * given attenuation in dB and transition width in samples.
c@119 81 */
c@119 82 static Parameters parametersForTransitionWidth(double attenuation,
c@119 83 double transition);
c@119 84
c@119 85 /**
c@119 86 * Obtain the parameters necessary for a Kaiser window of the
c@119 87 * given attenuation in dB and transition bandwidth in Hz for the
c@119 88 * given samplerate.
c@119 89 */
c@119 90 static Parameters parametersForBandwidth(double attenuation,
c@119 91 double bandwidth,
c@119 92 double samplerate) {
c@119 93 return parametersForTransitionWidth
c@119 94 (attenuation, (bandwidth * 2 * M_PI) / samplerate);
c@119 95 }
c@119 96
c@119 97 int getLength() const {
c@119 98 return m_length;
c@119 99 }
c@119 100
c@119 101 const double *getWindow() const {
c@119 102 return m_window.data();
c@119 103 }
c@119 104
c@119 105 void cut(double *src) const {
c@119 106 cut(src, src);
c@119 107 }
c@119 108
c@119 109 void cut(const double *src, double *dst) const {
c@119 110 for (int i = 0; i < m_length; ++i) {
c@119 111 dst[i] = src[i] * m_window[i];
c@119 112 }
c@119 113 }
c@119 114
c@119 115 private:
c@119 116 int m_length;
c@119 117 double m_beta;
c@119 118 std::vector<double> m_window;
c@119 119
c@119 120 void init();
c@119 121 };
c@119 122
c@119 123 #endif