Jamoma API  0.6.0.a19
TTTanhFunction.test.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspFunctionLib
4  *
5  * @brief Unit tests for the Jamoma DSP #TTTanhFunction of the #TTFunctionLib
6  *
7  * @details
8  *
9  * @authors Trond Lossius
10  *
11  * @copyright Copyright © 2012 by Trond Lossius @n
12  * This code is licensed under the terms of the "New BSD License" @n
13  * http://creativecommons.org/licenses/BSD/
14  */
15 
16 
17 #include "TTFunction.h"
18 #include "TTTanhFunction.h"
19 
20 
21 /*
22 
23  // We test using the default values for offset and width:
24 
25  offset = 0
26  width = 0.5
27 
28  a = log(7)/width = 2*log(7)
29  b = 0.5 * (mOffset+1) = 0.5
30 
31  f0 = tanh(a*(-b)) = tanh(-0.5*a)
32  f1 = tanh(a*(1-b)) = tanh(0.5*a)
33 
34  alpha = 1/(f1-f0);
35  beta = f0;
36 
37  y = alpha * (tanh(a * (x-b)) - beta)
38 
39 
40  * coefficients calculated in Octave using:
41 
42  x = linspace(0,1,128);
43  a = 2*log(7);
44  b = 0.5;
45  f0 = tanh(-0.5*a);
46  f1 = tanh(0.5*a);
47  alpha = 1/(f1-f0);
48  beta = f0;
49  y = alpha * (tanh(a * (x-b)) - beta);
50  printf("%.16e,\n", y)
51 
52  */
53 
55 {
56  int errorCount = 0;
57  int testAssertionCount = 0;
58  int badSampleCount = 0;
59  TTAudioSignalPtr input = NULL;
60  TTAudioSignalPtr output = NULL;
61  int N = 128;
62  TTValue v;
63 
64  TTFloat64 inputSignal1[128] = {
65  0.0000000000000000e+00,
66  7.8740157480314960e-03,
67  1.5748031496062992e-02,
68  2.3622047244094488e-02,
69  3.1496062992125984e-02,
70  3.9370078740157480e-02,
71  4.7244094488188976e-02,
72  5.5118110236220472e-02,
73  6.2992125984251968e-02,
74  7.0866141732283464e-02,
75  7.8740157480314960e-02,
76  8.6614173228346455e-02,
77  9.4488188976377951e-02,
78  1.0236220472440945e-01,
79  1.1023622047244094e-01,
80  1.1811023622047244e-01,
81  1.2598425196850394e-01,
82  1.3385826771653542e-01,
83  1.4173228346456693e-01,
84  1.4960629921259844e-01,
85  1.5748031496062992e-01,
86  1.6535433070866140e-01,
87  1.7322834645669291e-01,
88  1.8110236220472442e-01,
89  1.8897637795275590e-01,
90  1.9685039370078738e-01,
91  2.0472440944881889e-01,
92  2.1259842519685040e-01,
93  2.2047244094488189e-01,
94  2.2834645669291337e-01,
95  2.3622047244094488e-01,
96  2.4409448818897639e-01,
97  2.5196850393700787e-01,
98  2.5984251968503935e-01,
99  2.6771653543307083e-01,
100  2.7559055118110237e-01,
101  2.8346456692913385e-01,
102  2.9133858267716534e-01,
103  2.9921259842519687e-01,
104  3.0708661417322836e-01,
105  3.1496062992125984e-01,
106  3.2283464566929132e-01,
107  3.3070866141732280e-01,
108  3.3858267716535434e-01,
109  3.4645669291338582e-01,
110  3.5433070866141730e-01,
111  3.6220472440944884e-01,
112  3.7007874015748032e-01,
113  3.7795275590551181e-01,
114  3.8582677165354329e-01,
115  3.9370078740157477e-01,
116  4.0157480314960631e-01,
117  4.0944881889763779e-01,
118  4.1732283464566927e-01,
119  4.2519685039370081e-01,
120  4.3307086614173229e-01,
121  4.4094488188976377e-01,
122  4.4881889763779526e-01,
123  4.5669291338582674e-01,
124  4.6456692913385828e-01,
125  4.7244094488188976e-01,
126  4.8031496062992124e-01,
127  4.8818897637795278e-01,
128  4.9606299212598426e-01,
129  5.0393700787401574e-01,
130  5.1181102362204722e-01,
131  5.1968503937007871e-01,
132  5.2755905511811019e-01,
133  5.3543307086614167e-01,
134  5.4330708661417326e-01,
135  5.5118110236220474e-01,
136  5.5905511811023623e-01,
137  5.6692913385826771e-01,
138  5.7480314960629919e-01,
139  5.8267716535433067e-01,
140  5.9055118110236215e-01,
141  5.9842519685039375e-01,
142  6.0629921259842523e-01,
143  6.1417322834645671e-01,
144  6.2204724409448819e-01,
145  6.2992125984251968e-01,
146  6.3779527559055116e-01,
147  6.4566929133858264e-01,
148  6.5354330708661412e-01,
149  6.6141732283464560e-01,
150  6.6929133858267720e-01,
151  6.7716535433070868e-01,
152  6.8503937007874016e-01,
153  6.9291338582677164e-01,
154  7.0078740157480313e-01,
155  7.0866141732283461e-01,
156  7.1653543307086609e-01,
157  7.2440944881889768e-01,
158  7.3228346456692917e-01,
159  7.4015748031496065e-01,
160  7.4803149606299213e-01,
161  7.5590551181102361e-01,
162  7.6377952755905509e-01,
163  7.7165354330708658e-01,
164  7.7952755905511806e-01,
165  7.8740157480314954e-01,
166  7.9527559055118113e-01,
167  8.0314960629921262e-01,
168  8.1102362204724410e-01,
169  8.1889763779527558e-01,
170  8.2677165354330706e-01,
171  8.3464566929133854e-01,
172  8.4251968503937003e-01,
173  8.5039370078740162e-01,
174  8.5826771653543310e-01,
175  8.6614173228346458e-01,
176  8.7401574803149606e-01,
177  8.8188976377952755e-01,
178  8.8976377952755903e-01,
179  8.9763779527559051e-01,
180  9.0551181102362199e-01,
181  9.1338582677165348e-01,
182  9.2125984251968507e-01,
183  9.2913385826771655e-01,
184  9.3700787401574803e-01,
185  9.4488188976377951e-01,
186  9.5275590551181100e-01,
187  9.6062992125984248e-01,
188  9.6850393700787396e-01,
189  9.7637795275590555e-01,
190  9.8425196850393704e-01,
191  9.9212598425196852e-01,
192  1.0000000000000000e+00
193  };
194 
195 
196  TTFloat64 expectedSignal1[128] = {
197  0.0000000000000000e+00,
198  1.2888187396638967e-03,
199  2.6555334980766371e-03,
200  4.1046191017844510e-03,
201  5.6407778689472497e-03,
202  7.2689473805817705e-03,
203  8.9943079966691569e-03,
204  1.0822290024937141e-02,
205  1.2758580438150710e-02,
206  1.4809129022838886e-02,
207  1.6980153828563840e-02,
208  1.9278145772136265e-02,
209  2.1709872235661093e-02,
210  2.4282379481054574e-02,
211  2.7002993686838356e-02,
212  2.9879320395769886e-02,
213  3.2919242144436624e-02,
214  3.6130914028613001e-02,
215  3.9522756941310036e-02,
216  4.3103448204465565e-02,
217  4.6881909300631561e-02,
218  5.0867290398419938e-02,
219  5.5068951355544245e-02,
220  5.9496438876826693e-02,
221  6.4159459502405897e-02,
222  6.9067848104532648e-02,
223  7.4231531580836022e-02,
224  7.9660487448883086e-02,
225  8.5364697072407431e-02,
226  9.1354093284923915e-02,
227  9.7638502222736095e-02,
228  1.0422757923768325e-01,
229  1.1113073883134192e-01,
230  1.1835707863758407e-01,
231  1.2591529757994183e-01,
232  1.3381360844431853e-01,
233  1.4205964523598882e-01,
234  1.5066036583177095e-01,
235  1.5962195059236411e-01,
236  1.6894969776404256e-01,
237  1.7864791667035329e-01,
238  1.8871981986951353e-01,
239  1.9916741562738066e-01,
240  2.0999140222386417e-01,
241  2.2119106576644540e-01,
242  2.3276418332140286e-01,
243  2.4470693328432372e-01,
244  2.5701381498921211e-01,
245  2.6967757959279937e-01,
246  2.8268917426076395e-01,
247  2.9603770161960280e-01,
248  3.0971039631719888e-01,
249  3.2369262035375795e-01,
250  3.3796787860181460e-01,
251  3.5251785563091254e-01,
252  3.6732247459344242e-01,
253  3.8235997851978554e-01,
254  3.9760703392289520e-01,
255  4.1303885613678470e-01,
256  4.2862935532426499e-01,
257  4.4435130160248815e-01,
258  4.6017650726718884e-01,
259  4.7607602366490365e-01,
260  4.9202034988319643e-01,
261  5.0797965011680357e-01,
262  5.2392397633509635e-01,
263  5.3982349273281116e-01,
264  5.5564869839751174e-01,
265  5.7137064467573495e-01,
266  5.8696114386321530e-01,
267  6.0239296607710491e-01,
268  6.1764002148021446e-01,
269  6.3267752540655764e-01,
270  6.4748214436908746e-01,
271  6.6203212139818535e-01,
272  6.7630737964624199e-01,
273  6.9028960368280123e-01,
274  7.0396229838039714e-01,
275  7.1731082573923599e-01,
276  7.3032242040720063e-01,
277  7.4298618501078784e-01,
278  7.5529306671567631e-01,
279  7.6723581667859719e-01,
280  7.7880893423355446e-01,
281  7.9000859777613575e-01,
282  8.0083258437261939e-01,
283  8.1128018013048653e-01,
284  8.2135208332964671e-01,
285  8.3105030223595744e-01,
286  8.4037804940763583e-01,
287  8.4933963416822900e-01,
288  8.5794035476401109e-01,
289  8.6618639155568145e-01,
290  8.7408470242005831e-01,
291  8.8164292136241584e-01,
292  8.8886926116865805e-01,
293  8.9577242076231667e-01,
294  9.0236149777726393e-01,
295  9.0864590671507606e-01,
296  9.1463530292759254e-01,
297  9.2033951255111701e-01,
298  9.2576846841916405e-01,
299  9.3093215189546741e-01,
300  9.3584054049759413e-01,
301  9.4050356112317335e-01,
302  9.4493104864445576e-01,
303  9.4913270960158014e-01,
304  9.5311809069936848e-01,
305  9.5689655179553446e-01,
306  9.6047724305868998e-01,
307  9.6386908597138710e-01,
308  9.6708075785556336e-01,
309  9.7012067960423010e-01,
310  9.7299700631316166e-01,
311  9.7571762051894551e-01,
312  9.7829012776433899e-01,
313  9.8072185422786373e-01,
314  9.8301984617143623e-01,
315  9.8519087097716118e-01,
316  9.8724141956184930e-01,
317  9.8917770997506294e-01,
318  9.9100569200333088e-01,
319  9.9273105261941830e-01,
320  9.9435922213105277e-01,
321  9.9589538089821561e-01,
322  9.9734446650192343e-01,
323  9.9871118126033609e-01,
324  1.0000000000000000e+00,
325  };
326 
327  // setup Function
328  this->setAttributeValue(TT("function"), TT("tanh"));
329 
330 
331  // create 1 channel audio signal objects
332  TTObjectBaseInstantiate(kTTSym_audiosignal, &input, 1);
333  TTObjectBaseInstantiate(kTTSym_audiosignal, &output, 1);
334  input->allocWithVectorSize(N);
335  output->allocWithVectorSize(N);
336 
337  // create a signal to be transformed and then process it)
338  input->clear();
339  for (int i=0; i<N; i++)
340  input->mSampleVectors[0][i] = inputSignal1[i];
341 
342  this->process(input, output);
343 
344  // now test the output
345  for (int n=0; n<N; n++)
346  {
347  TTBoolean result = !TTTestFloatEquivalence(output->mSampleVectors[0][n], expectedSignal1[n]);
348  badSampleCount += result;
349  if (result)
350  TTTestLog("BAD SAMPLE @ n=%i ( value=%.10f expected=%.10f )", n, output->mSampleVectors[0][n], expectedSignal1[n]);
351  }
352 
353  TTTestAssertion("Produces correct function values",
354  badSampleCount == 0,
355  testAssertionCount,
356  errorCount);
357  if (badSampleCount)
358  TTTestLog("badSampleCount is %i", badSampleCount);
359 
360 
361  TTObjectBaseRelease(&input);
362  TTObjectBaseRelease(&output);
363 
364  // wrap up test results and pass back to whoever called test
365  return TTTestFinish(testAssertionCount, errorCount, returnedTestInfo);
366 
367 }
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
virtual TTErr test(TTValue &returnedTestInfo)
Unit Tests.
TTErr TTObjectBaseRelease(TTObjectBasePtr *anObject)
DEPRECATED.
TTErr setAttributeValue(const TTSymbol name, TTValue &value)
Set an attribute value for an object.
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
TTErr clear()
Zero out all of the sample values in the audio signal.
TTTanhFunction Unit for Jamoms DSP
TTErr TTObjectBaseInstantiate(const TTSymbol className, TTObjectBasePtr *returnedObjectPtr, const TTValue arguments)
DEPRECATED.
The TTAudioSignal class represents N vectors of audio samples for M channels.
Definition: TTAudioSignal.h:57
TTSampleValue ** mSampleVectors
An array of pointers to the first sample in each vector. Declared Public for fast access...
Definition: TTAudioSignal.h:74
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
TTFunction is a generalized function wrapper for Jamoma DSP
TTErr allocWithVectorSize(const TTUInt16 newVectorSize)
Allocate memory for all channels at the specified vectorsize, if the vectorsize is different from the...
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34