ITAConvolution_BM_UPConv.cpp 4.63 KB
Newer Older
1 2 3 4 5
/*
* ----------------------------------------------------------------
*
*		ITA core libs
*		(c) Copyright Institute of Technical Acoustics (ITA)
6
*		RWTH Aachen University, Germany, 2015-2019
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
*
* ----------------------------------------------------------------
*				    ____  __________  _______
*				   //  / //__   ___/ //  _   |
*				  //  /    //  /    //  /_|  |
*				 //  /    //  /    //  ___   |
*				//__/    //__/    //__/   |__|
*
* ----------------------------------------------------------------
*
* Benchmark uniform partitioned convolution
*
*/

#include <ITAUPConvolution.h>
#include <ITAUPFilter.h>

#include <ITAStringUtils.h>
#include <ITAAudiofileWriter.h>
#include <ITASampleBuffer.h>
#include <ITASampleFrame.h>
#include <ITAStopWatch.h>
#include <ITAConfigUtils.h>

#include <ITAStreamFunctionGenerator.h>
#include <ITAFileDataSource.h>
#include <ITAStreamInfo.h>

#include <iostream>
#include <math.h>
#include <vector>
#include <stdio.h>

using namespace std;
const std::string sBMFilePath = "ITAConvolution_BM_UPConv.ini";

struct ParameterSet
{
	unsigned int iBlockLength;
	double dSampleRate;
	string sWAVOutFilePath;
	unsigned long iNumFrames;
	int iFilterLength;
	int iScalingApproximation;

	inline void SetSomeDefaults()
	{
		dSampleRate = 44.1e3;
#ifdef DEBUG
		iNumFrames = 100;
#else
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
58
		iNumFrames = 1e2;
59
#endif
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
60
		iScalingApproximation = 64;
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
	};

	inline void SetSomeDependentParameters()
	{
		iFilterLength = 2 * iBlockLength;
	};

	inline void WriteToINIFile() const
	{
		INIFileWriteInt( "BlockLength", iBlockLength );
		INIFileWriteDouble( "SampleRate", dSampleRate );
		INIFileWriteInt( "FilterLength", iFilterLength );
		INIFileWriteInt( "NumFrames", iNumFrames );
		INIFileWriteString( "WAVOutFilePath", sWAVOutFilePath );
		INIFileWriteDouble( "BlockTimeSeconds", iBlockLength / dSampleRate );
		INIFileWriteString( "BlockTime", timeToString( iBlockLength / dSampleRate ) );
		INIFileWriteInt( "ScalingApproximation", iScalingApproximation );
	};
};

void run_benchmark( const ParameterSet&, const std::string& );

int main( int, char** )
{
	cout << "Starting uniformly partitioned block convolution benchmark" << endl;

	INIFileUseFile( sBMFilePath );

	ParameterSet pm;
	pm.SetSomeDefaults();

	for( auto BL : { 32, 64, 128, 256 } )
	{
		for( auto BM : { 1, 2, 4, 8, 16 } )
		{
			stringstream ss;
			ss << "ITAConvolution_BM_NUPConv_L" << BL << "_M" << BL*BM;
			string sBMID = ss.str();
			pm.iBlockLength = BL;
			pm.iFilterLength = BL*BM;
			pm.sWAVOutFilePath = sBMID + ".wav";
			pm.SetSomeDependentParameters();
			cout << "\tStarting " << sBMID << " ";
			run_benchmark( pm, sBMID );
			cout << "\tdone." << endl;
		}
	}

	cout << "All done." << endl;

	return 255;
}

void run_benchmark( const ParameterSet& pm, const std::string& sName )
{
	INIFileUseSection( sName );
	pm.WriteToINIFile();

	ITAStreamFunctionGenerator sinesignal( 1, pm.dSampleRate, pm.iBlockLength, ITAStreamFunctionGenerator::SINE, 500.0f, 0.9f, true );
	ITADatasource* pIntputStream = &sinesignal;

	ITAUPConvolution* pFIRFilterEnginge = new ITAUPConvolution( pm.iBlockLength, pm.iFilterLength );
	ITAUPFilter* pFIRFilter = pFIRFilterEnginge->RequestFilter();
	pFIRFilter->identity();
	pFIRFilterEnginge->ExchangeFilter( pFIRFilter );
	pFIRFilter->Release();
	
	ITAAudiofileProperties props_out;
	props_out.iChannels = 1;
	props_out.dSampleRate = pm.dSampleRate;
	props_out.eQuantization = ITAQuantization::ITA_FLOAT;
	props_out.eDomain = ITADomain::ITA_TIME_DOMAIN;
	props_out.iLength = pm.iNumFrames * ( unsigned int ) ( pm.iBlockLength );
	props_out.iChannels = 1;
	ITAAudiofileWriter* writer_out = ITAAudiofileWriter::create( pm.sWAVOutFilePath, props_out );

	ITAStreamInfo oState;

	ITASampleBuffer* psbInput = new ITASampleBuffer( pm.iBlockLength, true );
	ITASampleFrame sfTemp( 1, pm.iBlockLength, true );

	ITAStopWatch swBenchmark;

	long unsigned int n = 0;
	while( n < int( pm.iNumFrames ) )
	{
		// Add new samples
		psbInput->write( pIntputStream->GetBlockPointer( 0, &oState ), pm.iBlockLength );

		swBenchmark.start();
		pFIRFilterEnginge->Process( psbInput->GetData(), sfTemp[ 0 ].GetData() );
		swBenchmark.stop();

		INIFileWriteDouble( "ComputationMean", swBenchmark.mean() );
		INIFileWriteDouble( "ComputationStdDev", swBenchmark.std_deviation() );
		INIFileWriteDouble( "ComputationMinimum", swBenchmark.minimum() );
		INIFileWriteDouble( "ComputationMaximum", swBenchmark.maximum() );
		INIFileWriteDouble( "ComputationScalingApproximation", swBenchmark.mean() * pm.iScalingApproximation );

		writer_out->write( &sfTemp, sfTemp.GetLength() );
		n++;

		pIntputStream->IncrementBlockPointer();

		if( n % ( pm.iNumFrames / 10 ) == 0 )
			cout << ".";
	}

	delete writer_out;

	delete psbInput;
}