ITAHDFTSpectrum.cpp 12.5 KB
Newer Older
Jonas Stienen's avatar
Jonas Stienen committed
1
2
3
4
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
   +-----------------------------------------------------------------------+
   |                                                                       |
   |  ITAToolkit                                                           |
   |                                                                       |
   |  (c) Copyright Institut für technische Akustik (ITA)                  |
   |      RWTH Aachen University of Technology                             |
   |                                                                       |
   +-----------------------------------------------------------------------+
   |                                                                       |
   |  File:     ITAHDFTSpectrum.h                                          |
   |  Purpose:  Klasse die symmetrische Half-size DFT-Spektren realisiert  |
   |  Authors:  Frank Wefers                                               |
   |                                                                       |
   +-----------------------------------------------------------------------+
*/
// $Id: ITAHDFTSpectrum.cpp 3913 2015-01-06 12:42:15Z stienen $

#include <ITAHDFTSpectrum.h>

#include <ITAFastMath.h>
#include <stdio.h>
#include <ITAException.h>
#include <ITAStringUtils.h>
#include <cmath>
#include <complex>
#include <math.h>
#include <ITANumericUtils.h>
#include "ITASampleBuffer.h"

ITAHDFTSpectrum::ITAHDFTSpectrum() :
m_iSize(0), m_iDFTSize(0), m_dSamplerate(0), m_pfData(NULL)
{
}

ITAHDFTSpectrum::ITAHDFTSpectrum(int iDFTSize, bool bZeroinit) :
m_iSize(0), m_iDFTSize(0), m_dSamplerate(0), m_pfData(NULL)
{
	init(iDFTSize, bZeroinit);
}

ITAHDFTSpectrum::ITAHDFTSpectrum(double dSampleRate, int iDFTSize, bool bZeroInit)
: m_iSize(-1), m_iDFTSize( iDFTSize ), m_dSamplerate( dSampleRate ), m_pfData(NULL)
{
	setSamplerate( dSampleRate );
	init( iDFTSize, bZeroInit ); // sets size and resolution
}

ITAHDFTSpectrum::ITAHDFTSpectrum(const ITAHDFTSpectrum* pSource) :
m_iSize(0), m_iDFTSize(0), m_dSamplerate(0), m_pfData(NULL)
{
	init(pSource->getDFTSize());
	setSamplerate(pSource->getSamplerate());
	copyFrom( *pSource );
}

ITAHDFTSpectrum::ITAHDFTSpectrum(const ITAHDFTSpectrum& oSource) :
m_iSize(0), m_iDFTSize(0), m_dSamplerate(0), m_pfData(NULL)
{
	init(oSource.getDFTSize());
	setSamplerate(oSource.getSamplerate());
	copyFrom( oSource );
}

ITAHDFTSpectrum::~ITAHDFTSpectrum() {
	fm_free(m_pfData);
}

ITAHDFTSpectrum& ITAHDFTSpectrum::init(int iDFTSize, bool bZeroinit) {
	// Bereits allozierten Speicher freigeben
	if (m_pfData) {
		fm_free(m_pfData);
		m_pfData = NULL;
	}

	m_iDFTSize = iDFTSize;
	m_iSize = (int) floor((float) m_iDFTSize / 2.0) + 1;

	// +2 wegen 4-Byte Ausrichtung
	if (m_iSize > 0) m_pfData = fm_falloc(m_iSize*2+2, bZeroinit);

	return *this;
}

int ITAHDFTSpectrum::getSize() const {
	return m_iSize;
}

int ITAHDFTSpectrum::getDFTSize() const {
	return m_iDFTSize;
}

double ITAHDFTSpectrum::getSamplerate() const {
	return m_dSamplerate;
}

void ITAHDFTSpectrum::setSamplerate(double dSamplerate) {
	m_dSamplerate = dSamplerate;

}
		
double ITAHDFTSpectrum::getFrequencyResolution() const {
    return (m_iDFTSize == 0 ? 0 : (m_dSamplerate / (double) m_iDFTSize));
}

float* ITAHDFTSpectrum::data() const {
	return m_pfData;
}

void ITAHDFTSpectrum::SetUnity()
{
	for( int i=0; i < m_iSize; i++ )
	{
		int iRe = 2 * i;
		int iIm = iRe + 1;
		m_pfData[iRe] = 1.0f;
		m_pfData[iIm] = 0.0f;
	}

	return;
}

void ITAHDFTSpectrum::SetZero()
{
	for( int i=0; i < m_iSize; i++ )
	{
		int iRe = 2 * i;
		int iIm = iRe + 1;
		m_pfData[iRe] = 0.0f;
		m_pfData[iIm] = 0.0f;
	}

	return;
}

void ITAHDFTSpectrum::add(float fReal, float fImag) {
	for (int i=0; i<2*m_iSize; i+=2) {
		m_pfData[i] += fReal;
		m_pfData[i+1] += fImag;
	}
}

void ITAHDFTSpectrum::setCoeffRI(int iIndex, float fReal, float fImag) {
	setCoeffsRI(iIndex, 1, fReal, fImag);
}

void ITAHDFTSpectrum::setCoeffsRI(float fReal, float fImag) {
	setCoeffsRI(0, m_iSize, fReal, fImag);
}

void ITAHDFTSpectrum::setCoeffsRI(int iOffset, int iCount, float fReal, float fImag) {
	if ((iOffset < 0) || (iOffset >= m_iSize) || (iCount < 0) || (iOffset + iCount > m_iSize))
		ITA_EXCEPT1(INVALID_PARAMETER, "Index/count out of bounds");

	for (int i=iOffset; i<iOffset+iCount; i++) {
		m_pfData[2*i] = fReal;
		m_pfData[2*i+1] = fImag;
	}
}

void ITAHDFTSpectrum::setCoeffMP(int iIndex, float fMagnitude, float fPhase) {
	setCoeffsMP(iIndex, 1, fMagnitude, fPhase);
}

void ITAHDFTSpectrum::setCoeffsMP(float fMagnitude, float fPhase) {
	setCoeffsMP(0, m_iSize, fMagnitude, fPhase);
}

void ITAHDFTSpectrum::setCoeffsMP(int iOffset, int iCount, float fMagnitude, float fPhase) {
	if ((iOffset < 0) || (iOffset >= m_iSize) || (iCount < 0) || (iOffset + iCount > m_iSize))
		ITA_EXCEPT1(INVALID_PARAMETER, "Index/count out of bounds");

	setCoeffsRI(fMagnitude * cos(fPhase), fMagnitude * sin(fPhase));
}

void ITAHDFTSpectrum::setMagnitudePreservePhase(int iIndex, float fMagnitude) {
	setMagnitudesPreservePhases(iIndex, 1, fMagnitude);
}

void ITAHDFTSpectrum::setMagnitudesPreservePhases(float fMagnitude) {
	setMagnitudesPreservePhases(0, m_iSize, fMagnitude);
}

void ITAHDFTSpectrum::setMagnitudesPreservePhases(int iOffset, int iCount, float fMagnitude) {
	if ((iOffset < 0) || (iOffset >= m_iSize) || (iCount < 0) || (iOffset + iCount > m_iSize))
		ITA_EXCEPT1(INVALID_PARAMETER, "Index/count out of bounds");	

	for (int i=iOffset; i<iOffset+iCount; i++)
		csabsparg(m_pfData[2*i], m_pfData[2*i+1], fMagnitude, m_pfData[2*i], m_pfData[2*i+1]);
}

void ITAHDFTSpectrum::setPhasePreserveMagnitude(int iIndex, float fPhase) {
	setPhasesPreserveMagnitudes(iIndex, 1, fPhase);
}

void ITAHDFTSpectrum::setPhasesPreserveMagnitudes(float fPhase) {
	setPhasesPreserveMagnitudes(0, m_iSize, fPhase);
}

void ITAHDFTSpectrum::setPhasesPreserveMagnitudes(int iOffset, int iCount, float fPhase) {
	if ((iOffset < 0) || (iOffset >= m_iSize) || (iCount < 0) || (iOffset + iCount > m_iSize))
		ITA_EXCEPT1(INVALID_PARAMETER, "Index/count out of bounds");	

	for (int i=iOffset; i<iOffset+iCount; i++)
		csargpabs(m_pfData[2*i], m_pfData[2*i+1], fPhase, m_pfData[2*i], m_pfData[2*i+1]);
}

void ITAHDFTSpectrum::sub(float fReal, float fImag) {
	for (int i=0; i<2*m_iSize; i+=2) {
		m_pfData[i] -= fReal;
		m_pfData[i+1] -= fImag;
	}
}

void ITAHDFTSpectrum::add(const ITAHDFTSpectrum& s) {
	if (s.getSize() != m_iSize)
		ITA_EXCEPT1(INVALID_PARAMETER, "Spectra must have the same size");

	fm_add(m_pfData, s.data(), 2*m_iSize);
}

void ITAHDFTSpectrum::add( const ITAHDFTSpectrum* pSource )
{
	if( pSource->getSize() != m_iSize )
		ITA_EXCEPT1( INVALID_PARAMETER, "Source spectrum must be of same size" );

	fm_add( m_pfData, pSource->data(), 2*m_iSize );

	return;
}

void ITAHDFTSpectrum::sub(const ITAHDFTSpectrum& s) {
	if (s.getSize() != m_iSize)
		ITA_EXCEPT1(INVALID_PARAMETER, "Spectra must have the same size");

	fm_sub(m_pfData, s.data(), 2*m_iSize);
}

void ITAHDFTSpectrum::sub( const ITAHDFTSpectrum* pSource )
{
	if( pSource->getSize() != m_iSize )
		ITA_EXCEPT1( INVALID_PARAMETER, "Source spectrum must be of same size" );

	fm_sub( m_pfData, pSource->data(), 2*m_iSize );

	return;
}

void ITAHDFTSpectrum::mul(const ITAHDFTSpectrum& s )
{
	if (s.getSize() != m_iSize)
		ITA_EXCEPT1(INVALID_PARAMETER, "Spectra must have the same size");

	fm_cmul(m_pfData, s.data(), m_iSize);

	return;
}

void ITAHDFTSpectrum::mul( const ITAHDFTSpectrum* pSource )
{
	if( pSource->getSize() != m_iSize )
		ITA_EXCEPT1(INVALID_PARAMETER, "Spectra must have the same size");

	const float* fS( pSource->data() );
	float* fD( m_pfData );

	float fR1, fR2, fI1, fI2;
	for( int i=0; i<m_iSize; i++ )
	{
		const int idxRe = 2*i;
		const int idxIm = idxRe + 1;

		// numerically stable
		fR1 = fD[idxRe] * fS[idxRe]; 
		fR2 = fD[idxIm] * fS[idxIm];
		fI1 = fD[idxRe] * fS[idxIm];
		fI2 = fS[idxRe] * fD[idxIm];

		// numerically instable, be aware of your data range
		fD[idxRe] = fR1 - fR2;
		fD[idxIm] = fI1 + fI2;
	}

	return;
}

void ITAHDFTSpectrum::mul_conj( const ITAHDFTSpectrum* pSource )
{
	if( pSource->getSize() != m_iSize )
		ITA_EXCEPT1( INVALID_PARAMETER, "Source spectrum must be of same size" );

	const float* fS( pSource->data() );
	float* fD( m_pfData );

	float fR1, fR2, fI1, fI2;
	for( int i=0; i<m_iSize; i++ )
	{
		const int idxRe = 2*i;
		const int idxIm = idxRe + 1;

		// numerically stable
		fR1 = fD[idxRe] * fS[idxRe]; 
		fR2 = fD[idxIm] * fS[idxIm];
		fI1 = fD[idxRe] * fS[idxIm];
		fI2 = fS[idxRe] * fD[idxIm];

		// numerically instable, be aware of your data range
		fD[idxRe] = fR1 + fR2;
		fD[idxIm] = fI2 - fI1;
	}

	return;
}

void ITAHDFTSpectrum::mul( const float fFactor )
{
	fm_mul( m_pfData, fFactor, m_iSize*2 );	
	return;
}

void ITAHDFTSpectrum::div( const ITAHDFTSpectrum& s )
{
	if (s.getSize() != m_iSize)
		ITA_EXCEPT1(INVALID_PARAMETER, "Spectra must have the same size");

	fm_cdiv(m_pfData, s.data(), m_iSize);

	return;
}

void ITAHDFTSpectrum::div( const ITAHDFTSpectrum* pSource )
{
	if( pSource->getSize() != m_iSize )
		ITA_EXCEPT1( INVALID_PARAMETER, "Source spectrum must be of same size" );

	fm_cdiv( m_pfData, pSource->data(), m_iSize );

	return;
}

341
342
343
344
345
346
347
348
349
350
351
352
353
float ITAHDFTSpectrum::calcMagnitude( int iIndex ) const
{
	if( iIndex >= m_iDFTSize )
		ITA_EXCEPT1( INVALID_PARAMETER, "Index out of range" );

	return cabsf( m_pfData[ 2 * iIndex ], m_pfData[ 2 * iIndex + 1 ] );
}

void ITAHDFTSpectrum::calcMagnitudes( float* pfMagnitudes ) const
{
	if( pfMagnitudes )
		for( int i = 0; i < m_iSize; i++ )
			pfMagnitudes[ i ] = calcMagnitude( i );
Jonas Stienen's avatar
Jonas Stienen committed
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
}

void ITAHDFTSpectrum::calcPhases(float* pfPhases) {
	if (pfPhases)
		for (int i=0; i<m_iSize; i++)
			pfPhases[i] = canglef(m_pfData[2*i], m_pfData[2*i+1]);
}

void ITAHDFTSpectrum::log(bool bComplex) {
	for (int i=0; i<m_iSize; i++){
		// TODO: Check for (magnitude < eps, -HUGE_VAL), negative value on real log
		if (bComplex) {
			//  z = x+i*y returns log(z) = log(abs(z)) + i*atan2(y,x); cp. http://www.mathworks.de/help/techdoc/ref/log.html
			float x = m_pfData[2*i];
			m_pfData[2*i] =  logf(cabsf(x, m_pfData[2*i+1]));
			m_pfData[2*i+1] = atan2f(m_pfData[2*i+1], x);
		} else {
			m_pfData[2*i] =  logf(cabsf(m_pfData[2*i], m_pfData[2*i+1]));
			m_pfData[2*i+1] = 0.0f;
		}
	}
}

void ITAHDFTSpectrum::exp(bool bComplex) {
	for (int i=0; i<m_iSize; i++){
		// TODO: Check for (magnitude > thresh, HUGE_VAL)
		if (bComplex) {
			// z = x+i*y returns e^z = e^x * (cos(y) + i*sin(y)); cp. http://www.mathworks.de/help/techdoc/ref/exp.html
			float expMag = std::exp(m_pfData[2*i]);
			m_pfData[2*i]   = expMag * cosf(m_pfData[2*i+1]);
			m_pfData[2*i+1] = expMag * sinf(m_pfData[2*i+1]);
		} else {
			m_pfData[2*i] = std::exp(m_pfData[2*i]);
			m_pfData[2*i+1] = 0.0f;
		}
	}
}

void ITAHDFTSpectrum::negate() {
	for (int i=0; i<2*m_iSize; i++)
		m_pfData[i] = -m_pfData[i];
}

void ITAHDFTSpectrum::conjugate() {
	for (int i=0; i<m_iSize; i++)
		m_pfData[2*i+1] *= -1;
}

float ITAHDFTSpectrum::findMax() const {
	int iDummy;
	return findMax(iDummy);
}

float ITAHDFTSpectrum::findMax(int& iMaxIndex) const {
	int i=0;
	float x=0;

	for (int j=0; j<m_iSize; j++) {
		float y=cabsf(m_pfData[2*j], m_pfData[2*j+1]);
		if (y > x) {
			x = y;
			i = j;
		}
	}

	iMaxIndex = i;
	return x;
}

void ITAHDFTSpectrum::copyFrom(const ITAHDFTSpectrum& s) {
	if (s.getSize() != m_iSize)
		ITA_EXCEPT1(INVALID_PARAMETER, "Spectra must have the same size");

	fm_copy(m_pfData, s.data(), m_iSize*2);
}

void ITAHDFTSpectrum::copyFrom(const ITAHDFTSpectrum& s, int iOffset, int iCount) {
	if (s.getSize() != m_iSize)
		ITA_EXCEPT1(INVALID_PARAMETER, "Spectra must have the same size");

	// TODO: FastMath? Jetzt gibt es Probleme mit SIMD-Alignment?
	for (int i=iOffset*2; i<(iOffset+iCount)*2; i++)
		m_pfData[i] = s.data()[i];
}

ITAHDFTSpectrum& ITAHDFTSpectrum::operator=(const ITAHDFTSpectrum& rhs) {
	// Selbstzuweisung abfangen
	if (&rhs == this) return *this;

	// Neu allozieren
	init(rhs.getDFTSize(), false);
	setSamplerate(rhs.getSamplerate());

	// Samples kopieren (+2 für 16 Byte alignment)
	fm_copy(m_pfData, rhs.data(), m_iDFTSize*2+2);

	return *this;
}

std::string ITAHDFTSpectrum::toString() {
	std::string s = "(";
	for (int i=0; i<2*m_iSize; i+=2) {
		if (i>0) s += ", ";
		s += FloatToString(m_pfData[i], 3) + (m_pfData[i+1] >= 0 ? "+" : "") + FloatToString(m_pfData[i+1], 3) + "i";
	}
	s += ")";
	return s;
}

Michael Kohnen's avatar
Michael Kohnen committed
463
464
465
466
467
468
469
470
471
472
float ITAHDFTSpectrum::getEnergy() const
{
	float energy=0;
	for (int i = 0; i < m_iSize; i++) {
		float mags = cabsf(m_pfData[2 * i], m_pfData[2 * i + 1]);
		energy += mags*mags;
	}
	return energy/((float)m_iSize);
}

Jonas Stienen's avatar
Jonas Stienen committed
473
474
475
476
477
478
void ITAHDFTSpectrum::copy( const ITAHDFTSpectrum* pSource )
{
	// TODO: FastMath? Jetzt gibt es Probleme mit SIMD-Alignment?
	for( int i=0; i<2*m_iSize; i++)
		m_pfData[i] = pSource->data()[i];
}