forked from grame-cncm/faustlibraries
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreducemaps.lib
More file actions
94 lines (77 loc) · 3.63 KB
/
reducemaps.lib
File metadata and controls
94 lines (77 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/************************************************************************
************************************************************************
FAUST library file
Copyright (C) 2010-2011 GRAME, Centre National de Creation Musicale
---------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
************************************************************************
************************************************************************/
declare name "Reduce Library";
declare author "Yann Orlarey (orlarey at grame.fr)";
declare copyright "Grame";
declare version "0.1";
declare license "LGPL";
ba = library("basics.lib");
//---------------------------------------------------------------
// Provides various operations on block of samples
// using a high order 'reduce(op, n)' fold-like function :
//
// sumn(n) : the sum of a block of n input samples
// maxn(n) : the max of a block of n input samples
// minn(n) : the min of a block of n input samples
// mean(n) : the mean of a block of n input samples
// RMS(n) : the RMS of a block of n input samples
//---------------------------------------------------------------
//---------------------------------------------------------------
// reduce(op, n, x)
//---------------------------------------------------------------
// Fold-like high order function. Apply a binary operation <op>
// on a block of <n> consecutive samples of a signal <x>.
// For example : reduce(max,128) will compute the maximun of each
// block of 128 samples. Please note that the resulting
// value, while produced continuously, will be constant for
// the duration of a block. A new value is only produced
// at the end of a block. Note also that blocks should be of at
// least one sample (n>0).
reduce(op, n, x) = compute ~ (_,_,_) : (!,!,_)
with {
compute(acc, count, val) =
ba.if(count<n, op(acc,x), x), // new acc
ba.if(count<n, count+1, 1), // new count
ba.if(count<n, val, acc); // new val
};
//---------------------------------------------------------------
// reducemap (op, foo, n, x)
//---------------------------------------------------------------
// Like reduce but a foo function is applied to the result. From
// a mathematical point of view :
// reducemap(op,foo,n) is equivalent to reduce(op,n):foo
// but more efficient.
reducemap(op, foo, n, x) = compute ~ (_,_,_) : (!,!,_)
with {
compute(acc, count, val) =
ba.if(count<n, op(acc,x), x), // new acc
ba.if(count<n, count+1, 1), // new count
ba.if(count<n, val, foo(acc)); // new val
};
// the sum of the amplitudes of the input signal
sumn(n) = reduce(+,n);
// the maximum amplitude of the input signal
maxn(n) = reduce(max,n);
// the minimum amplitude of the input signal
minn(n) = reduce(min,n);
// the average amplitude of the input signal
mean(n) = reducemap(+, /(n), n);
// RMS
RMS(n) = float : ^(2) : reducemap(+, (/(n):sqrt), n);