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 #include "KaiserWindow.h"
|
c@119
|
33
|
c@122
|
34 #include "MathUtilities.h"
|
c@119
|
35
|
c@119
|
36 KaiserWindow::Parameters
|
c@119
|
37 KaiserWindow::parametersForTransitionWidth(double attenuation,
|
c@119
|
38 double transition)
|
c@119
|
39 {
|
c@119
|
40 Parameters p;
|
c@119
|
41 p.length = 1 + (attenuation > 21.0 ?
|
c@119
|
42 ceil((attenuation - 7.95) / (2.285 * transition)) :
|
c@119
|
43 ceil(5.79 / transition));
|
c@119
|
44 p.beta = (attenuation > 50.0 ?
|
c@119
|
45 0.1102 * (attenuation - 8.7) :
|
c@119
|
46 attenuation > 21.0 ?
|
c@119
|
47 0.5842 * pow(attenuation - 21.0, 0.4) + 0.07886 * (attenuation - 21.0) :
|
c@119
|
48 0);
|
c@119
|
49 return p;
|
c@119
|
50 }
|
c@119
|
51
|
c@119
|
52 static double besselTerm(double x, int i)
|
c@119
|
53 {
|
c@119
|
54 if (i == 0) {
|
c@119
|
55 return 1;
|
c@119
|
56 } else {
|
c@119
|
57 double f = MathUtilities::factorial(i);
|
c@119
|
58 return pow(x/2, i*2) / (f*f);
|
c@119
|
59 }
|
c@119
|
60 }
|
c@119
|
61
|
c@119
|
62 static double bessel0(double x)
|
c@119
|
63 {
|
c@119
|
64 double b = 0.0;
|
c@119
|
65 for (int i = 0; i < 20; ++i) {
|
c@119
|
66 b += besselTerm(x, i);
|
c@119
|
67 }
|
c@119
|
68 return b;
|
c@119
|
69 }
|
c@119
|
70
|
c@119
|
71 void
|
c@119
|
72 KaiserWindow::init()
|
c@119
|
73 {
|
c@119
|
74 double denominator = bessel0(m_beta);
|
c@119
|
75 bool even = (m_length % 2 == 0);
|
c@119
|
76 for (int i = 0; i < (even ? m_length/2 : (m_length+1)/2); ++i) {
|
c@119
|
77 double k = double(2*i) / double(m_length-1) - 1.0;
|
c@119
|
78 m_window.push_back(bessel0(m_beta * sqrt(1.0 - k*k)) / denominator);
|
c@119
|
79 }
|
c@119
|
80 for (int i = 0; i < (even ? m_length/2 : (m_length-1)/2); ++i) {
|
c@119
|
81 m_window.push_back(m_window[int(m_length/2) - i - 1]);
|
c@119
|
82 }
|
c@119
|
83 }
|