c@349: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@349: c@349: /* c@349: QM DSP library c@349: Centre for Digital Music, Queen Mary, University of London. c@349: c@349: This program is free software; you can redistribute it and/or c@349: modify it under the terms of the GNU General Public License as c@349: published by the Free Software Foundation; either version 2 of the c@349: License, or (at your option) any later version. See the file c@349: COPYING included with this distribution for more information. c@349: */ c@349: c@349: #include "KaiserWindow.h" c@349: c@349: #include "maths/MathUtilities.h" c@349: c@349: KaiserWindow::Parameters c@349: KaiserWindow::parametersForTransitionWidth(double attenuation, cannam@483: double transition) c@349: { c@349: Parameters p; c@349: p.length = 1 + (attenuation > 21.0 ? cannam@483: ceil((attenuation - 7.95) / (2.285 * transition)) : cannam@483: ceil(5.79 / transition)); c@349: p.beta = (attenuation > 50.0 ? cannam@483: 0.1102 * (attenuation - 8.7) : cannam@483: attenuation > 21.0 ? cannam@483: 0.5842 * pow(attenuation - 21.0, 0.4) + 0.07886 * (attenuation - 21.0) : cannam@483: 0); c@349: return p; c@349: } c@349: c@349: static double besselTerm(double x, int i) c@349: { c@349: if (i == 0) { cannam@483: return 1; c@349: } else { cannam@483: double f = MathUtilities::factorial(i); cannam@483: return pow(x/2, i*2) / (f*f); c@349: } c@349: } c@349: c@349: static double bessel0(double x) c@349: { c@349: double b = 0.0; c@349: for (int i = 0; i < 20; ++i) { cannam@483: b += besselTerm(x, i); c@349: } c@349: return b; c@349: } c@349: c@349: void c@349: KaiserWindow::init() c@349: { c@349: double denominator = bessel0(m_beta); c@352: bool even = (m_length % 2 == 0); c@352: for (int i = 0; i < (even ? m_length/2 : (m_length+1)/2); ++i) { cannam@483: double k = double(2*i) / double(m_length-1) - 1.0; cannam@483: m_window.push_back(bessel0(m_beta * sqrt(1.0 - k*k)) / denominator); c@349: } c@352: for (int i = 0; i < (even ? m_length/2 : (m_length-1)/2); ++i) { c@352: m_window.push_back(m_window[int(m_length/2) - i - 1]); c@352: } c@349: }