File indexing completed on 2024-05-19 04:34:06

0001 /***************************************************************************
0002  *                                                                         *
0003  *   copyright : (C) 2007 The University of Toronto                        *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  ***************************************************************************/
0011 
0012 #include "testcsd.h"
0013 
0014 #include <QtTest>
0015 
0016 #include <math_kst.h>
0017 #include <object.h>
0018 #include <qdir.h>
0019 #include <qfile.h>
0020 #include <datacollection.h>
0021 #include <objectstore.h>
0022 
0023 
0024 #include <csd.h>
0025 
0026 
0027 static Kst::ObjectStore _store;
0028 
0029 void TestCSD::cleanupTestCase() {
0030     _store.clear();
0031 }
0032 
0033 QDomDocument TestCSD::makeDOMElement(const QString& tag, const QString& val) {
0034 // Should be some boundary checking in the constructor.
0035   QDomDocument csdDOM("csddocument");
0036   QDomElement csdElement, child;
0037   QDomText text;
0038 
0039   csdElement = csdDOM.createElement("csdDOMTest");
0040 
0041   child = csdDOM.createElement("tag");
0042   text = csdDOM.createTextNode(tag);
0043   child.appendChild(text);
0044   csdElement.appendChild(child);
0045 
0046   child = csdDOM.createElement("vector");
0047   text = csdDOM.createTextNode(val);
0048   child.appendChild(text);
0049   csdElement.appendChild(child);
0050 
0051   child = csdDOM.createElement("samplerate");
0052   text = csdDOM.createTextNode("1");
0053   child.appendChild(text);
0054   csdElement.appendChild(child);
0055 
0056   child = csdDOM.createElement("average");
0057   text = csdDOM.createTextNode("1");
0058   child.appendChild(text);
0059   csdElement.appendChild(child);
0060 
0061   child = csdDOM.createElement("fftlength");
0062   text = csdDOM.createTextNode("5");
0063   child.appendChild(text);
0064   csdElement.appendChild(child);
0065 
0066   child = csdDOM.createElement("apodize");
0067   text = csdDOM.createTextNode("1");
0068   child.appendChild(text);
0069   csdElement.appendChild(child);
0070 
0071   child = csdDOM.createElement("apodizefunction");
0072   text = csdDOM.createTextNode("WindowOriginal");
0073   child.appendChild(text);
0074   csdElement.appendChild(child);
0075 
0076   child = csdDOM.createElement("gaussiansigma");
0077   text = csdDOM.createTextNode("0.01");
0078   child.appendChild(text);
0079   csdElement.appendChild(child);
0080 
0081   child = csdDOM.createElement("removemean");
0082   text = csdDOM.createTextNode("1");
0083   child.appendChild(text);
0084   csdElement.appendChild(child);
0085 
0086   child = csdDOM.createElement("windowsize");
0087   text = csdDOM.createTextNode("5000");
0088   child.appendChild(text);
0089   csdElement.appendChild(child);
0090 
0091   child = csdDOM.createElement("vectorunits");
0092   text = csdDOM.createTextNode("1");
0093   child.appendChild(text);
0094   csdElement.appendChild(child);
0095 
0096   child = csdDOM.createElement("rateunits");
0097   text = csdDOM.createTextNode("1");
0098   child.appendChild(text);
0099   csdElement.appendChild(child);
0100 
0101   child = csdDOM.createElement("outputtype");
0102   text = csdDOM.createTextNode("1");
0103   child.appendChild(text);
0104   csdElement.appendChild(child);
0105 
0106   csdDOM.appendChild(csdElement);
0107 
0108   return csdDOM;
0109 }
0110 
0111 void TestCSD::testCSD() {
0112 
0113   Kst::VectorPtr vp = Kst::kst_cast<Kst::Vector>(_store.createObject<Kst::Vector>());
0114   Q_ASSERT(vp);
0115   vp->resize(10);
0116   vp->setDescriptiveName("tempVector");
0117   for (int i = 0; i < 10; i++){
0118     vp->value()[i] = i;
0119   }
0120 
0121   Kst::CSDPtr csd = Kst::kst_cast<Kst::CSD>(_store.createObject<Kst::CSD>());
0122   csd->change(vp, 1.0, false, false, false, WindowUndefined, 0, 0, 0.0, PSDUndefined, QString::null, QString::null);
0123 
0124   QCOMPARE(csd->vector()->descriptiveName(), QLatin1String("tempVector"));
0125   QCOMPARE(csd->output(), PSDUndefined);
0126   QVERIFY(!csd->apodize());
0127   QVERIFY(!csd->removeMean());
0128   QVERIFY(!csd->average());
0129   QCOMPARE(csd->frequency(), 1.0);
0130   QCOMPARE(csd->apodizeFxn(), WindowUndefined);
0131   QCOMPARE(csd->length(), 0);
0132   QCOMPARE(csd->windowSize(), 0);
0133   QCOMPARE(csd->gaussianSigma(), 0.0);
0134   QVERIFY(csd->vectorUnits().isEmpty());
0135   QVERIFY(csd->rateUnits().isEmpty());
0136 
0137   csd->setOutput(PSDAmplitudeSpectralDensity);
0138   csd->setApodize(true);
0139   csd->setRemoveMean(true);
0140   csd->setAverage(true);
0141   csd->setFrequency(0.1);
0142   csd->setApodizeFxn(WindowOriginal);
0143   csd->setLength(3);
0144   csd->setWindowSize(50);
0145   csd->setGaussianSigma(0.2);
0146 
0147   QCOMPARE(csd->vector()->descriptiveName(), QLatin1String("tempVector"));
0148   QCOMPARE(csd->output(), PSDAmplitudeSpectralDensity);
0149   QVERIFY(csd->apodize());
0150   QVERIFY(csd->removeMean());
0151   QVERIFY(csd->average());
0152   QCOMPARE(csd->frequency(), 0.1);
0153   QCOMPARE(csd->windowSize(), 50);
0154   QCOMPARE(csd->apodizeFxn(), WindowOriginal);
0155   QCOMPARE(csd->gaussianSigma(), 0.2);
0156 
0157 //   KTempFile tf(locateLocal("tmp", "kst-csd"), "txt");
0158 //   QFile *qf = tf.file();
0159 //   QTextStream ts(qf);
0160 //   csd->save(ts, "");
0161 
0162 // the constructor used here is no longer used in kst: use the factory instead
0163 //   QDomNode n = makeDOMElement("csdDOMCsd", "csdDOMVector").firstChild();
0164 //   QDomElement e = n.toElement();
0165 //   Kst::CSDPtr csdDOM = new Kst::CSD(&_store, e);
0166 // 
0167 //   QCOMPARE(csdDOM->output(), PSDPowerSpectralDensity);
0168 //   QVERIFY(csdDOM->apodize());
0169 //   QVERIFY(csdDOM->removeMean());
0170 //   QVERIFY(csdDOM->average());
0171 //   QCOMPARE(csdDOM->frequency(), 1.0);
0172 //   QCOMPARE(csdDOM->apodizeFxn(), WindowOriginal);
0173 //   QCOMPARE(csdDOM->gaussianSigma(), 0.01);
0174 //   QCOMPARE(csdDOM->windowSize(), 5000);
0175 
0176 //   Kst::VectorPtr vp2 = Kst::kst_cast<Kst::Vector>(_store.createObject<Kst::Vector>());
0177 //   Q_ASSERT(vp2);
0178 //   vp2->resize(10);
0179 //   for (int i = 0; i < 10; i++){
0180 //     vp2->value()[i] = i;
0181 //   }
0182 //   csdDOM->setVector(vp2);
0183 //   QCOMPARE(csdDOM->vector()->descriptiveName(), QLatin1String("tempVector2"));
0184 //   csdDOM->setWindowSize(9);
0185 //   Kst::MatrixPtr outMatrix = csdDOM->outputMatrix();
0186 // 
0187 //   QVERIFY(outMatrix->resize(3, 3, false)); // very odd thing to do?
0188 //   QVERIFY(outMatrix->setValue(0, 0, 1.716299));
0189 //   QVERIFY(outMatrix->setValue(0, 1, -0.485527));
0190 //   QVERIFY(outMatrix->setValue(0, 2, -0.288690));
0191 //   QVERIFY(outMatrix->setValue(1, 0, 1.716299));
0192 //   QVERIFY(outMatrix->setValue(1, 1, NAN));
0193 //   QVERIFY(outMatrix->setValue(1, 2, -0.274957));
0194 //   QVERIFY(outMatrix->setValue(2, 0, 1.711721));
0195 //   QVERIFY(outMatrix->setValue(2, 1, -0.485527));
0196 //   QVERIFY(outMatrix->setValue(2, 2, -0.293267));
0197 // 
0198 //   QCOMPARE(outMatrix->sampleCount(), 9);
0199 //   QCOMPARE(outMatrix->value(0, 0), 1.716299);
0200 //   QCOMPARE(outMatrix->value(0, 1),  -0.485527);
0201 //   QCOMPARE(outMatrix->value(0, 2), -0.288690);
0202 //   QCOMPARE(outMatrix->value(1, 0), 1.716299);
0203 //   QCOMPARE(outMatrix->value(1, 1), 0.0);
0204 //   QCOMPARE(outMatrix->value(1, 2), -0.274957);
0205 //   QCOMPARE(outMatrix->value(2, 0), 1.711721);
0206 //   QCOMPARE(outMatrix->value(2, 1), -0.485527);
0207 //   QCOMPARE(outMatrix->value(2, 2), -0.293267);
0208 // 
0209 //   csdDOM->writeLock();
0210 //   QCOMPARE(csdDOM->update(), Kst::Object::UPDATE);
0211 //   csdDOM->unlock();
0212 // 
0213 //   outMatrix = csdDOM->outputMatrix();
0214 //   QEXPECT_FAIL("", "This has always failed", Continue);
0215 //   QCOMPARE(outMatrix->sampleCount(), 128);
0216 // 
0217 //   csdDOM->setWindowSize(11);
0218 //   QEXPECT_FAIL("", "This has always failed", Continue);
0219 //   QCOMPARE(outMatrix->sampleCount(), 128);
0220 // 
0221 //   QEXPECT_FAIL("", "This has always failed", Continue);
0222 //   QCOMPARE(outMatrix->value(0, 0), 1.716299);
0223 //   QEXPECT_FAIL("", "This has always failed", Continue);
0224 //   QCOMPARE(outMatrix->value(0, 1),  -0.485527);
0225 //   QEXPECT_FAIL("", "This has always failed", Continue);
0226 //   QCOMPARE(outMatrix->value(0, 2), -0.288690);
0227 //   QEXPECT_FAIL("", "This has always failed", Continue);
0228 //   QCOMPARE(outMatrix->value(1, 0), 1.716299);
0229 //   QCOMPARE(outMatrix->value(1, 1), 0.0);
0230 //   QEXPECT_FAIL("", "This has always failed", Continue);
0231 //   QCOMPARE(outMatrix->value(1, 2), -0.274957);
0232 //   QEXPECT_FAIL("", "This has always failed", Continue);
0233 //   QCOMPARE(outMatrix->value(2, 0), 1.711721);
0234 //   QEXPECT_FAIL("", "This has always failed", Continue);
0235 //   QCOMPARE(outMatrix->value(2, 1), -0.485527);
0236 //   QEXPECT_FAIL("", "This has always failed", Continue);
0237 //   QCOMPARE(outMatrix->value(2, 2), -0.293267);
0238 
0239 }
0240 
0241 #ifdef KST_USE_QTEST_MAIN
0242 QTEST_MAIN(TestCSD)
0243 #endif