blob: 377814fc788e6f719fa98b69b2c90cb5da4549ec [file] [log] [blame]
Mathias Agopian4b8a3d82007-08-23 03:16:02 -07001/*
Dan Bornstein4cb4f7c2008-10-03 10:34:57 -07002 * Copyright (C) 2007 The Android Open Source Project
Mathias Agopian4b8a3d82007-08-23 03:16:02 -07003 *
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#include <math.h>
18#include <stdio.h>
19
20static double sinc(double x) {
21 if (fabs(x) == 0.0f) return 1.0f;
22 return sin(x) / x;
23}
24
25static double sqr(double x) {
26 return x*x;
27}
28
29static double I0(double x) {
30 // from the Numerical Recipes in C p. 237
31 double ax,ans,y;
32 ax=fabs(x);
33 if (ax < 3.75) {
34 y=x/3.75;
35 y*=y;
36 ans=1.0+y*(3.5156229+y*(3.0899424+y*(1.2067492
37 +y*(0.2659732+y*(0.360768e-1+y*0.45813e-2)))));
38 } else {
39 y=3.75/ax;
40 ans=(exp(ax)/sqrt(ax))*(0.39894228+y*(0.1328592e-1
41 +y*(0.225319e-2+y*(-0.157565e-2+y*(0.916281e-2
42 +y*(-0.2057706e-1+y*(0.2635537e-1+y*(-0.1647633e-1
43 +y*0.392377e-2))))))));
44 }
45 return ans;
46}
47
48static double kaiser(int k, int N, double alpha) {
49 if (k < 0 || k > N)
50 return 0;
51 return I0(M_PI*alpha * sqrt(1.0 - sqr((2.0*k)/N - 1.0))) / I0(M_PI*alpha);
52}
53
54int main(int argc, char** argv)
55{
56 // nc is the number of bits to store the coefficients
Mathias Agopian2a967b32007-10-29 04:34:36 -070057 int nc = 32;
Mathias Agopian4b8a3d82007-08-23 03:16:02 -070058
59 // ni is the minimum number of bits needed for interpolation
60 // (not used for generating the coefficients)
61 const int ni = nc / 2;
62
Mathias Agopian2a967b32007-10-29 04:34:36 -070063 // cut off frequency ratio Fc/Fs
64 // The bigger the stop-band, the less coefficients we'll need.
Mathias Agopian4b613662007-10-29 23:44:25 -070065 double Fcr = 20000.0 / 48000.0;
Mathias Agopian2a967b32007-10-29 04:34:36 -070066
Mathias Agopian4b8a3d82007-08-23 03:16:02 -070067 // nzc is the number of zero-crossing on one half of the filter
Mathias Agopian4b613662007-10-29 23:44:25 -070068 int nzc = 8;
Mathias Agopian4b8a3d82007-08-23 03:16:02 -070069
70 // alpha parameter of the kaiser window
71 // Larger numbers reduce ripples in the rejection band but increase
Mathias Agopian2a967b32007-10-29 04:34:36 -070072 // the width of the transition band.
73 // the table below gives some value of alpha for a given
74 // stop-band attenuation.
75 // 30 dB 2.210
76 // 40 dB 3.384
77 // 50 dB 4.538
78 // 60 dB 5.658
79 // 70 dB 6.764
80 // 80 dB 7.865
81 // 90 dB 8.960
82 // 100 dB 10.056
Mathias Agopian4b613662007-10-29 23:44:25 -070083 double alpha = 7.865; // -80dB stop-band attenuation
Mathias Agopian4b8a3d82007-08-23 03:16:02 -070084
85 // 2^nz is the number coefficients per zero-crossing
86 // (int theory this should be 1<<(nc/2))
87 const int nz = 4;
Mathias Agopian2a967b32007-10-29 04:34:36 -070088
Mathias Agopian4b8a3d82007-08-23 03:16:02 -070089 // total number of coefficients
Mathias Agopian65682fb2007-08-23 21:01:28 -070090 const int N = (1 << nz) * nzc;
Mathias Agopian4b8a3d82007-08-23 03:16:02 -070091
92 // generate the right half of the filter
Mathias Agopian4b8a3d82007-08-23 03:16:02 -070093 printf("const int32_t RESAMPLE_FIR_SIZE = %d;\n", N);
94 printf("const int32_t RESAMPLE_FIR_NUM_COEF = %d;\n", nzc);
95 printf("const int32_t RESAMPLE_FIR_COEF_BITS = %d;\n", nc);
96 printf("const int32_t RESAMPLE_FIR_LERP_FRAC_BITS = %d;\n", ni);
97 printf("const int32_t RESAMPLE_FIR_LERP_INT_BITS = %d;\n", nz);
98 printf("\n");
99 printf("static int16_t resampleFIR[%d] = {", N);
100 for (int i=0 ; i<N ; i++)
101 {
Mathias Agopian65682fb2007-08-23 21:01:28 -0700102 double x = (2.0 * M_PI * i * Fcr) / (1 << nz);
Mathias Agopian4b8a3d82007-08-23 03:16:02 -0700103 double y = kaiser(i+N, 2*N, alpha) * sinc(x);
104
Mathias Agopian2a967b32007-10-29 04:34:36 -0700105 long yi = floor(y * ((1ULL<<(nc-1))) + 0.5);
106 if (yi >= (1LL<<(nc-1))) yi = (1LL<<(nc-1))-1;
Mathias Agopian4b8a3d82007-08-23 03:16:02 -0700107
Mathias Agopian4b8a3d82007-08-23 03:16:02 -0700108 if ((i % (1 << 4)) == 0) printf("\n ");
Mathias Agopian2a967b32007-10-29 04:34:36 -0700109 if (nc > 16)
110 printf("0x%08x, ", int(yi));
111 else
112 printf("0x%04x, ", int(yi)&0xFFFF);
Mathias Agopian4b8a3d82007-08-23 03:16:02 -0700113 }
114 printf("\n};\n");
Mathias Agopian4b8a3d82007-08-23 03:16:02 -0700115 return 0;
116 }
Mathias Agopian2a967b32007-10-29 04:34:36 -0700117
118// http://www.dsptutor.freeuk.com/KaiserFilterDesign/KaiserFilterDesign.html
119// http://www.csee.umbc.edu/help/sound/AFsp-V2R1/html/audio/ResampAudio.html
120
Dan Bornstein4cb4f7c2008-10-03 10:34:57 -0700121