tomwalters@0
|
1 /*
|
tomwalters@0
|
2 Copyright (c) Applied Psychology Unit, Medical Research Council. 1988, 1989
|
tomwalters@0
|
3 ===========================================================================
|
tomwalters@0
|
4
|
tomwalters@0
|
5 Permission to use, copy, modify, and distribute this software without fee
|
tomwalters@0
|
6 is hereby granted for research purposes, provided that this copyright
|
tomwalters@0
|
7 notice appears in all copies and in all supporting documentation, and that
|
tomwalters@0
|
8 the software is not redistributed for any fee (except for a nominal shipping
|
tomwalters@0
|
9 charge). Anyone wanting to incorporate all or part of this software in a
|
tomwalters@0
|
10 commercial product must obtain a license from the Medical Research Council.
|
tomwalters@0
|
11
|
tomwalters@0
|
12 The MRC makes no representations about the suitability of this
|
tomwalters@0
|
13 software for any purpose. It is provided "as is" without express or implied
|
tomwalters@0
|
14 warranty.
|
tomwalters@0
|
15
|
tomwalters@0
|
16 THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
|
tomwalters@0
|
17 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE
|
tomwalters@0
|
18 A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
|
tomwalters@0
|
19 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
|
tomwalters@0
|
20 AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
tomwalters@0
|
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
tomwalters@0
|
22 */
|
tomwalters@0
|
23
|
tomwalters@0
|
24 /*
|
tomwalters@0
|
25 funcs.c
|
tomwalters@0
|
26 =======
|
tomwalters@0
|
27
|
tomwalters@0
|
28
|
tomwalters@0
|
29
|
tomwalters@0
|
30
|
tomwalters@0
|
31 */
|
tomwalters@0
|
32
|
tomwalters@0
|
33 #include <math.h>
|
tomwalters@0
|
34
|
tomwalters@0
|
35 #include "stitch.h"
|
tomwalters@0
|
36 #include "source.h"
|
tomwalters@0
|
37 #include "funcs.h"
|
tomwalters@0
|
38 #include "ops.h"
|
tomwalters@0
|
39
|
tomwalters@0
|
40 #if !defined(PC) && !defined(DSP32)
|
tomwalters@0
|
41 DoubleSource LogarithmDoubleSource( source )
|
tomwalters@0
|
42 DoubleSource source ;
|
tomwalters@0
|
43 {
|
tomwalters@0
|
44 return ( CallingDoubleSource( source, log ) ) ;
|
tomwalters@0
|
45 }
|
tomwalters@0
|
46
|
tomwalters@0
|
47 DoubleSource ExponentialDoubleSource( source )
|
tomwalters@0
|
48 DoubleSource source ;
|
tomwalters@0
|
49 {
|
tomwalters@0
|
50 return ( CallingDoubleSource( source, exp) ) ;
|
tomwalters@0
|
51 }
|
tomwalters@0
|
52
|
tomwalters@0
|
53 DoubleSource SineDoubleSource( source )
|
tomwalters@0
|
54 DoubleSource source ;
|
tomwalters@0
|
55 {
|
tomwalters@0
|
56 return ( CallingDoubleSource( source, sin ) ) ;
|
tomwalters@0
|
57 }
|
tomwalters@0
|
58
|
tomwalters@0
|
59 DoubleSource CosineDoubleSource( source )
|
tomwalters@0
|
60 DoubleSource source ;
|
tomwalters@0
|
61 {
|
tomwalters@0
|
62 return ( CallingDoubleSource( source, cos ) ) ;
|
tomwalters@0
|
63 }
|
tomwalters@0
|
64
|
tomwalters@0
|
65 DoubleSource SquareRootDoubleSource( source )
|
tomwalters@0
|
66 DoubleSource source ;
|
tomwalters@0
|
67 {
|
tomwalters@0
|
68 return ( CallingDoubleSource( source, sqrt ) ) ;
|
tomwalters@0
|
69 }
|
tomwalters@0
|
70
|
tomwalters@0
|
71 DoubleSource DoubleAbsDoubleSource( source )
|
tomwalters@0
|
72 DoubleSource source ;
|
tomwalters@0
|
73 {
|
tomwalters@0
|
74 return ( CallingDoubleSource( source, fabs ) ) ;
|
tomwalters@0
|
75 }
|
tomwalters@0
|
76
|
tomwalters@0
|
77 DoubleSource OscilatorDoubleSource( input, samplerate, phase0 )
|
tomwalters@0
|
78 DoubleSource input ;
|
tomwalters@0
|
79 double samplerate, phase0 ;
|
tomwalters@0
|
80 {
|
tomwalters@0
|
81 double TwoPi = atan( 1. ) * 8. ;
|
tomwalters@0
|
82
|
tomwalters@0
|
83 return (
|
tomwalters@0
|
84 CosineDoubleSource(
|
tomwalters@0
|
85 IntegrateDoubleSource(
|
tomwalters@0
|
86 MultiplyDoubleSources(
|
tomwalters@0
|
87 input,
|
tomwalters@0
|
88 ConstantDoubleSource( TwoPi / samplerate )
|
tomwalters@0
|
89 ),
|
tomwalters@0
|
90 phase0 / 360. * TwoPi
|
tomwalters@0
|
91 )
|
tomwalters@0
|
92 )
|
tomwalters@0
|
93 ) ;
|
tomwalters@0
|
94 }
|
tomwalters@0
|
95
|
tomwalters@0
|
96 DoubleSource CosinewaveDoubleSource( frequency, amplitude, phase0 )
|
tomwalters@0
|
97 double frequency, amplitude, phase0 ;
|
tomwalters@0
|
98 {
|
tomwalters@0
|
99 return ( MultiplyDoubleSources( OscilatorDoubleSource( ConstantDoubleSource( frequency ), 1., phase0 ), ConstantDoubleSource( amplitude ) ) ) ;
|
tomwalters@0
|
100 }
|
tomwalters@0
|
101
|
tomwalters@0
|
102 DoubleSource SinewaveDoubleSource( frequency, amplitude, phase0 )
|
tomwalters@0
|
103 double frequency, amplitude, phase0 ;
|
tomwalters@0
|
104 {
|
tomwalters@0
|
105 return ( CosinewaveDoubleSource( frequency, amplitude, phase0 - 90. ) ) ;
|
tomwalters@0
|
106 }
|
tomwalters@0
|
107
|
tomwalters@0
|
108 DoubleSource RaisedCosinewaveDoubleSource( frequency, amplitude, phase0 )
|
tomwalters@0
|
109 double frequency, amplitude, phase0 ;
|
tomwalters@0
|
110 {
|
tomwalters@0
|
111 return ( AddDoubleSources( CosinewaveDoubleSource( frequency, amplitude / 2., phase0 ), ConstantDoubleSource( amplitude / 2. ) ) ) ;
|
tomwalters@0
|
112 }
|
tomwalters@0
|
113
|
tomwalters@0
|
114 DoubleSource RaisedSinewaveDoubleSource( frequency, amplitude, phase0 )
|
tomwalters@0
|
115 double frequency, amplitude, phase0 ;
|
tomwalters@0
|
116 {
|
tomwalters@0
|
117 return ( RaisedCosinewaveDoubleSource( frequency, amplitude, phase0 - 90. ) ) ;
|
tomwalters@0
|
118 }
|
tomwalters@0
|
119 #endif
|
tomwalters@0
|
120
|
tomwalters@0
|
121 #define LOG_ZERO (-10)
|
tomwalters@0
|
122
|
tomwalters@0
|
123 static short imB( number )
|
tomwalters@0
|
124 short number ;
|
tomwalters@0
|
125 {
|
tomwalters@0
|
126 register int i, out ;
|
tomwalters@0
|
127 register long in ;
|
tomwalters@0
|
128 static int precision = 12 ;
|
tomwalters@0
|
129 static int *table = { ( int * ) 0 } ;
|
tomwalters@0
|
130 static unsigned table_size ;
|
tomwalters@0
|
131 static int inc ;
|
tomwalters@0
|
132 double k ;
|
tomwalters@0
|
133
|
tomwalters@0
|
134 if( table == ( int * ) 0 ) {
|
tomwalters@0
|
135
|
tomwalters@0
|
136 table_size = 1 << precision ;
|
tomwalters@0
|
137
|
tomwalters@0
|
138 k = 2000. / log( 10. ) ;
|
tomwalters@0
|
139 inc = k * log( 2. ) + 0.5 ;
|
tomwalters@0
|
140
|
tomwalters@0
|
141 table = (int *) stitch_malloc( table_size * sizeof( *table ), "imb.c for log table" ) ;
|
tomwalters@0
|
142
|
tomwalters@0
|
143 for( i=0 ; i < table_size ; i++ )
|
tomwalters@0
|
144 table[ i ] = log( (double) ( table_size + i ) / table_size ) * k + 0.5 ;
|
tomwalters@0
|
145 }
|
tomwalters@0
|
146
|
tomwalters@0
|
147 if( number > 0 ) {
|
tomwalters@0
|
148
|
tomwalters@0
|
149 in = number ;
|
tomwalters@0
|
150
|
tomwalters@0
|
151 out = precision * inc ;
|
tomwalters@0
|
152
|
tomwalters@0
|
153 if( number < table_size )
|
tomwalters@0
|
154 do {
|
tomwalters@0
|
155 in <<= 1 ;
|
tomwalters@0
|
156 out -= inc ;
|
tomwalters@0
|
157 }
|
tomwalters@0
|
158 while( in < table_size ) ;
|
tomwalters@0
|
159 else
|
tomwalters@0
|
160 while( in - table_size >= table_size ) {
|
tomwalters@0
|
161 in >>= 1 ;
|
tomwalters@0
|
162 out += inc ;
|
tomwalters@0
|
163 }
|
tomwalters@0
|
164
|
tomwalters@0
|
165 return ( out + table[ in - table_size ] ) ;
|
tomwalters@0
|
166 }
|
tomwalters@0
|
167 else
|
tomwalters@0
|
168 return ( LOG_ZERO ) ;
|
tomwalters@0
|
169 }
|
tomwalters@0
|
170
|
tomwalters@0
|
171 ShortSource milliBellShortSource( source )
|
tomwalters@0
|
172 ShortSource source ;
|
tomwalters@0
|
173 {
|
tomwalters@0
|
174 return ( CallingShortSource( source, imB ) ) ;
|
tomwalters@0
|
175 }
|
tomwalters@0
|
176
|