📄 corr_02.cc
字号:
// file: $isip/class/algo/Correlation/corr_02.cc// version: $Id: corr_02.cc,v 1.20 2002/04/13 21:22:33 huang Exp $//// isip include files//#include "Correlation.h"// method: diagnose//// arguments:// Integral::DEBUG level: (input) debug level for diagnostics//// return: a boolean value indicating status//boolean Correlation::diagnose(Integral::DEBUG level_a) { //--------------------------------------------------------------------------- // // 0. preliminaries // //--------------------------------------------------------------------------- // output the class name // if (level_a > Integral::NONE) { SysString output(L"diagnosing class "); output.concat(CLASS_NAME); output.concat(L": "); Console::put(output); Console::increaseIndention(); } //-------------------------------------------------------------------------- // // 1. required public methods // class constructors // //-------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing required public methods: class constructors...\n"); Console::increaseIndention(); } Correlation cor0; cor0.setAlgorithm(AUTO); cor0.setImplementation(FACTORED); cor0.setNormalization(NONE); Correlation cor1(cor0); if (!cor1.eq(cor0)) { return Error::handle(name(), L"copy constructor/eq", Error::TEST, __FILE__, __LINE__); } // test large allocation construction and deletion // if (level_a == Integral::ALL) { Console::put(L"\ntesting large chunk memory allocation and deletion:\n"); // set the memory to a strange block size so we can hopefully catch any // frame overrun errors // Correlation::setGrowSize((long)500); Correlation* pcor = new Correlation(); for (long j = 1; j <= 100; j++) { Correlation** pcors = new Correlation*[j * 100]; // create the objects // for (long i = 0; i < j * 100; i++) { pcors[i] = new Correlation(); } // delete objects // for (long i = (j * 100) - 1; i >= 0; i--) { delete pcors[i]; } delete [] pcors; } delete pcor; } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // 2. required public methods // i/o methods // //--------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing required public methods: io methods...\n"); Console::increaseIndention(); } // declare a reference object // cor0.setAlgorithm(AUTO); cor0.setImplementation(FACTORED); cor0.setNormalization(NONE); // we need binary and text sof files // String tmp_filename0; Integral::makeTemp(tmp_filename0); String tmp_filename1; Integral::makeTemp(tmp_filename1); // open files in write mode // Sof tmp_file0; tmp_file0.open(tmp_filename0, File::WRITE_ONLY, File::TEXT); Sof tmp_file1; tmp_file1.open(tmp_filename1, File::WRITE_ONLY, File::BINARY); cor0.write(tmp_file0, (long)0); cor0.write(tmp_file1, (long)0); // close the files // tmp_file0.close(); tmp_file1.close(); // open the files in read mode // tmp_file0.open(tmp_filename0); tmp_file1.open(tmp_filename1); // read the object back // cor1.read(tmp_file0, (long)0); cor1.init(); if (!cor0.eq(cor1)) { return Error::handle(name(), L"i/o", Error::TEST, __FILE__, __LINE__); } cor1.read(tmp_file1, (long)0); cor1.init(); if (!cor0.eq(cor1)) { return Error::handle(name(), L"i/o", Error::TEST, __FILE__, __LINE__); } // close and delete the temporary files // tmp_file0.close(); tmp_file1.close(); File::remove(tmp_filename0); File::remove(tmp_filename1); // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // 3. class-specific public methods // set and get methods // //--------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: set and get methods...\n"); Console::increaseIndention(); } // set the parameters manually // cor0.setAlgorithm(AUTO); cor0.setImplementation(FACTORED); cor0.setNormalization(LENGTH); cor0.setOrder(27); // check that the values were set // if (cor0.algorithm_d != AUTO) { return Error::handle(name(), L"setAlgorithm", Error::TEST, __FILE__, __LINE__); } else if (cor0.implementation_d != FACTORED) { return Error::handle(name(), L"setImplementation", Error::TEST, __FILE__, __LINE__); } else if (cor0.normalization_d != LENGTH) { return Error::handle(name(), L"setNormalization", Error::TEST, __FILE__, __LINE__); } else if (cor0.order_d != (Long)27) { return Error::handle(name(), L"setOrder", Error::TEST, __FILE__, __LINE__); } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // 4. class-specific public methods // computational methods // //--------------------------------------------------------------------------- // set indentation // if (level_a > Integral::NONE) { Console::put(L"testing class-specific public methods: computational methods...\n"); Console::increaseIndention(); } // establish some signals // VectorFloat input(L"0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15"); VectorFloat output; VectorFloat zeros(L"0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0"); VectorFloat ones(L"1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1"); VectorFloat ans; // algorithm: AUTO // check that FACTORED and UNFACTORED produce the same result // cor0.setAlgorithm(AUTO); cor0.setImplementation(UNFACTORED); cor0.setNormalization(NONE); cor0.setOrder(4); cor0.compute(ans, input); cor0.setAlgorithm(AUTO); cor0.setImplementation(FACTORED); cor0.setNormalization(NONE); cor0.setOrder(4); cor0.compute(output, input); if (!output.almostEqual(ans)) { ans.debug(L"correct answer"); output.debug(L"wrong answer"); return Error::handle(name(), L"compute (AUTO) from sampled data", ERR, __FILE__, __LINE__); } // algorithm: AUTO_TAPERED // check that FACTORED and UNFACTORED produce the same result // cor0.setAlgorithm(AUTO_TAPERED); cor0.setImplementation(UNFACTORED); cor0.setNormalization(NONE); cor0.setOrder(4); cor0.compute(ans, input); cor0.setAlgorithm(AUTO_TAPERED); cor0.setImplementation(FACTORED); cor0.setNormalization(NONE); cor0.setOrder(4); cor0.compute(output, input); if (!output.almostEqual(ans)) { ans.debug(L"correct answer"); output.debug(L"wrong answer"); return Error::handle(name(), L"compute (AUTO_TAPERED) from sampled data", ERR, __FILE__, __LINE__); } // check that normalization works // VectorFloat input_norm(input); input_norm.mult(1.0 / input_norm.rms()); ans.assign(L"1.0000, 0.9070, 0.8140, 0.7210, 0.6281"); cor0.setAlgorithm(AUTO); cor0.setImplementation(UNFACTORED); cor0.setNormalization(UNIT_ENERGY); cor0.setOrder(4); cor0.compute(output, input_norm); if (!output.almostEqual(ans)) { ans.debug(L"correct answer"); output.debug(L"wrong answer"); return Error::handle(name(), L"compute (AUTO) with normalization", ERR, __FILE__, __LINE__); } ans.assign(L"1.31828, 1.1957, 1.07312, 0.950538, 0.827957"); cor0.setNormalization(LENGTH); cor0.setOrder(4); cor0.compute(output, input_norm); if (!output.almostEqual(ans)) { ans.debug(L"correct answer"); output.debug(L"wrong answer"); return Error::handle(name(), L"compute (AUTO) with normalization", ERR, __FILE__, __LINE__); } // compute a correlation function using predictor coefficients // input.assign(L"1.0, -2.34644, 1.65697,-0.00599, 0.32305, -1.48213, 1.15463, -0.18966, -0.05899"); cor0.setOrder(8); cor0.setAlgorithm(AUTO); cor0.compute(output, input, AlgorithmData::PREDICTION); // compute using the corresponding reflection coefficients // input.assign(L"-0.94217, 0.92386, -0.56198, -0.09454, 0.20218, 0.53595, -0.32922, -0.05899"); VectorFloat tmp; cor0.compute(tmp, input, AlgorithmData::REFLECTION); if (!output.almostEqual(tmp, 0.3)) { tmp.debug(L"correct answer"); output.debug(L"wrong answer"); return Error::handle(name(), L"compute (AUTO) from prediction coefficients", Error::TEST, __FILE__, __LINE__); } // test for degenerate cases // ans.assign(0.0); cor0.setAlgorithm(AUTO); cor0.setImplementation(FACTORED); cor0.setOrder(4); cor0.compute(output, zeros); if (!output.almostEqual(ans)) { ans.debug(L"correct answer"); output.debug(L"wrong answer"); return Error::handle(name(), L"compute for a zero-valued signal", Error::TEST, __FILE__, __LINE__); } cor0.setImplementation(UNFACTORED); cor0.compute(output, zeros); if (!output.almostEqual(ans)) { ans.debug(L"correct answer"); output.debug(L"wrong answer"); return Error::handle(name(), L"compute (AUTO) for a zero-valued signal", Error::TEST, __FILE__, __LINE__); } ans.assign(L"11.0, 11.0, 11.0, 11.0, 11.0"); cor0.setAlgorithm(AUTO); cor0.setImplementation(UNFACTORED); cor0.setNormalization(NONE); cor0.compute(output, ones); if (!output.almostEqual(ans)) { ans.debug(L"correct answer"); output.debug(L"wrong answer"); return Error::handle(name(), L"compute (AUTO) for a one-valued signal", Error::TEST, __FILE__, __LINE__); } cor0.setImplementation(FACTORED); cor0.setOrder(4); cor0.compute(output, ones); if (!output.almostEqual(ans)) { ans.debug(L"correct answer"); output.debug(L"wrong answer"); return Error::handle(name(), L"compute (AUTO) for a one-valued signal", Error::TEST, __FILE__, __LINE__); } // check the CROSS algorithm // input.assign(L"1, 2, 3, 2, 1"); VectorFloat ref(L"1, 2"); ans.assign(L"0.512989, 0.820783, 0.718185, 0.410391, 0.102598"); cor0.setAlgorithm(CROSS); cor0.setImplementation(UNFACTORED); cor0.setNormalization(UNIT_ENERGY); cor0.compute(output, input, ref); if (!output.almostEqual(ans)) { ans.debug(L"correct answer"); output.debug(L"wrong answer"); return Error::handle(name(), L"compute (CROSS)", ERR, __FILE__, __LINE__); } // check the CONV algorithm // cor0.setAlgorithm(CONV); ans.assign(L"1.0, 4.0, 7.0, 8.0, 5.0"); cor0.setAlgorithm(CONV); cor0.setImplementation(UNFACTORED); cor0.setNormalization(NONE); cor0.compute(output, input, ref); if (!output.almostEqual(ans)) { ans.debug(L"correct answer"); output.debug(L"wrong answer"); return Error::handle(name(), L"compute (CONV)", ERR, __FILE__, __LINE__); } // test for degenerate cases // ans.setLength(12); ans.assign(0.0); cor0.setAlgorithm(CROSS); cor0.setImplementation(UNFACTORED); cor0.setNormalization(UNIT_ENERGY); cor0.compute(output, zeros, ref); if (!output.almostEqual(ans)) { ans.debug(L"correct answer"); output.debug(L"wrong answer"); return Error::handle(name(), L"compute (CROSS) for a zero-valued signal", Error::TEST, __FILE__, __LINE__); } cor0.setAlgorithm(CONV); cor0.setNormalization(LENGTH); cor0.compute(output, zeros, ref); if (!output.almostEqual(ans)) { ans.debug(L"correct answer"); output.debug(L"wrong answer"); return Error::handle(name(), L"compute (CONV) for a zero-valued signal", Error::TEST, __FILE__, __LINE__); } // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } //--------------------------------------------------------------------------- // // 5. print completion message // //--------------------------------------------------------------------------- // reset indentation // if (level_a > Integral::NONE) { Console::decreaseIndention(); } // possibly print completion message // if (level_a > Integral::NONE) { String output(L"diagnostics completed successfully for class "); output.concat(name()); output.concat(L"\n"); Console::put(output); } // exit gracefully // return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -