1 | //math.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
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 |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | * NOTE for everyone want's to change something and send patches: |
---|
24 | * read README and HACKING! There a addition information on |
---|
25 | * the license of this document you need to read before you send |
---|
26 | * any patches. |
---|
27 | * |
---|
28 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
29 | * or libpulse*: |
---|
30 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
31 | * and are therefore GPL. Because of this it may be illigal to use |
---|
32 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
33 | */ |
---|
34 | |
---|
35 | #include "libroardsp.h" |
---|
36 | |
---|
37 | int roar_math_mkpoly (float * poly, float * data, int len) { |
---|
38 | if ( len == 2 ) |
---|
39 | return roar_math_mkpoly_2x2(poly, data); |
---|
40 | if ( len == 3 ) |
---|
41 | return roar_math_mkpoly_3x3(poly, data); |
---|
42 | if ( len == 4 ) |
---|
43 | return roar_math_mkpoly_4x4(poly, data); |
---|
44 | if ( len == 5 ) |
---|
45 | return roar_math_mkpoly_5x5(poly, data); |
---|
46 | |
---|
47 | return -1; |
---|
48 | } |
---|
49 | |
---|
50 | int roar_math_mkpoly_2x2 (float * poly, float * data) { |
---|
51 | /* |
---|
52 | A B |
---|
53 | 0 1 x |
---|
54 | 1 1 y |
---|
55 | |
---|
56 | B = x |
---|
57 | A = y - x |
---|
58 | */ |
---|
59 | |
---|
60 | poly[0] = data[0]; |
---|
61 | poly[1] = data[1] - data[0]; |
---|
62 | |
---|
63 | return 0; |
---|
64 | } |
---|
65 | |
---|
66 | int roar_math_mkpoly_3x3 (float * poly, float * data) { |
---|
67 | /* |
---|
68 | A B C |
---|
69 | 0 0 1 x |
---|
70 | 1 1 1 y |
---|
71 | 4 2 1 z |
---|
72 | */ |
---|
73 | |
---|
74 | poly[0] = data[0]; |
---|
75 | poly[1] = 2*data[1] - 2*data[0] - (data[2]-data[0])/2; |
---|
76 | poly[2] = (data[2]-data[0])/2 - data[1] + data[0]; |
---|
77 | |
---|
78 | |
---|
79 | return 0; |
---|
80 | } |
---|
81 | |
---|
82 | int roar_math_mkpoly_4x4 (float * poly, float * data) { |
---|
83 | /* |
---|
84 | a b c d |
---|
85 | 0 0 0 1 A X 0 |
---|
86 | 1 1 1 1 B Y 1 |
---|
87 | 8 4 2 1 C Z 2 |
---|
88 | 27 9 3 1 D Q 3 |
---|
89 | */ |
---|
90 | |
---|
91 | |
---|
92 | // { a = -(3z-3y+x-q)/6, b = (4z-5y+2x-q)/2, c = -(9z-18y+11x-2q)/6, d = x }. |
---|
93 | |
---|
94 | poly[0] = data[0]; |
---|
95 | poly[1] = -(11*data[0] - 18*data[1] + 9*data[2] - 2*data[3])/6; |
---|
96 | poly[2] = (2 *data[0] - 5*data[1] + 4*data[2] - data[3])/2; |
---|
97 | poly[3] = -( data[0] - 3*data[1] + 3*data[2] - data[3])/6; |
---|
98 | |
---|
99 | return 0; |
---|
100 | } |
---|
101 | |
---|
102 | int roar_math_mkpoly_5x5 (float * poly, float * data) { |
---|
103 | return -1; |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | float roar_math_cvpoly_4x4 (float * poly, float t) { |
---|
108 | float ret = poly[0]; |
---|
109 | float ct = t; |
---|
110 | |
---|
111 | ret += poly[1] * ct; |
---|
112 | ct *= t; |
---|
113 | ret += poly[2] * ct; |
---|
114 | ct *= t; |
---|
115 | ret += poly[3] * ct; |
---|
116 | |
---|
117 | // printf("ret=%f\n", ret); |
---|
118 | |
---|
119 | return ret; |
---|
120 | } |
---|
121 | |
---|
122 | //ll |
---|