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