Commit 715db06b authored by Dipl.-Ing. Jonas Stienen's avatar Dipl.-Ing. Jonas Stienen
Browse files

Imrpoving spectra

parent 89328b6c
......@@ -78,8 +78,11 @@ namespace ITABase
*/
void Init( const int iLength, const float fSampleRate, const bool bZeroInit = true );
//! Set a dirac pulse / delta function (first sample 1, rest 0)
void SetDirac();
//! Set a dirac pulse / delta function (first sample 1 (or amplitude), rest 0)
void SetDirac( float fAmplitude = 1.0f );
//! Set a unit step function (all samples 1 (or amplitude))
void SetUnitStepFunction( float fAmplitude = 1.0f );
private:
......
......@@ -334,6 +334,14 @@ public:
//! Normalize data
void Normalize();
//! Calculate root-mean-square
/**
* @param[in] bPower If true, the RMS value is divided by the length of the buffer
*
* @return Root-Mean-Square value
*/
float RootMeanSquare( bool bPower = true ) const;
//! Read/Write Indizierungsoperator
float& operator[]( int iSample );
......
......@@ -92,6 +92,9 @@ public:
//! Index operator for actual values
float& operator[]( int iFrequencyBandIndex );
//! Set entire values of the spectrum by extrapolating range
void SetValuesExtrapolated( const std::vector< float >& vfProvidedFrequencies, const std::vector< float >& vfProvidedValues, const int iStrategy = ITABase::InterpolationFunctions::NONE );
protected:
std::vector< float > m_vfValues; //!< Scalar spectrum values of any kind
......
......@@ -41,7 +41,14 @@ float CFiniteImpulseResponse::GetNyquistFrequency() const
return GetSampleRate() / 2.0f;
}
void ITABase::CFiniteImpulseResponse::SetDirac()
void ITABase::CFiniteImpulseResponse::SetDirac( float fAmplitude /* = 1.0f */ )
{
Identity();
Identity(); // also validates buffer
if( fAmplitude != 1.0f )
ITASampleBuffer::mul_scalar( fAmplitude ); // not efficient, but safe
}
void ITABase::CFiniteImpulseResponse::SetUnitStepFunction( float fAmplitude /* = 1.0f */ )
{
Fill( fAmplitude );
}
......@@ -550,6 +550,17 @@ void ITASampleBuffer::Normalize()
mul_scalar( FindPeak() );
}
float ITASampleBuffer::RootMeanSquare( bool bPower /* = true */ ) const
{
float fRMS = 0.0f;
for( int i = 0; i < m_iLength; i++ )
fRMS += std::powf( m_pfData[ i ], 2 );
return std::sqrtf( fRMS ) / ( bPower ? ( float ) m_iLength : 1.0f );
}
float& ITASampleBuffer::operator[]( int iSample )
{
return m_pfData[ iSample ];
......
......@@ -157,6 +157,43 @@ float& CITASpectrum::operator[]( int iFrequencyBandIndex )
return rV;
}
void CITASpectrum::SetValuesExtrapolated( const std::vector< float >& vfProvidedFrequencies, const std::vector< float >& vfProvidedValues, const int iStrategy /* =NONE */ )
{
if( vfProvidedFrequencies.size() != vfProvidedValues.size() )
ITA_EXCEPT1( INVALID_PARAMETER, "Frequency-value count mismatch in interpolation routine" );
if( vfProvidedFrequencies.size() < 1 )
ITA_EXCEPT1( INVALID_PARAMETER, "At least one frequency-value sample must be provided" );
if( iStrategy != ITABase::InterpolationFunctions::NONE )
ITA_EXCEPT1( INVALID_PARAMETER, "Only value repetition at boundaries is supported, yet" );
if( vfProvidedFrequencies.size() > GetNumBands() )
ITA_EXCEPT1( INVALID_PARAMETER, "Provided values must be less than available frequencies to extrapolate range" );
size_t iExtrapolatedRangeLower = 0;
size_t iExtrapolatedRangeUpper = m_vfCenterFrequencies.size();
for( size_t i = 0; i < m_vfCenterFrequencies.size(); i++ )
{
if( m_vfCenterFrequencies[ i ] < vfProvidedFrequencies[ 0 ] )
{
m_vfValues[ i ] = vfProvidedValues[ 0 ];
iExtrapolatedRangeLower = i;
}
else if( m_vfCenterFrequencies[ i ] > vfProvidedFrequencies[ vfProvidedFrequencies.size() - 1 ] )
{
m_vfValues[ i ] = vfProvidedValues[ vfProvidedFrequencies.size() - 1 ];
if( i < iExtrapolatedRangeUpper )
iExtrapolatedRangeUpper = i;
}
else
{
// Assign
m_vfValues[ i ] = vfProvidedValues[ i - iExtrapolatedRangeLower - 1 ];
}
}
}
std::ostream& operator<<( std::ostream& os, const CITASpectrum& oSpectrum )
{
std::string sName = "Unnamed spectrum";
......
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