ITAHDFTSpectra.cpp 4.88 KB
Newer Older
1
2
3
#include <ITAHDFTSpectra.h>

#include <ITAAudiofileWriter.h>
Michael Kohnen's avatar
Michael Kohnen committed
4
#include <ITAConstants.h>
5
6
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
#include <ITAFilesystemUtils.h>
#include <ITAHDFTSpectrum.h>
#include <ITASampleFrame.h>
#include <ITAStringUtils.h>

ITAHDFTSpectra::ITAHDFTSpectra( const double dSampleRate, const int iNumChannels, const int iDFTSize, const bool bZeroInit/*=true*/ )
{
	if( iNumChannels < 1 )
		ITA_EXCEPT1( INVALID_PARAMETER, "At least one DFT channel must be used" );
	if( iDFTSize < 1 )
		ITA_EXCEPT1( INVALID_PARAMETER, "Invalid DFT size" );

	for( int i=0; i<iNumChannels; i++ )
		m_vpSpectra.push_back( new ITAHDFTSpectrum( dSampleRate, iDFTSize, bZeroInit ) );
}

ITAHDFTSpectra::ITAHDFTSpectra( const std::vector< ITAHDFTSpectrum* >& vpSpectrumVec )
{
	if( vpSpectrumVec.size() == 0 )
		ITA_EXCEPT1( INVALID_PARAMETER, "At least one DFT channel must be used" );

	for( size_t i=0; i<vpSpectrumVec.size(); i++ )
	{
		const ITAHDFTSpectrum* pSpectrum( vpSpectrumVec[i] );
		if( pSpectrum->getDFTSize() <= 0 )
			ITA_EXCEPT1( INVALID_PARAMETER, "Invalid DFT size in spectrum number " + IntToString( int(i) ) );

		if( pSpectrum->getSamplerate() <= 0 )
			ITA_EXCEPT1( INVALID_PARAMETER, "Invalid sampling rate in spectrum number " + IntToString( int(i) ) );

		m_vpSpectra.push_back( new ITAHDFTSpectrum( pSpectrum ) ); // copy
	}
}

ITAHDFTSpectra::~ITAHDFTSpectra()
{
	for( size_t i=0; i<m_vpSpectra.size(); i++ )
		delete m_vpSpectra[i];
}

Michael Kohnen's avatar
Michael Kohnen committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
float ITAHDFTSpectra::GetMaxEnergy( int* piIdx ) const
{
	float fEnergyTemp;
	float fEnergyMax = -1.0f;
	for (size_t n = 0; n < m_vpSpectra.size(); n++)
	{
		fEnergyTemp = m_vpSpectra[n]->getEnergy();
		if (fEnergyTemp > fEnergyMax)
		{
			fEnergyMax = fEnergyTemp;
			*piIdx = n;
		}
	}

	return fEnergyMax;
}

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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
void ITAHDFTSpectra::CopyFrom(const ITAHDFTSpectra *otherSpectra)
{
	int iNumChannels=otherSpectra->GetNumChannels();
	
	m_vpSpectra.clear();
	
	for( int i=0; i<iNumChannels; i++ )
	{
        //ITAHDFTSpectrum* tempSpectrum = new ITAHDFTSpectrum(double(otherSpectra->GetSampleRate()),int(otherSpectra->GetDFTSize()));
        ITAHDFTSpectrum* tempSpectrum = new ITAHDFTSpectrum((*otherSpectra)[i]);
        //tempSpectrum->copyFrom((*otherSpectra)[i]);
		m_vpSpectra.push_back(tempSpectrum);
	}
}

void ITAHDFTSpectra::SetUnity()
{
	for( size_t i=0; i<m_vpSpectra.size(); i++ )
	{
		ITAHDFTSpectrum* pSpectrum( m_vpSpectra[i] );
		pSpectrum->SetUnity();
	}
}

void ITAHDFTSpectra::SetZero()
{
	for( size_t i=0; i<m_vpSpectra.size(); i++ )
	{
		ITAHDFTSpectrum* pSpectrum( m_vpSpectra[i] );
		pSpectrum->mul( 0.0f );
	}
}

int ITAHDFTSpectra::GetNumChannels() const
{
	return int( m_vpSpectra.size() );
}

int ITAHDFTSpectra::GetDFTSize() const
{
	return m_vpSpectra[0]->getDFTSize();
}

double ITAHDFTSpectra::GetSampleRate() const
{
	return m_vpSpectra[0]->getSamplerate();
}

void ITAHDFTSpectra::add( const ITAHDFTSpectra* pSource )
{
	if( GetNumChannels() != pSource->GetNumChannels() )
		ITA_EXCEPT1( INVALID_PARAMETER, "Channel number mismatch" );

	for( int i=0; i<GetNumChannels(); i++ )
	{
		ITAHDFTSpectrum* pSpectrum( m_vpSpectra[i] );
		const ITAHDFTSpectrum* pSourceSpectrum( (*pSource)[i] );
		pSpectrum->add( pSourceSpectrum );
	}

	return;
}

void ITAHDFTSpectra::sub( const ITAHDFTSpectra* pSource )
{
	if( GetNumChannels() != pSource->GetNumChannels() )
		ITA_EXCEPT1( INVALID_PARAMETER, "Channel number mismatch" );

	for( int i=0; i<GetNumChannels(); i++ )
	{
		ITAHDFTSpectrum* pSpectrum( m_vpSpectra[i] );
		const ITAHDFTSpectrum* pSourceSpectrum( (*pSource)[i] );
		pSpectrum->sub( pSourceSpectrum );
	}

	return;
}

void ITAHDFTSpectra::mul( const ITAHDFTSpectra* pSource )
{
	if( GetNumChannels() != pSource->GetNumChannels() )
		ITA_EXCEPT1( INVALID_PARAMETER, "Channel number mismatch" );

	for( int i=0; i<GetNumChannels(); i++ )
	{
		ITAHDFTSpectrum* pSpectrum( m_vpSpectra[i] );
		const ITAHDFTSpectrum* pSourceSpectrum( (*pSource)[i] );
		pSpectrum->mul( pSourceSpectrum );
	}

	return;
}

void ITAHDFTSpectra::mul_conj( const ITAHDFTSpectra* pSource )
{
	if( GetNumChannels() != pSource->GetNumChannels() )
		ITA_EXCEPT1( INVALID_PARAMETER, "Channel number mismatch" );

	for( int i=0; i<GetNumChannels(); i++ )
	{
		ITAHDFTSpectrum* pSpectrum( m_vpSpectra[i] );
		const ITAHDFTSpectrum* pSourceSpectrum( (*pSource)[i] );
		pSpectrum->mul( pSourceSpectrum );
	}

	return;
}

void ITAHDFTSpectra::mul_scalar( double )
{
	ITA_EXCEPT0( NOT_IMPLEMENTED );
}

void ITAHDFTSpectra::div( const ITAHDFTSpectra* pSource )
{
	if( GetNumChannels() != pSource->GetNumChannels() )
		ITA_EXCEPT1( INVALID_PARAMETER, "Channel number mismatch" );

	for( int i=0; i<GetNumChannels(); i++ )
	{
		ITAHDFTSpectrum* pSpectrum( m_vpSpectra[i] );
		const ITAHDFTSpectrum* pSourceSpectrum( (*pSource)[i] );
		pSpectrum->div( pSourceSpectrum );
	}

	return;
}

const ITAHDFTSpectrum* ITAHDFTSpectra::operator[]( const int iIdx ) const
{
	return m_vpSpectra[iIdx];
}
ITAHDFTSpectrum* ITAHDFTSpectra::operator[]( const int iIdx )
{
	return m_vpSpectra[iIdx];
}