diff options
Diffstat (limited to 'tools/resampler_tools/fir.cpp')
-rw-r--r-- | tools/resampler_tools/fir.cpp | 69 |
1 files changed, 39 insertions, 30 deletions
diff --git a/tools/resampler_tools/fir.cpp b/tools/resampler_tools/fir.cpp index 62eddca..fe4d212 100644 --- a/tools/resampler_tools/fir.cpp +++ b/tools/resampler_tools/fir.cpp @@ -66,19 +66,20 @@ static double kaiser(int k, int N, double beta) { static void usage(char* name) { fprintf(stderr, - "usage: %s [-h] [-d] [-s sample_rate] [-c cut-off_frequency] [-n half_zero_crossings]" + "usage: %s [-h] [-d] [-D] [-s sample_rate] [-c cut-off_frequency] [-n half_zero_crossings]" " [-f {float|fixed|fixed16}] [-b beta] [-v dBFS] [-l lerp]\n" - " %s [-h] [-d] [-s sample_rate] [-c cut-off_frequency] [-n half_zero_crossings]" + " %s [-h] [-d] [-D] [-s sample_rate] [-c cut-off_frequency] [-n half_zero_crossings]" " [-f {float|fixed|fixed16}] [-b beta] [-v dBFS] -p M/N\n" " -h this help message\n" " -d debug, print comma-separated coefficient table\n" + " -D generate extra declarations\n" " -p generate poly-phase filter coefficients, with sample increment M/N\n" " -s sample rate (48000)\n" " -c cut-off frequency (20478)\n" " -n number of zero-crossings on one side (8)\n" " -l number of lerping bits (4)\n" " -m number of polyphases (related to -l, default 16)\n" - " -f output format, can be fixed-point or floating-point (fixed)\n" + " -f output format, can be fixed, fixed16, or float (fixed)\n" " -b kaiser window parameter beta (7.865 [-80dB])\n" " -v attenuation in dBFS (0)\n", name, name @@ -97,7 +98,8 @@ int main(int argc, char** argv) double Fs = 48000; double Fc = 20478; double atten = 1; - int format = 0; + int format = 0; // 0=fixed, 1=float + bool declarations = false; // in order to keep the errors associated with the linear // interpolation of the coefficients below the quantization error @@ -158,11 +160,14 @@ int main(int argc, char** argv) int M = 1 << 4; // number of phases for interpolation int ch; - while ((ch = getopt(argc, argv, ":hds:c:n:f:l:m:b:p:v:z:")) != -1) { + while ((ch = getopt(argc, argv, ":hds:c:n:f:l:m:b:p:v:z:D")) != -1) { switch (ch) { case 'd': debug = true; break; + case 'D': + declarations = true; + break; case 'p': if (sscanf(optarg, "%u/%u", &polyM, &polyN) != 2) { usage(argv[0]); @@ -225,24 +230,26 @@ int main(int argc, char** argv) for (int i = M-1 ; i; i>>=1, nz++); // generate the right half of the filter if (!debug) { - printf("// cmd-line: "); - for (int i=1 ; i<argc ; i++) { - printf("%s ", argv[i]); + printf("// cmd-line:"); + for (int i=0 ; i<argc ; i++) { + printf(" %s", argv[i]); } printf("\n"); - if (!polyphase) { - printf("const int32_t RESAMPLE_FIR_SIZE = %d;\n", N); - printf("const int32_t RESAMPLE_FIR_INT_PHASES = %d;\n", M); - printf("const int32_t RESAMPLE_FIR_NUM_COEF = %d;\n", nzc); - } else { - printf("const int32_t RESAMPLE_FIR_SIZE = %d;\n", 2*nzc*polyN); - printf("const int32_t RESAMPLE_FIR_NUM_COEF = %d;\n", 2*nzc); - } - if (!format) { - printf("const int32_t RESAMPLE_FIR_COEF_BITS = %d;\n", nc); + if (declarations) { + if (!polyphase) { + printf("const int32_t RESAMPLE_FIR_SIZE = %d;\n", N); + printf("const int32_t RESAMPLE_FIR_INT_PHASES = %d;\n", M); + printf("const int32_t RESAMPLE_FIR_NUM_COEF = %d;\n", nzc); + } else { + printf("const int32_t RESAMPLE_FIR_SIZE = %d;\n", 2*nzc*polyN); + printf("const int32_t RESAMPLE_FIR_NUM_COEF = %d;\n", 2*nzc); + } + if (!format) { + printf("const int32_t RESAMPLE_FIR_COEF_BITS = %d;\n", nc); + } + printf("\n"); + printf("static %s resampleFIR[] = {", !format ? "int32_t" : "float"); } - printf("\n"); - printf("static %s resampleFIR[] = {", !format ? "int32_t" : "float"); } if (!polyphase) { @@ -260,12 +267,15 @@ int main(int argc, char** argv) if (!format) { int64_t yi = toint(y, 1ULL<<(nc-1)); if (nc > 16) { - printf("0x%08x, ", int32_t(yi)); + printf("0x%08x,", int32_t(yi)); } else { - printf("0x%04x, ", int32_t(yi)&0xffff); + printf("0x%04x,", int32_t(yi)&0xffff); } } else { - printf("%.9g%s ", y, debug ? "," : "f,"); + printf("%.9g%s", y, debug ? "," : "f,"); + } + if (j != nzc-1) { + printf(" "); } } } @@ -283,23 +293,22 @@ int main(int argc, char** argv) if (!format) { int64_t yi = toint(y, 1ULL<<(nc-1)); if (nc > 16) { - printf("0x%08x, ", int32_t(yi)); + printf("0x%08x,", int32_t(yi)); } else { - printf("0x%04x, ", int32_t(yi)&0xffff); + printf("0x%04x,", int32_t(yi)&0xffff); } } else { - printf("%.9g%s", y, debug ? "" : "f"); + printf("%.9g%s", y, debug ? "," : "f,"); } - if (debug && (i==nzc-1)) { - } else { - printf(", "); + if (i != nzc-1) { + printf(" "); } } } } - if (!debug) { + if (!debug && declarations) { printf("\n};"); } printf("\n"); |