Home | History | Annotate | Line # | Download | only in cosdata-gen
cosdata.c revision 1.3
      1  1.3  christos /* $NetBSD: cosdata.c,v 1.3 2017/01/30 15:50:21 christos Exp $ */
      2  1.1       nat 
      3  1.1       nat /*-
      4  1.1       nat  * Copyright (c) 2015 - 2016 Nathanial Sloss <nathanialsloss (at) yahoo.com.au>
      5  1.1       nat  * All rights reserved.
      6  1.1       nat  *
      7  1.1       nat  *		This software is dedicated to the memory of -
      8  1.1       nat  *	   Baron James Anlezark (Barry) - 1 Jan 1949 - 13 May 2012.
      9  1.1       nat  *
     10  1.1       nat  *		Barry was a man who loved his music.
     11  1.1       nat  *
     12  1.1       nat  * Redistribution and use in source and binary forms, with or without
     13  1.1       nat  * modification, are permitted provided that the following conditions
     14  1.1       nat  * are met:
     15  1.1       nat  * 1. Redistributions of source code must retain the above copyright
     16  1.1       nat  *    notice, this list of conditions and the following disclaimer.
     17  1.1       nat  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.1       nat  *    notice, this list of conditions and the following disclaimer in the
     19  1.1       nat  *    documentation and/or other materials provided with the distribution.
     20  1.1       nat  *
     21  1.1       nat  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  1.1       nat  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  1.1       nat  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  1.1       nat  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  1.1       nat  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  1.1       nat  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  1.1       nat  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  1.1       nat  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  1.1       nat  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  1.1       nat  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  1.1       nat  * POSSIBILITY OF SUCH DAMAGE.
     32  1.1       nat  */
     33  1.1       nat 
     34  1.1       nat #include <math.h>
     35  1.1       nat #include <stdio.h>
     36  1.1       nat 
     37  1.3  christos static const double sbc_coeffs8[] = {
     38  1.1       nat 	0.00000000e+00, 1.56575398e-04, 3.43256425e-04, 5.54620202e-04,
     39  1.1       nat 	8.23919506e-04, 1.13992507e-03, 1.47640169e-03, 1.78371725e-03,
     40  1.1       nat 	2.01182542e-03, 2.10371989e-03, 1.99454554e-03, 1.61656283e-03,
     41  1.1       nat 	9.02154502e-04, -1.78805361e-04, -1.64973098e-03, -3.49717454e-03,
     42  1.1       nat 	5.65949473e-03, 8.02941163e-03, 1.04584443e-02, 1.27472335e-02,
     43  1.1       nat 	1.46525263e-02, 1.59045603e-02, 1.62208471e-02, 1.53184106e-02,
     44  1.1       nat 	1.29371806e-02, 8.85757540e-03, 2.92408442e-03, -4.91578024e-03,
     45  1.1       nat 	-1.46404076e-02, -2.61098752e-02, -3.90751381e-02, -5.31873032e-02,
     46  1.1       nat 	6.79989431e-02, 8.29847578e-02, 9.75753918e-02, 1.11196689e-01,
     47  1.1       nat 	1.23264548e-01, 1.33264415e-01, 1.40753505e-01, 1.45389847e-01,
     48  1.1       nat 	1.46955068e-01, 1.45389847e-01, 1.40753505e-01, 1.33264415e-01,
     49  1.1       nat 	1.23264548e-01, 1.11196689e-01, 9.75753918e-02, 8.29847578e-02,
     50  1.1       nat 	-6.79989431e-02, -5.31873032e-02, -3.90751381e-02, -2.61098752e-02,
     51  1.1       nat 	-1.46404076e-02, -4.91578024e-03, 2.92408442e-03, 8.85757540e-03,
     52  1.1       nat 	1.29371806e-02, 1.53184106e-02, 1.62208471e-02, 1.59045603e-02,
     53  1.1       nat 	1.46525263e-02, 1.27472335e-02, 1.04584443e-02, 8.02941163e-03,
     54  1.1       nat 	-5.65949473e-03, -3.49717454e-03, -1.64973098e-03, -1.78805361e-04,
     55  1.1       nat 	9.02154502e-04, 1.61656283e-03, 1.99454554e-03, 2.10371989e-03,
     56  1.1       nat 	2.01182542e-03, 1.78371725e-03, 1.47640169e-03, 1.13992507e-03,
     57  1.1       nat 	8.23919506e-04, 5.54620202e-04, 3.43256425e-04, 1.56575398e-04,
     58  1.1       nat };
     59  1.1       nat 
     60  1.3  christos static const double sbc_coeffs4[] = {
     61  1.1       nat 	 0.00000000e+00, 5.36548976e-04, 1.49188357e-03, 2.73370904e-03,
     62  1.1       nat 	 3.83720193e-03, 3.89205149e-03, 1.86581691e-03,-3.06012286e-03,
     63  1.1       nat 	 1.09137620e-02, 2.04385087e-02, 2.88757392e-02, 3.21939290e-02,
     64  1.1       nat 	 2.58767811e-02, 6.13245186e-03,-2.88217274e-02,-7.76463494e-02,
     65  1.1       nat 	 1.35593274e-01, 1.94987841e-01, 2.46636662e-01, 2.81828203e-01,
     66  1.1       nat 	 2.94315332e-01, 2.81828203e-01, 2.46636662e-01, 1.94987841e-01,
     67  1.1       nat 	-1.35593274e-01,-7.76463494e-02,-2.88217274e-02, 6.13245186e-03,
     68  1.1       nat 	 2.58767811e-02, 3.21939290e-02, 2.88757392e-02, 2.04385087e-02,
     69  1.1       nat 	-1.09137620e-02,-3.06012286e-03, 1.86581691e-03, 3.89205149e-03,
     70  1.1       nat 	 3.83720193e-03, 2.73370904e-03, 1.49188357e-03, 5.36548976e-04,
     71  1.1       nat };
     72  1.1       nat 
     73  1.3  christos #define COEFFS_MULTI	(1 << 12)
     74  1.3  christos #define SI_MULTI	512
     75  1.1       nat 
     76  1.3  christos static void
     77  1.3  christos arrayprint(const char *name, const double *arr, size_t len)
     78  1.1       nat {
     79  1.3  christos 	size_t k, count;
     80  1.1       nat 
     81  1.3  christos 	printf("static const int32_t %s[] = {\n\t", name);
     82  1.3  christos 	for (count = k = 0; k < len; k++, count++) {
     83  1.1       nat 		if (count % 8 == 0 && count != 0)
     84  1.3  christos 			printf("\n\t");
     85  1.3  christos 		printf("%d, ", (int)roundf((float)arr[k] * COEFFS_MULTI));
     86  1.1       nat 	}
     87  1.1       nat 	printf("\n};\n");
     88  1.3  christos }
     89  1.1       nat 
     90  1.3  christos #define ARRAYPRINT(a)	arrayprint(#a, a, sizeof(a) / sizeof(a[0]))
     91  1.1       nat 
     92  1.3  christos static void
     93  1.3  christos cosprint(int lim, int sgn)
     94  1.3  christos {
     95  1.3  christos 	size_t count, i, k;
     96  1.3  christos 	size_t mi = lim * 4;
     97  1.3  christos 	size_t mk = mi * 2;
     98  1.3  christos 	int dk = sgn * lim * 2;
     99  1.3  christos 	double mp = M_PI_4 / lim;
    100  1.3  christos 	float val;
    101  1.1       nat 
    102  1.3  christos 	printf("static const int32_t cos%sdata%zu[%zu][%zu] = {\n\t",
    103  1.3  christos 	    sgn == -1 ? "" : "dec", mi, mi, mk);
    104  1.1       nat 
    105  1.3  christos 	for (count = 0, i = 0; i < mi; i++) {
    106  1.3  christos 		for (k = 0; k < mk; k++, count++) {
    107  1.1       nat 			if (count % 8 == 0 && count != 0)
    108  1.3  christos 				printf("\n\t");
    109  1.1       nat 			if (k == 0)
    110  1.1       nat 				printf("{ ");
    111  1.1       nat 
    112  1.3  christos 			val = cosf((i + 0.5) * ((int)k + dk) * mp);
    113  1.3  christos 			printf("%d, ", (int)roundf(val * SI_MULTI));
    114  1.1       nat 
    115  1.3  christos 			if (k == mk - 1)
    116  1.1       nat 				printf("},");
    117  1.1       nat 		}
    118  1.1       nat 	}
    119  1.1       nat 	printf("\n};\n");
    120  1.3  christos }
    121  1.1       nat 
    122  1.3  christos int
    123  1.3  christos main(void)
    124  1.3  christos {
    125  1.3  christos 	printf("/* sbc_coeffs.h - Automatically generated by cosdata.c. */\n"
    126  1.3  christos 	    "\n");
    127  1.1       nat 
    128  1.3  christos 	printf("#define\tSIMULTI\t%d\n\n", SI_MULTI);
    129  1.3  christos 	printf("#define\tCOEFFSMULTI\t%d\n\n", COEFFS_MULTI);
    130  1.1       nat 
    131  1.3  christos 	ARRAYPRINT(sbc_coeffs8);
    132  1.3  christos 	ARRAYPRINT(sbc_coeffs4);
    133  1.1       nat 
    134  1.3  christos 	cosprint(2, -1);
    135  1.3  christos 	cosprint(1, -1);
    136  1.3  christos 	cosprint(2, 1);
    137  1.3  christos 	cosprint(1, 1);
    138  1.1       nat 
    139  1.1       nat 	return 0;
    140  1.1       nat }
    141