• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef RESAMPLER_HYPERBOLIC_COSINE_WINDOW_H
18 #define RESAMPLER_HYPERBOLIC_COSINE_WINDOW_H
19 
20 #include <math.h>
21 
22 #include "ResamplerDefinitions.h"
23 
24 namespace RESAMPLER_OUTER_NAMESPACE::resampler {
25 
26 /**
27  * Calculate a HyperbolicCosineWindow window centered at 0.
28  * This can be used in place of a Kaiser window.
29  *
30  * The code is based on an anonymous contribution by "a concerned citizen":
31  * https://dsp.stackexchange.com/questions/37714/kaiser-window-approximation
32  */
33 class HyperbolicCosineWindow {
34 public:
HyperbolicCosineWindow()35     HyperbolicCosineWindow() {
36         setStopBandAttenuation(60);
37     }
38 
39     /**
40      * @param attenuation typical values range from 30 to 90 dB
41      * @return beta
42      */
setStopBandAttenuation(double attenuation)43     double setStopBandAttenuation(double attenuation) {
44         double alpha = ((-325.1e-6 * attenuation + 0.1677) * attenuation) - 3.149;
45         setAlpha(alpha);
46         return alpha;
47     }
48 
setAlpha(double alpha)49     void setAlpha(double alpha) {
50         mAlpha = alpha;
51         mInverseCoshAlpha = 1.0 / cosh(alpha);
52     }
53 
54     /**
55      * @param x ranges from -1.0 to +1.0
56      */
operator()57     double operator()(double x) {
58         double x2 = x * x;
59         if (x2 >= 1.0) return 0.0;
60         double w = mAlpha * sqrt(1.0 - x2);
61         return cosh(w) * mInverseCoshAlpha;
62     }
63 
64 private:
65     double mAlpha = 0.0;
66     double mInverseCoshAlpha = 1.0;
67 };
68 
69 } /* namespace RESAMPLER_OUTER_NAMESPACE::resampler */
70 
71 #endif //RESAMPLER_HYPERBOLIC_COSINE_WINDOW_H
72