Classes Utilities Scripts Speech Search Up Home
ISIP IFC Index
Title Bar

/home5/piconepr/public_html/isip/projects/speech/software/documentation/class/algo/Energy/enrgy_02.cc

// file: $isip/class/algo/Energy/enrgy_02.cc
// version: $Id: enrgy_02.cc 7497 2001-11-22 17:05:11Z parihar $
//

// isip include files
//
#include "Energy.h"
#include <Console.h>

[snipped]

// method: diagnose
//
// arguments:
//  Integral::DEBUG level: (input) debug level for diagnostics
//
// return: a boolean value indicating status
//
boolean Energy::diagnose(Integral::DEBUG level_a) {

  //---------------------------------------------------------------------------
  //
  // 0. preliminaries
  //
  //---------------------------------------------------------------------------

  // output the class name
  //
  if (level_a > Integral::NONE) {
    String 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...\n");
    Console::increaseIndention();
  }

  // test destructor/constructor(s) and memory management
  //
  Energy egy0;
  egy0.setAlgorithm(FILTER);
  egy0.setImplementation(POWER);
  egy0.setFloor(0.01);
  Energy egy1(egy0);
  
  if (!egy1.eq(egy0)) {
    return Error::handle(name(), L"copy constructor", 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
    //
    Energy::setGrowSize((long)500);
    
    Energy* pegy = new Energy();

    for (long j = 1; j <= 100; j++) {
      Energy** pegys = new Energy*[j * 100];
      
      // create the objects
      //
      for (long i = 0; i < j * 100; i++) {
	pegys[i] = new Energy();
      }
      
      // delete objects
      //
      for (long i = (j * 100) - 1; i >= 0; i--) {
	delete pegys[i];
      }
      
      delete [] pegys;
    }
    
    delete pegy;
  }

  // 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: i/o methods...\n");
    Console::increaseIndention();
  }

  // declare a reference object
  //
  VectorFloat ma_coef(L"1.0, 1.0");
  VectorFloat ar_coef(L"1.0, 0.5");

  egy0.setAlgorithm(FILTER);
  egy0.setImplementation(POWER);
  egy0.setFloor(0.01);
  egy0.setFilter(ma_coef, ar_coef);
  egy0.init();

  // 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);


  egy0.write(tmp_file0, (long)0);
  //  egy0.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
  //
  egy1.read(tmp_file0, (long)0);
  egy1.init();

  if (!egy0.eq(egy1)) {
    return Error::handle(name(), L"i/o", Error::TEST,
			 __FILE__, __LINE__);
  }
    
  egy1.read(tmp_file1, (long)0);
  egy1.init();

  if (!egy0.eq(egy1)) {
    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();
  }

  // establish an object
  //
  egy0.setAlgorithm(SUM);
  egy0.setImplementation(POWER);
  egy0.setFloor(27.0);

  // check that the values were set
  //
  if (egy0.algorithm_d != SUM) {
    return Error::handle(name(), L"setAlgorithm", Error::TEST,
			 __FILE__, __LINE__);
  }
  else if (egy0.implementation_d != POWER) {
    return Error::handle(name(), L"setImplementation", Error::TEST,
			 __FILE__, __LINE__);
  }
  else if (egy0.floor_d != (float)27.0) {
    return Error::handle(name(), L"setDuration", Error::TEST,
			 __FILE__, __LINE__);
  }

  // test setFilter
  //
  ma_coef.assign(L"1.0, 1.0");
  ar_coef.assign(L"1.0, 0.5");
  VectorFloat ma_tmp, ar_tmp;
  egy0.setAlgorithm(FILTER);
  egy0.setFilter(ma_coef, ar_coef);
  egy0.getFilter(ma_tmp, ar_tmp);

  if (!ma_tmp.eq(ma_coef)) {
    return Error::handle(name(), L"setFilter - ma coef", Error::TEST,
			 __FILE__, __LINE__);
  }
  if (!ar_tmp.eq(ar_coef)) {
    return Error::handle(name(), L"setFilter - ar coef", Error::TEST,
			 __FILE__, __LINE__);
  }

  // reset indentation
  //
  if (level_a > Integral::NONE) {
    Console::decreaseIndention();
  }
  
  //---------------------------------------------------------------------------
  //
  // 4. class-specific public methods:
  //     computation methods
  //
  //---------------------------------------------------------------------------

  // set indentation
  //
  if (level_a > Integral::NONE) {
    Console::put(L"testing class-specific public methods: computational methods...\n");
    Console::increaseIndention();
  }

  // declare local variables
  //
  VectorFloat input;
  VectorFloat ans;
  VectorFloat output;

  // perform a simple test that compares SUM to FILTER:
  //  run the filter twice to make sure the history works properly
  //
  input.assign(L"1, 2, 3, 4");

  egy0.setAlgorithm(SUM);
  egy0.setImplementation(IDENTITY);
  egy0.compute(ans, input);

  VectorFloat ma(L"1, 1, 1, 1");
  VectorFloat ar;

  egy0.setAlgorithm(FILTER);
  egy0.setImplementation(IDENTITY);
  egy0.setFilter(ma, ar);
  input.assign(L"1, 0, 1, 0");
  egy0.compute(output, input);
  input.assign(L"1, 2, 3, 4");
  egy0.compute(output, input);

  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  // check the complex mode
  //
  VectorComplexFloat input_cmplx(L"1+1j, 2+2j, 3+3j");
  ans.assign(L"28");
  
  egy0.setAlgorithm(SUM);
  egy0.setImplementation(IDENTITY);
  egy0.compute(output, input_cmplx);

  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  // check the FILTER mode more extensively
  //
  input.assign(L"1, 2, 3, 4");
  ans.assign(L"22.9766");

  ma.assign(L"1");
  ar.assign(L"1, -0.5");
  egy0.setAlgorithm(FILTER);
  egy0.setImplementation(IDENTITY);
  egy0.setFloor(1.0);
  egy0.setFilter(ma, ar);
  egy0.compute(output, input);
  egy0.compute(output, input);

  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  // test the scaling modes using a longer signal
  //
  egy0.setAlgorithm(SUM);
  input.assign(L"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,48,32,32,0,32,-32,16,48,48,16,-48,0,16,32,-32,-16,0,0,16,16,-32,-32,32,0,-16,-48,0,0,-16,0,-32,-48,48,-32,48,16,0,-48,-48,0,48,-32,0,16,0,16,48,48,-48,0,48,0,48,-32,48,16,-48,-16,32,32,-48,48,32,0,-48,-32,-16,48,48,16,32,0,-16,16,0,-32,-48,-48,0,16,0,-32,32,-48,32,0,48,-48,-48,32,32,0,48,48,-48,-48,0,16,-16,32,-16,16,-32,-16,0,32,48,0,0,48,0,0,-16,0,16,16,0,0,0,-32,0,32,16,0,32,-48,16,32,16,32,16,48,0,32,-16,16,-48,0,48,-32,0,-16,-48,-32,-32,16,-48,00,-16,16,-48");

  ans.assign(L"142848");
  egy0.setImplementation(IDENTITY);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }
    
  ans.assign(L"11.869536406860874");
  egy0.setAlgorithm(SUM);
  egy0.setImplementation(LOG);
  egy0.compute(output, input);

  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  ans.assign(L"51.5487");
  egy0.setImplementation(DB);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  ans.assign(L"714.24");
  egy0.setImplementation(POWER);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }
  
  ans.assign(L"6.57122");
  egy0.setImplementation(LOG_POWER);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  ans.assign(L"28.5385");
  egy0.setImplementation(DB_POWER);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }
  
  ans.assign(L"26.7253");
  egy0.setImplementation(RMS);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }
    
  ans.assign(L"3.2856");
  egy0.setImplementation(LOG_RMS);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }
    
  ans.assign(L"28.5385");
  egy0.setImplementation(DB_RMS);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }
    
  // test with an all zero vector:
  //  note the use of an energy floor to make sure we correctly clip the result
  //
  input.assign(L"0, 0, 0, 0");
  egy0.setFloor(-10);

  ans.assign(L"0.0");
  egy0.setImplementation(IDENTITY);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  egy0.setImplementation(LOG);
  ans.assign(L"-10");
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  egy0.setImplementation(DB);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  egy0.setImplementation(POWER);
  ans.assign(L"0");
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }
  
  egy0.setImplementation(LOG_POWER);
  ans.assign(L"-10");
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  egy0.setImplementation(DB_POWER);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }
  
  egy0.setImplementation(RMS);
  ans.assign(L"0");
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  egy0.setImplementation(LOG_RMS);
  ans.assign(L"-10");
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  egy0.setImplementation(DB_RMS);
  ans.assign(L"-10");
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  // test with a constant vector
  //
  input.assign(L"1, 1, 1, 1");

  ans.assign(L"4.0");
  egy0.setImplementation(IDENTITY);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }
    
  ans.assign(L"1.386294");
  egy0.setImplementation(LOG);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  ans.assign(L"6.0206");
  egy0.setImplementation(DB);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  ans.assign(L"1.0");
  egy0.setImplementation(POWER);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }
  
  ans.assign(L"0.0");
  egy0.setImplementation(LOG_POWER);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }

  ans.assign(L"0.0");
  egy0.setImplementation(DB_POWER);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }
  
  ans.assign(L"1.0");
  egy0.setImplementation(RMS);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }
    
  ans.assign(L"0.0");
  egy0.setImplementation(LOG_RMS);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", Error::TEST, __FILE__, __LINE__);
  }
    
  ans.assign(L"0.0");
  egy0.setImplementation(DB_RMS);
  egy0.compute(output, input);
  if (!output.almostEqual(ans)) {
    ans.debug(L"expected result:");
    output.debug(L"wrong result:");
    return Error::handle(name(), L"compute", 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();
  }
  
  if (level_a > Integral::NONE) {
    String output(L"diagnostics passed for class ");
    output.concat(name());
    output.concat(L"\n");
    Console::put(output);
  }
  
  // exit gracefully
  //
  return true;
}
//
// end of file


Footer

Up | Home | Courses | Projects | Proposals | Publications Please direct questions or comments to joseph.picone@isip.piconepress.com