[4668] | 1 | //poly.c: |
---|
[31] | 2 | |
---|
[690] | 3 | /* |
---|
[5823] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2013 |
---|
[690] | 5 | * |
---|
| 6 | * This file is part of libroar a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * libroar is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[690] | 23 | * |
---|
| 24 | * NOTE for everyone want's to change something and send patches: |
---|
| 25 | * read README and HACKING! There a addition information on |
---|
| 26 | * the license of this document you need to read before you send |
---|
| 27 | * any patches. |
---|
| 28 | * |
---|
| 29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 30 | * or libpulse*: |
---|
| 31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 34 | */ |
---|
| 35 | |
---|
[1369] | 36 | #include "libroardsp.h" |
---|
[31] | 37 | |
---|
[34] | 38 | int roar_math_mkpoly (float * poly, float * data, int len) { |
---|
| 39 | if ( len == 2 ) |
---|
| 40 | return roar_math_mkpoly_2x2(poly, data); |
---|
| 41 | if ( len == 3 ) |
---|
| 42 | return roar_math_mkpoly_3x3(poly, data); |
---|
| 43 | if ( len == 4 ) |
---|
| 44 | return roar_math_mkpoly_4x4(poly, data); |
---|
| 45 | if ( len == 5 ) |
---|
| 46 | return roar_math_mkpoly_5x5(poly, data); |
---|
| 47 | |
---|
| 48 | return -1; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | int roar_math_mkpoly_2x2 (float * poly, float * data) { |
---|
| 52 | /* |
---|
| 53 | A B |
---|
| 54 | 0 1 x |
---|
| 55 | 1 1 y |
---|
| 56 | |
---|
| 57 | B = x |
---|
| 58 | A = y - x |
---|
| 59 | */ |
---|
| 60 | |
---|
| 61 | poly[0] = data[0]; |
---|
| 62 | poly[1] = data[1] - data[0]; |
---|
| 63 | |
---|
| 64 | return 0; |
---|
| 65 | } |
---|
[31] | 66 | |
---|
[34] | 67 | int roar_math_mkpoly_3x3 (float * poly, float * data) { |
---|
| 68 | /* |
---|
| 69 | A B C |
---|
| 70 | 0 0 1 x |
---|
| 71 | 1 1 1 y |
---|
| 72 | 4 2 1 z |
---|
| 73 | */ |
---|
| 74 | |
---|
| 75 | poly[0] = data[0]; |
---|
| 76 | poly[1] = 2*data[1] - 2*data[0] - (data[2]-data[0])/2; |
---|
| 77 | poly[2] = (data[2]-data[0])/2 - data[1] + data[0]; |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | return 0; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | int roar_math_mkpoly_4x4 (float * poly, float * data) { |
---|
| 84 | /* |
---|
| 85 | a b c d |
---|
| 86 | 0 0 0 1 A X 0 |
---|
| 87 | 1 1 1 1 B Y 1 |
---|
| 88 | 8 4 2 1 C Z 2 |
---|
| 89 | 27 9 3 1 D Q 3 |
---|
| 90 | */ |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | // { a = -(3z-3y+x-q)/6, b = (4z-5y+2x-q)/2, c = -(9z-18y+11x-2q)/6, d = x }. |
---|
| 94 | |
---|
| 95 | poly[0] = data[0]; |
---|
| 96 | poly[1] = -(11*data[0] - 18*data[1] + 9*data[2] - 2*data[3])/6; |
---|
| 97 | poly[2] = (2 *data[0] - 5*data[1] + 4*data[2] - data[3])/2; |
---|
| 98 | poly[3] = -( data[0] - 3*data[1] + 3*data[2] - data[3])/6; |
---|
| 99 | |
---|
| 100 | return 0; |
---|
| 101 | } |
---|
| 102 | |
---|
[49] | 103 | int roar_math_mkpoly_5x5 (float * poly, float * data) { |
---|
[5270] | 104 | roar_err_set(ROAR_ERROR_NOSYS); |
---|
[49] | 105 | return -1; |
---|
| 106 | } |
---|
[31] | 107 | |
---|
[383] | 108 | |
---|
[2442] | 109 | float roar_math_cvpoly (float * poly, float t, int len) { |
---|
| 110 | float ret = 0; |
---|
| 111 | float ct = 1; |
---|
| 112 | int i; |
---|
| 113 | |
---|
| 114 | if ( poly == NULL ) |
---|
| 115 | return 0; |
---|
| 116 | |
---|
| 117 | switch (len) { |
---|
| 118 | case 4: return roar_math_cvpoly_4x4(poly, t); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | for (i = 0; i < len; i++) { |
---|
| 122 | ret += poly[i] * ct; |
---|
| 123 | ct *= t; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | return ret; |
---|
| 127 | } |
---|
| 128 | |
---|
[383] | 129 | float roar_math_cvpoly_4x4 (float * poly, float t) { |
---|
| 130 | float ret = poly[0]; |
---|
| 131 | float ct = t; |
---|
| 132 | |
---|
| 133 | ret += poly[1] * ct; |
---|
| 134 | ct *= t; |
---|
| 135 | ret += poly[2] * ct; |
---|
| 136 | ct *= t; |
---|
| 137 | ret += poly[3] * ct; |
---|
| 138 | |
---|
| 139 | // printf("ret=%f\n", ret); |
---|
| 140 | |
---|
| 141 | return ret; |
---|
| 142 | } |
---|
| 143 | |
---|
[4879] | 144 | int roar_math_diffpoly(float * poly, int len) { |
---|
| 145 | int i; |
---|
| 146 | |
---|
| 147 | for (i = 1; i < len; i++) { |
---|
| 148 | poly[i-1] = poly[i]*(i+1); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | poly[len-1] = 0; |
---|
| 152 | |
---|
| 153 | return 0; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | int roar_math_intpoly(float * poly, int len, float c) { |
---|
| 157 | int i; |
---|
| 158 | |
---|
[4880] | 159 | for (i = len; i > 0; i--) { |
---|
[4879] | 160 | poly[i] = poly[i-1]/i; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | poly[0] = c; |
---|
| 164 | |
---|
| 165 | return 0; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | float roar_math_numintpoly(float * poly, int len, float st, float et) { |
---|
| 169 | float ipoly[8]; |
---|
| 170 | float upper, lower; |
---|
| 171 | |
---|
[4881] | 172 | if (len > 7) { |
---|
[4879] | 173 | roar_err_set(ROAR_ERROR_RANGE); |
---|
| 174 | return -1.; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | memcpy(ipoly, poly, sizeof(float)*len); |
---|
| 178 | roar_math_intpoly(ipoly, len, 0); |
---|
| 179 | |
---|
[4880] | 180 | // printf("{%f, %f, %f}\n", ipoly[0], ipoly[1], ipoly[2]); |
---|
| 181 | |
---|
[4879] | 182 | lower = roar_math_cvpoly(ipoly, st, len+1); |
---|
| 183 | upper = roar_math_cvpoly(ipoly, et, len+1); |
---|
| 184 | |
---|
[4880] | 185 | // printf("lower(%f)=%f, upper(%f)=%f\n", st, lower, et, upper); |
---|
[4879] | 186 | |
---|
| 187 | return upper-lower; |
---|
| 188 | } |
---|
| 189 | |
---|
[31] | 190 | //ll |
---|