Restructuring ITBase tests and adding spectrum test

parent a5a7e8c8
......@@ -21,6 +21,7 @@
#include <ITABaseDefinitions.h>
#include <iostream>
#include <string>
#include <vector>
......@@ -43,6 +44,9 @@ public:
void SetName( const std::string& sVolatileName );
std::string GetName() const;
std::string GetValueUnit() const;
void SetValueUnit( std::string sUnit );
int GetNumBands() const;
const std::vector< float >& GetCenterFrequencies() const;
......@@ -79,14 +83,17 @@ public:
//! Index operator for actual values
float& operator[]( int iFrequencyBandIndex );
protected:
std::vector< float > m_vfValues; //!< Scalar spectrum values of any kind
std::vector< float > m_vfCenterFrequencies; //!< Base or center frequencies for bands
private:
std::string m_sName; //!< Volatile name for debugging / backtracking
std::string m_sName; //!< Volatile name for output formatting
std::string m_sValueUnit; //!< Value unit, such as dB, dB(A) ... for output formatting.
};
//! STL stream output operator
ITA_BASE_API std::ostream& operator<<( std::ostream& os, const CITASpectrum& oSpectrum );
#endif // INCLUDE_WATCHER_ITA_SPECTRUM
......@@ -6,6 +6,7 @@
CITAMagnitudeSpectrum::CITAMagnitudeSpectrum( const int nNumBands )
: CITASpectrum( nNumBands )
{
SetValueUnit( "dB" );
}
void CITAMagnitudeSpectrum::SetMagnitudes( const std::vector< float >& vfMagnitudes )
......
......@@ -20,6 +20,16 @@ std::string CITASpectrum::GetName() const
return m_sName;
}
void CITASpectrum::SetValueUnit( std::string sUnit )
{
m_sValueUnit = sUnit;
}
std::string CITASpectrum::GetValueUnit() const
{
return m_sValueUnit;
}
int CITASpectrum::GetNumBands() const
{
return int( m_vfCenterFrequencies.size() );
......@@ -120,3 +130,20 @@ float& CITASpectrum::operator[]( int iFrequencyBandIndex )
float& rV( m_vfValues[ iFrequencyBandIndex ] );
return rV;
}
std::ostream& operator<<( std::ostream& os, const CITASpectrum& oSpectrum )
{
std::string sName = "Unnamed spectrum";
if( !oSpectrum.GetName().empty() )
sName = oSpectrum.GetName();
os << "[ ITASpectrum ] '" << sName << "' spectrum with " << oSpectrum.GetNumBands() << " bands." << std::endl;
for( int n = 0; n < oSpectrum.GetNumBands(); n++ )
{
if( n > 0 )
os << ", ";
os << oSpectrum.GetCenterFrequencies()[ n ] << " Hz = " << oSpectrum[ n ] << " " << oSpectrum.GetValueUnit();
}
return os;
}
\ No newline at end of file
......@@ -8,28 +8,38 @@ include( VistaCommon )
vista_use_package( ITABase REQUIRED FIND_DEPENDENCIES )
add_executable( SampleBufferTest SampleBufferTest.cpp )
target_link_libraries( SampleBufferTest ${VISTA_USE_PACKAGE_LIBRARIES} )
add_executable( ITABaseSampleBufferTest ITABaseSampleBufferTest.cpp )
target_link_libraries( ITABaseSampleBufferTest ${VISTA_USE_PACKAGE_LIBRARIES} )
vista_configure_app( SampleBufferTest )
vista_install( SampleBufferTest )
vista_create_default_info_file( SampleBufferTest )
vista_configure_app( ITABaseSampleBufferTest )
vista_install( ITABaseSampleBufferTest )
vista_create_default_info_file( ITABaseSampleBufferTest )
set_property( TARGET SampleBufferTest PROPERTY FOLDER "ITACoreLibs/Tests/ITABase" )
set_property( TARGET ITABaseSampleBufferTest PROPERTY FOLDER "ITACoreLibs/Tests/ITABase" )
if( ITA_BASE_WITH_SNDFILE )
add_executable( SampleFrameTest SampleFrameTest.cpp )
target_link_libraries( SampleFrameTest ${VISTA_USE_PACKAGE_LIBRARIES} )
add_executable( ITABaseSampleFrameTest ITABaseSampleFrameTest.cpp )
target_link_libraries( ITABaseSampleFrameTest ${VISTA_USE_PACKAGE_LIBRARIES} )
vista_configure_app( SampleFrameTest )
vista_install( SampleFrameTest )
vista_create_default_info_file( SampleFrameTest )
vista_configure_app( ITABaseSampleFrameTest )
vista_install( ITABaseSampleFrameTest )
vista_create_default_info_file( ITABaseSampleFrameTest )
set_property( TARGET SampleFrameTest PROPERTY FOLDER "ITACoreLibs/Tests/ITABase" )
set_property( TARGET ITABaseSampleFrameTest PROPERTY FOLDER "ITACoreLibs/Tests/ITABase" )
endif( )
add_executable( ITABaseSpectrumTests ITABaseSpectrumTests.cpp )
target_link_libraries( ITABaseSpectrumTests ${VISTA_USE_PACKAGE_LIBRARIES} )
vista_configure_app( ITABaseSpectrumTests )
vista_install( ITABaseSpectrumTests )
vista_create_default_info_file( ITABaseSpectrumTests )
set_property( TARGET ITABaseSpectrumTests PROPERTY FOLDER "ITACoreLibs/Tests/ITABase" )
add_subdirectory( "VistaTests" )
#include <ITASpectrum.h>
#include <ITAMagnitudeSpectrum.h>
#include <ITAThirdOctaveMagnitudeSpectrum.h>
#include <ITANumericUtils.h>
#include <stdio.h>
using namespace std;
void test_third_octave_spectrum();
void test_db_to_ration();
void test_ratio_to_db();
int main( int, char** )
{
test_third_octave_spectrum();
cout << "-----------" << endl;
test_db_to_ration();
cout << "-----------" << endl;
test_ratio_to_db();
cout << "-----------" << endl;
return 0;
}
void test_db_to_ration()
{
CITAThirdOctaveMagnitudeSpectrum oTOSpectrumConvertToRatio;
oTOSpectrumConvertToRatio.SetName( "Test unit magnitude spectrum to ratio" );
oTOSpectrumConvertToRatio.SetIdentity();
for( int n = 0; n < oTOSpectrumConvertToRatio.GetNumBands(); n++ )
oTOSpectrumConvertToRatio[ n ] = db10_to_ratio( oTOSpectrumConvertToRatio[ n ] );
oTOSpectrumConvertToRatio.SetValueUnit( "(gain)" );
cout << oTOSpectrumConvertToRatio << endl;
oTOSpectrumConvertToRatio.SetZero();
for( int n = 0; n < oTOSpectrumConvertToRatio.GetNumBands(); n++ )
oTOSpectrumConvertToRatio[ n ] = db10_to_ratio( oTOSpectrumConvertToRatio[ n ] );
oTOSpectrumConvertToRatio.SetValueUnit( "(gain)" );
cout << oTOSpectrumConvertToRatio << endl;
}
void test_ratio_to_db()
{
CITAThirdOctaveMagnitudeSpectrum oTOSpectrumConvertToRatio;
oTOSpectrumConvertToRatio.SetName( "Test unit gain spectrum to db" );
oTOSpectrumConvertToRatio.SetValueUnit( "(gain)" );
oTOSpectrumConvertToRatio.SetZero();
for( int n = 0; n < oTOSpectrumConvertToRatio.GetNumBands(); n++ )
oTOSpectrumConvertToRatio[ n ] = ratio_to_db10( oTOSpectrumConvertToRatio[ n ] );
oTOSpectrumConvertToRatio.SetValueUnit( "dB" );
cout << oTOSpectrumConvertToRatio << endl;
}
void test_third_octave_spectrum()
{
CITAThirdOctaveMagnitudeSpectrum oTOSpectrumIdent;
oTOSpectrumIdent.SetName( "Test unit magnitude spectrum" );
oTOSpectrumIdent.SetIdentity();
cout << "Third octave magnitude spectrum identity:" << endl;
cout << oTOSpectrumIdent << endl;
CITAThirdOctaveMagnitudeSpectrum oTOSpectrumZero;
oTOSpectrumZero.SetName( "Test unit zero spectrum" );
oTOSpectrumZero.SetZero();
cout << "Third octave magnitude spectrum zero:" << endl;
cout << oTOSpectrumZero << endl;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment