Jamoma API  0.6.0.a19
TTTukeyWindow.test.cpp
Go to the documentation of this file.
1 /** @file
2  *
3  * @ingroup dspWindowFunctionLib
4  *
5  * @brief Unit tests for the Tukey Window Function Unit
6  *
7  * @details
8  *
9  * @authors Nathan Wolek, Tim Place, Trond Lossius
10  *
11  * @copyright Copyright © 2011 by Nathan Wolek @n
12  * This code is licensed under the terms of the "New BSD License" @n
13  * http://creativecommons.org/licenses/BSD/
14  */
15 
16 #include "TTWindowFunction.h"
17 #include "TTTukeyWindow.h"
18 
19 
20 /*
21  * coefficients calculated using Octave using these variables and commands:
22  * alpha = 0.5
23  * alphaOverTwo = alpha / 2
24  * twoOverAlpha = 2 / alpha
25  * x = 0.0:1/127:1.0
26  * n = 1:length(x)
27  * y =
28  * if x(n) < alphaOverTwo
29  * y(n) = 0.5 * (1 + cos ( pi * ((twoOverAlpha * x(n) ) - 1)));
30  * else
31  * if x(n) > (1 - alphaOverTwo)
32  * y(n) = 0.5 * (1 + cos ( pi * ((twoOverAlpha * x(n) ) - twoOverAlpha + 1)));
33  * else
34  * y(n) = 1;
35  * end
36  * end
37 */
38 static TTFloat64 sTukeyWindowCoefficients128[128] = {
39  0, 0.002445670414196688, 0.009758756441687333, 0.0218677164899363, 0.03865409245759671, 0.0599536685724058, 0.08555807786180814, 0.1152168405407152,
40  0.148639814375505, 0.1855000330531288, 0.2254369047884639, 0.2680597398789554, 0.3129515726976034, 0.3596732407349534, 0.4077676807861221, 0.4567644002546273,
41  0.5061840798316815, 0.5555432625244022, 0.6043590831616676, 0.6521539921103681, 0.6984604269914436, 0.7428253866937918, 0.7848148629399286, 0.8240180860508026,
42  0.8600515433748003, 0.8925627310699273, 0.9212336025366787, 0.9457836797666722, 0.9659727971697173, 0.9816034510373285, 0.9925227316586316, 0.9986238191873873,
43  1, 1, 1, 1, 1, 1, 1, 1,
44  1, 1, 1, 1, 1, 1, 1, 1,
45  1, 1, 1, 1, 1, 1, 1, 1,
46  1, 1, 1, 1, 1, 1, 1, 1,
47  1, 1, 1, 1, 1, 1, 1, 1,
48  1, 1, 1, 1, 1, 1, 1, 1,
49  1, 1, 1, 1, 1, 1, 1, 1,
50  1, 1, 1, 1, 1, 1, 1, 1,
51  0.9986238191873873, 0.9925227316586317, 0.9816034510373286, 0.9659727971697174, 0.9457836797666723, 0.9212336025366786, 0.8925627310699273, 0.8600515433748003,
52  0.8240180860508026, 0.7848148629399287, 0.7428253866937922, 0.698460426991444, 0.6521539921103678, 0.6043590831616674, 0.5555432625244022, 0.5061840798316815,
53  0.4567644002546273, 0.4077676807861222, 0.3596732407349537, 0.3129515726976038, 0.2680597398789558, 0.2254369047884637, 0.1855000330531288, 0.148639814375505,
54  0.1152168405407152, 0.08555807786180825, 0.05995366857240592, 0.03865409245759677, 0.02186771648993618, 0.009758756441687333, 0.002445670414196688, 0
55 };
56 
57 
58 TTErr TukeyWindow::test(TTValue& returnedTestInfo)
59 {
60  int errorCount = 0;
61  int testAssertionCount = 0;
62  int badSampleCount = 0;
63  TTAudioObjectBasePtr windowObject = NULL;
64  TTAudioSignalPtr input = NULL;
65  TTAudioSignalPtr output = NULL;
66  int N = 128;
67  TTValue v;
68  TTFloat64 testAlpha = 0.5;
69 
70  // setup windowObject
71  TTObjectBaseInstantiate(TT("WindowFunction"), &windowObject, TTValue(1));
72  windowObject->setAttributeValue(TT("function"), TT("tukey"));
73  windowObject->setAttributeValue(TT("mode"), TT("apply"));
74 
75  // set the value for alpha
76  windowObject->setAttributeValue(TT("alpha"), testAlpha);
77  TTTestLog("alpha was set to %.10f for test", testAlpha);
78 
79  // create 1 channel audio signal objects
80  TTObjectBaseInstantiate(kTTSym_audiosignal, &input, 1);
81  TTObjectBaseInstantiate(kTTSym_audiosignal, &output, 1);
82  input->allocWithVectorSize(N);
83  output->allocWithVectorSize(N);
84 
85  // create a signal to be transformed and then process it)
86  input->fill(1.0);
87  windowObject->process(input, output);
88 
89  // now test the output
90  for (int n=0; n<N; n++)
91  {
92  TTBoolean result = !TTTestFloatEquivalence(output->mSampleVectors[0][n], sTukeyWindowCoefficients128[n]);
93  badSampleCount += result;
94  if (result)
95  TTTestLog("BAD SAMPLE @ n=%i ( value=%.10f expected=%.10f )", n, output->mSampleVectors[0][n], sTukeyWindowCoefficients128[n]);
96  }
97 
98  TTTestAssertion("Produces correct window coefficients",
99  badSampleCount == 0,
100  testAssertionCount,
101  errorCount);
102  if (badSampleCount)
103  TTTestLog("badSampleCount is %i", badSampleCount);
104 
105 
106  TTObjectBaseRelease(&input);
107  TTObjectBaseRelease(&output);
108  TTObjectBaseRelease(&windowObject);
109 
110 
111  // wrap up test results and pass back to whoever called test
112  return TTTestFinish(testAssertionCount, errorCount, returnedTestInfo);
113 
114 }
bool TTBoolean
Boolean flag, same as Boolean on the Mac.
Definition: TTBase.h:167
TTErr TTObjectBaseRelease(TTObjectBasePtr *anObject)
DEPRECATED.
TTAudioObjectBase is the base class for all audio generating and processing objects in Jamoma DSP...
TTErr test(TTValue &returnedTestInfo)
Unit test for the window function unit.
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
Generalized Window Function Wrapper for Jamoma 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 process(TTAudioSignal &in, TTAudioSignal &out)
Process the input signal, resulting in an output signal.
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
Tukey Window Function Unit 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