Update configure and Makefiles for the corrected name "interpreter".
[Faustine.git] / interpreter / preprocessor / faust-0.9.47mr3 / architecture / reduce.lib
1 /************************************************************************
2 ************************************************************************
3 FAUST library file
4 Copyright (C) 2010-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 "Reduce Library";
24 declare author "Yann Orlarey (orlarey at grame.fr)";
25 declare copyright "Grame";
26 declare version "0.1";
27 declare license "LGPL";
28
29
30 //---------------------------------------------------------------
31 // Provides various operations on block of samples
32 // using a high order 'reduce(op, n)' fold-like function :
33 //
34 // sumn(n) : the sum of a block of n input samples
35 // maxn(n) : the max of a block of n input samples
36 // minn(n) : the min of a block of n input samples
37 // mean(n) : the mean of a block of n input samples
38 // RMS(n) : the RMS of a block of n input samples
39 //---------------------------------------------------------------
40
41
42
43 //---------------------------------------------------------------
44 // reduce (op, n, x)
45 //---------------------------------------------------------------
46 // Fold-like high order function. Apply a binary operation <op>
47 // on a block of <n> consecutive samples of a signal <x>.
48 // For example : reduce(max,128) will compute the maximun of each
49 // block of 128 samples. Please note that the resulting
50 // value, while produced continuously, will be constant for
51 // the duration of a block. A new value is only produced
52 // at the end of a block. Note also that blocks should be of at
53 // least one sample (n>0).
54 reduce(op, n, x) = compute ~ (_,_,_) : (!,!,_)
55 with {
56 compute (acc, count, val) =
57 if(count<n, op(acc,x), x), // new acc
58 if(count<n, count+1, 1), // new count
59 if(count<n, val, acc); // new val
60 if (c, then, else) = select2(c, else, then);
61 };
62
63
64 //---------------------------------------------------------------
65 // reducemap (op, foo, n, x)
66 //---------------------------------------------------------------
67 // Like reduce but a foo function is applied to the result. From
68 // a mathematical point of view :
69 // reducemap(op,foo,n) is equivalent to reduce(op,n):foo
70 // but more efficient.
71 reducemap(op, foo, n, x) = compute ~ (_,_,_) : (!,!,_)
72 with {
73 compute (acc, count, val) =
74 if(count<n, op(acc,x), x), // new acc
75 if(count<n, count+1, 1), // new count
76 if(count<n, val, foo(acc)); // new val
77 if (c, then, else) = select2(c, else, then);
78 };
79
80
81 // the sum of the amplitudes of the input signal
82 sumn(n) = reduce(+,n);
83
84 // the maximum amplitude of the input signal
85 maxn(n) = reduce(max,n);
86
87 // the minimum amplitude of the input signal
88 minn(n) = reduce(min,n);
89
90 // the average amplitude of the input signal
91 mean(n) = reducemap(+, /(n), n);
92
93 // RMS
94 RMS(n) = float : ^(2) : reducemap(+, (/(n):sqrt), n);
95