ITAConvolution_BM_UPConv.cpp 5.3 KB
Newer Older
1 2 3 4 5
/*
* ----------------------------------------------------------------
*
*		ITA core libs
*		(c) Copyright Institute of Technical Acoustics (ITA)
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
6
*		RWTH Aachen University, Germany, 2015-2020
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
*
* ----------------------------------------------------------------
*				    ____  __________  _______
*				   //  / //__   ___/ //  _   |
*				  //  /    //  /    //  /_|  |
*				 //  /    //  /    //  ___   |
*				//__/    //__/    //__/   |__|
*
* ----------------------------------------------------------------
*
* 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;
41 42

ITASampleBuffer* pbfImpulse;
43 44 45 46 47 48 49 50 51 52 53 54 55 56

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

	inline void SetSomeDefaults()
	{
		dSampleRate = 44.1e3;
#ifdef DEBUG
57
		iNumFrames = 1000;
58
#else
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
59
		iNumFrames = 1e2;
60
#endif
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
61
		iScalingApproximation = 64;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	};

	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** )
{
81 82 83 84 85 86 87 88
	pbfImpulse = new ITASampleBuffer( int( 1000 * 44.1e3 ), true );

	srand( 100 );
	int iLength = pbfImpulse->GetLength();
	for( unsigned int i = 0; i < iLength; i++ )
		pbfImpulse->GetData()[ i ] = ( float ) rand() / RAND_MAX;


89 90
	cout << "Starting uniformly partitioned block convolution benchmark" << endl;

91
	INIFileUseFile( "ITAConvolution_BM_UPConv.ini" );
92 93 94 95 96 97 98 99 100 101 102 103 104 105

	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";
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
			cout << "\tStarting " << sBMID << " ";
			//run_benchmark( pm, sBMID );
			cout << "\tdone." << endl;
		}
	}


	INIFileUseFile( "ITAConvolution_BM_UPConv_ScalingBehavior.ini" );

	for( auto BL : { 256 } )
	{
		for( auto BM : { 1, 2, 4, 8, 16, 32, 64, 128, 512, 1024, 2048, 4096, 8182 } )
		{
			stringstream ss;
			ss << "ITAConvolution_BM_NUPConv_ScalingBehavior_L" << BL << "_M" << BL*BM;
			string sBMID = ss.str();
			pm.iBlockLength = BL;
			pm.iFilterLength = BL*BM;
			pm.sWAVOutFilePath = sBMID + ".wav";
125 126 127 128 129 130
			cout << "\tStarting " << sBMID << " ";
			run_benchmark( pm, sBMID );
			cout << "\tdone." << endl;
		}
	}

131

132 133 134 135 136 137 138 139 140 141 142 143 144 145
	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 );
146 147
	ITAUPFilter* pFIRFilter = pFIRFilterEnginge->RequestFilter();;
	pFIRFilter->Load( pbfImpulse->GetData(), std::min( pbfImpulse->GetLength(), pm.iFilterLength ) );
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
	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;
}