1
/************************************************************************
2
************************************************************************
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.
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.
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
20
************************************************************************
21
************************************************************************/
23
declare name "Math Library";
24
declare author "GRAME";
25
declare copyright "GRAME";
26
declare version "1.0";
27
declare license "LGPL";
1
29
//--------------------------------------------------------------------------------
2
30
// Mathematic library for Faust
97
125
isnan = ffunction(int isnan (float),<math.h>,"");
98
126
nextafter = ffunction(float nextafter(float, float),<math.h>,"");
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
133
count ((xs, xxs)) = 1 + count(xxs);
136
take (1, (xs, xxs)) = xs;
138
take (nn, (xs, xxs)) = take (nn-1, xxs);
140
// linear interpolation between two signals
141
interpolate(i) = *(1.0-i),*(i) : +;
143
// if-then-else implemented with a select2.
144
if(cond,thn,els) = select2(cond,els,thn);
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))) ~_;
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))) ~_;
159
/******************************************************************
160
* Hadamard matrix function
161
* Implementation contributed by Remy Muller
162
*****************************************************************/
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, _);
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) = !; };
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)));
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)));
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, -);
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));