Add fft, 2d_fft and sinwave to examples.
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / architecture / math.lib
1 /************************************************************************
2 ************************************************************************
3 FAUST library file
4 Copyright (C) 2003-2011 GRAME, Centre National de Creation Musicale
5 ---------------------------------------------------------------------
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
20 ************************************************************************
21 ************************************************************************/
22
23 declare name "Math Library";
24 declare author "GRAME";
25 declare copyright "GRAME";
26 declare version "1.0";
27 declare license "LGPL";
28
29 //--------------------------------------------------------------------------------
30 // Mathematic library for Faust
31
32 // Implementation of the math.h file as Faust foreign functions
33 //
34 // History
35 // ----------
36 // 28/06/2005 [YO] postfixed functions with 'f' to force float version
37 // instead of double
38 // [YO] removed 'modf' because it requires a pointer as argument
39 //---------------------------------------------------------------------------------
40
41 // -- Utilities and constants
42
43 SR = min(192000, max(1, fconstant(int fSamplingFreq, <math.h>)));
44 BS = fvariable(int count, <math.h>);
45
46 PI = 3.1415926535897932385;
47
48 // -- neg and inv functions
49
50 neg(x) = -x;
51 inv(x) = 1/x;
52
53 // -- Trigonometric Functions
54
55 //acos = ffunction(float acosf (float), <math.h>, "");
56 //asin = ffunction(float asinf (float), <math.h>, "");
57 //atan = ffunction(float atanf (float), <math.h>, "");
58 //atan2 = ffunction(float atan2f (float, float), <math.h>, "");
59
60 //sin = ffunction(float sinf (float), <math.h>, "");
61 //cos = ffunction(float cosf (float), <math.h>, "");
62 //tan = ffunction(float tanf (float), <math.h>,"");
63
64 // -- Exponential Functions
65
66 //exp = ffunction(float expf (float), <math.h>,"");
67 //log = ffunction(float logf (float), <math.h>,"");
68 //log10 = ffunction(float log10f (float), <math.h>,"");
69 //pow = ffunction(float powf (float, float), <math.h>,"");
70 //sqrt = ffunction(float sqrtf (float), <math.h>,"");
71 cbrt = ffunction(float cbrtf (float), <math.h>,"");
72 hypot = ffunction(float hypotf (float, float), <math.h>,"");
73 ldexp = ffunction(float ldexpf (float, int), <math.h>,"");
74 scalb = ffunction(float scalbf (float, float), <math.h>,"");
75 log1p = ffunction(float log1pf (float), <math.h>,"");
76 logb = ffunction(float logbf (float), <math.h>,"");
77 ilogb = ffunction(int ilogbf (float), <math.h>,"");
78 expm1 = ffunction(float expm1f (float), <math.h>,"");
79
80 // -- Hyperbolic Functions
81
82 acosh = ffunction(float acoshf (float), <math.h>, "");
83 asinh = ffunction(float asinhf (float), <math.h>, "");
84 atanh = ffunction(float atanhf (float), <math.h>, "");
85
86 sinh = ffunction(float sinhf (float), <math.h>, "");
87 cosh = ffunction(float coshf (float), <math.h>, "");
88 tanh = ffunction(float tanhf (float), <math.h>,"");
89
90 // -- Remainder Functions
91
92 //fmod = ffunction(float fmodf (float, float),<math.h>,"");
93 //remainder = ffunction(float remainderf (float, float),<math.h>,"");
94
95 // -- Nearest Integer Functions
96
97 //floor = ffunction(float floorf (float), <math.h>,"");
98 //ceil = ffunction(float ceilf (float), <math.h>,"");
99 //rint = ffunction(float rintf (float), <math.h>,"");
100
101 // -- Special Functions
102
103 erf = ffunction(float erff(float), <math.h>,"");
104 erfc = ffunction(float erfcf(float), <math.h>,"");
105 gamma = ffunction(float gammaf(float), <math.h>,"");
106 J0 = ffunction(float j0f(float), <math.h>,"");
107 J1 = ffunction(float j1f(float), <math.h>,"");
108 Jn = ffunction(float jnf(int, float), <math.h>,"");
109 lgamma = ffunction(float lgammaf(float), <math.h>,"");
110 Y0 = ffunction(float y0f(float), <math.h>,"");
111 Y1 = ffunction(float y1f(float), <math.h>,"");
112 Yn = ffunction(float ynf(int, float), <math.h>,"");
113
114
115 // -- Miscellaneous Functions
116
117 //fabs = ffunction(float fabsf (float), <math.h>,"");
118 //fmax = ffunction(float max (float, float),<math.h>,"");
119 //fmin = ffunction(float min (float, float),<math.h>,"");
120
121 fabs = abs;
122 fmax = max;
123 fmin = min;
124
125 isnan = ffunction(int isnan (float),<math.h>,"");
126 nextafter = ffunction(float nextafter(float, float),<math.h>,"");
127
128 // Pattern matching functions to count and access the elements of a list
129 // USAGE : count ((10,20,30,40)) -> 4
130 // take (3,(10,20,30,40)) -> 30
131 //
132
133 count ((xs, xxs)) = 1 + count(xxs);
134 count (xx) = 1;
135
136 take (1, (xs, xxs)) = xs;
137 take (1, xs) = xs;
138 take (nn, (xs, xxs)) = take (nn-1, xxs);
139
140 // linear interpolation between two signals
141 interpolate(i) = *(1.0-i),*(i) : +;
142
143 // if-then-else implemented with a select2.
144 if(cond,thn,els) = select2(cond,els,thn);
145
146
147 //-----------------------------------------------------------------
148 // countdown(count,trig)
149 // start counting down from count, count-1,...,0 when trig > 0
150 //-----------------------------------------------------------------
151 countdown(count, trig) = \(c).(if(trig>0, count, max(0, c-1))) ~_;
152
153 //-----------------------------------------------------------------
154 // countup(count,trig)
155 // start counting down from 0, 1, ... count-1, count when trig > 0
156 //-----------------------------------------------------------------
157 countup(count, trig) = \(c).(if(trig>0, 0, min(count, c+1))) ~_;
158
159 /******************************************************************
160 * Hadamard matrix function
161 * Implementation contributed by Remy Muller
162 *****************************************************************/
163
164 // bus(n) : n parallel cables
165 bus(2) = _,_; // avoids a lot of "bus(1)" labels in block diagrams
166 bus(n) = par(i, n, _);
167
168 // selector(i,n) : select ith cable among n
169 selector(i,n) = par(j, n, S(i, j)) with { S(i,i) = _; S(i,j) = !; };
170
171 // interleave(m,n) : interleave m*n cables : x(0), x(m), x(2m), ..., x(1),x(1+m), x(1+2m)...
172 //interleave(m,n) = bus(m*n) <: par(i, m, par(j, n, selector(i+j*m,m*n)));
173
174 // interleave(row,col) : interleave row*col cables from column order to row order.
175 // input : x(0), x(1), x(2) ..., x(row*col-1)
176 // output: x(0+0*row), x(0+1*row), x(0+2*row), ..., x(1+0*row), x(1+1*row), x(1+2*row), ...
177 interleave(row,col) = bus(row*col) <: par(r, row, par(c, col, selector(r+c*row,row*col)));
178
179 // butterfly(n) : addition then substraction of interleaved signals :
180 butterfly(n) = bus(n) <: interleave(n/2,2), interleave(n/2,2) : par(i, n/2, +), par(i, n/2, -);
181
182 // hadamard(n) : hadamard matrix function of size n = 2^k
183 hadamard(2) = butterfly(2);
184 hadamard(n) = butterfly(n) : (hadamard(n/2) , hadamard(n/2));