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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
/*
* Copyright (c) 2001-2002 MUSIC TECHNOLOGY GROUP (MTG)
* UNIVERSITAT POMPEU FABRA
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/* WARNING WARNING WARNING: This test will only pass if CLAM_DOUBLE is defined */
#include <fstream>
#include <cstdlib> //rand()
#include "SpectrumProduct.hxx"
#include "TestUtils.hxx"
#include "OctaveVector.hxx"
//TODO: To be unquoted when we use gcc 3.0
//#include <limits>
namespace CLAMTest {
static const unsigned int ArraySize=40;
// Loads the vectors from a file, if they have not been previously loaded,
// and sets the arguments to the loaded values.
bool SetTestVectors(Spectrum &s1, Spectrum &s2, Spectrum &s3)
{
bool remove1=false,remove2=false,remove3=false;
static bool loaded=false;
static Spectrum orig1,orig2,orig3;
if (!loaded) {
std::ifstream file("SpectrumProductTest.data");
SpecTypeFlags f;
f.bMagPhase=false;
f.bComplex=true;
orig1.SetSize(ArraySize);
orig2.SetSize(ArraySize);
orig3.SetSize(ArraySize);
orig1.SetType(f);
orig2.SetType(f);
orig3.SetType(f);
if (file) {
std::cerr << "Loading Input1..." << std::endl;
if (!CLAMTest::LoadOctaveComplexVector(file,"Input1",orig1.GetComplexArray())) {
std::cerr << "Could not find Input1 vector in input!" << std::endl;
return false;
}
std::cerr << "Loading Input2..." << std::endl;
if (!CLAMTest::LoadOctaveComplexVector(file,"Input2",orig2.GetComplexArray())) {
std::cerr << "Could not find Audio vector in input!" << std::endl;
return false;
}
std::cerr << "Loading Output..." << std::endl;
if (!CLAMTest::LoadOctaveComplexVector(file,"Output",orig3.GetComplexArray())) {
std::cerr << "Could not find Audio vector in input!" << std::endl;
return false;
}
file.close();
}
else {
std::cerr << "Generating Inputs and output..." << std::endl;
for (int i = 0; i < ArraySize; i++) {
orig1.GetComplexArray().GetPtr()[i] =
Complex(10.0*(TData)rand()/(TData)RAND_MAX,
10.0*(TData)rand()/(TData)RAND_MAX);
orig2.GetComplexArray().GetPtr()[i] =
Complex(10.0*(TData)rand()/(TData)RAND_MAX,
10.0*(TData)rand()/(TData)RAND_MAX);
orig3.GetComplexArray().GetPtr()[i] =
orig1.GetComplexArray().GetPtr()[i] *
orig2.GetComplexArray().GetPtr()[i];
}
}
loaded=true;
}
SpecTypeFlags spec;
s1.GetType(spec);
if (!spec.bComplex) {
spec.bComplex=1;
s1.SetType(spec);
remove1=true;
}
s2.GetType(spec);
if (!spec.bComplex) {
spec.bComplex=1;
s2.SetType(spec);
remove2=true;
}
s3.GetType(spec);
if (!spec.bComplex) {
spec.bComplex=1;
s3.SetType(spec);
remove3=true;
}
s1.SetComplexArray(orig1.GetComplexArray());
s2.SetComplexArray(orig2.GetComplexArray());
s3.SetComplexArray(orig3.GetComplexArray());
spec.bMagPhase=spec.bPolar=spec.bMagPhaseBPF=false;
s1.SynchronizeTo(spec);
s2.SynchronizeTo(spec);
s3.SynchronizeTo(spec);
if (remove1) {
s1.GetType(spec);
spec.bComplex=false;
s1.SetType(spec);
}
if (remove2) {
s2.GetType(spec);
spec.bComplex=false;
s2.SetType(spec);
}
if (remove3) {
s3.GetType(spec);
spec.bComplex=false;
s3.SetType(spec);
}
return true;
}
void SetPrototypes(int proto, Spectrum &s)
{
SpecTypeFlags f;
f.bComplex=proto&(1<<(int)SpecTypeFlags::eComplex);
f.bPolar=proto&(1<<(int)SpecTypeFlags::ePolar);
f.bMagPhase=proto&(1<<(int)SpecTypeFlags::eMagPhase);
f.bMagPhaseBPF=proto&(1<<(int)SpecTypeFlags::eMagPhaseBPF);
s.SetType(f);
}
std::string ProtoName(int proto)
{
std::string str;
for (int i=0; i<4; i++)
if (proto&(1<<i)) {
str+=SpecTypeFlags::sFlagValues[i].name;
str+=' ';
}
return str;
}
bool TestProduct(SpectrumProduct &product)
{
SpectrumConfig cfg;
int proto1,proto2,proto3;
static int count=0;
#ifdef CLAM_DOUBLE
TData max_err = 0.00005;
#else
TData max_err = 0.00039f;
#endif
//TODO: To be unquoted when we use gcc 3.0
//TData max_err = std::numeric_limits<TData>::epsilon()*TData(1e3);
product.Start();
cfg.AddBPFSize();
cfg.UpdateData();
cfg.SetSize(ArraySize);
cfg.SetBPFSize(ArraySize);
Spectrum in1(cfg),in2(cfg),out(cfg),out_good(cfg);
for (proto1=1; proto1<16; proto1++)
for (proto2=1; proto2<16; proto2++)
for (proto3=1; proto3<16; proto3++) {
std::cerr << "Test " << count++ << '('
<< proto1 << ',' << proto2 << ',' << proto3 << ')' <<std::endl;
std::cerr << " in1 attributes: " << ProtoName(proto1) << std::endl
<< " in2 attributes: " << ProtoName(proto2) << std::endl
<< " out attributes: " << ProtoName(proto3) << std::endl;
if (proto3 & 8) { // Skip BPF outputs for now...
std::cerr << "skipped " << std::endl;
continue;
}
SetPrototypes(proto1,in1);
SetPrototypes(proto2,in2);
SetPrototypes(proto3,out);
SetTestVectors(in1,in2,out_good);
product.SetPrototypes(in1,in2,out);
product.Do(in1,in2,out);
TData diff = CLAMTest::TestUtils::MaxDiff(out,out_good);
std::cerr << "Tolerance: " << max_err << std::endl;
std::cerr << "Difference: " << diff << std::endl;;
if (diff > max_err)
return false;
std::cerr.flush();
}
return true;
}
}
int main()
{
try {
CLAM::SpectrumProduct prod;
bool res = CLAMTest::TestProduct(prod);
if (res)
std::cerr << std::endl << "Passed." << std::endl;
else
std::cerr << std::endl << "Failed." << std::endl;
return !res;
}
catch (CLAM::Err e) {
e.Print();
}
}
|