Audacity
3.2.0
src
import
SpecPowerMeter.cpp
Go to the documentation of this file.
1
/**********************************************************************
2
3
Audacity: A Digital Audio Editor
4
5
SpecPowerCalculation.cpp
6
7
Philipp Sibler
8
9
******************************************************************/
/*******************************************************************/
18
19
#include "
SpecPowerMeter.h
"
20
21
#include <cmath>
22
#include <cstdlib>
23
#include <wx/defs.h>
24
25
#include "
FFT.h
"
26
27
SpecPowerCalculation::SpecPowerCalculation
(
size_t
sigLen)
28
: mSigLen(sigLen)
29
, mSigI{ sigLen, true }
30
, mSigFR{ sigLen }
31
, mSigFI{ sigLen }
32
{
33
}
34
35
SpecPowerCalculation::~SpecPowerCalculation
()
36
{
37
}
38
39
float
SpecPowerCalculation::CalcPower
(
float
* sig,
float
fc,
float
bw)
40
{
41
float
pwr;
42
int
loBin, hiBin;
43
44
// Given the bandwidth bw, get the boundary bin numbers
45
loBin =
Freq2Bin
(fc - (bw / 2.0f));
46
hiBin =
Freq2Bin
(fc + (bw / 2.0f));
47
if
(loBin == hiBin)
48
{
49
hiBin = loBin + 1;
50
}
51
52
// Calc the FFT
53
FFT
(
mSigLen
, 0, sig,
mSigI
.get(),
mSigFR
.get(),
mSigFI
.get());
54
55
// Calc the in-band power
56
pwr =
CalcBinPower
(
mSigFR
.get(),
mSigFI
.get(), loBin, hiBin);
57
58
return
pwr;
59
}
60
61
float
SpecPowerCalculation::CalcBinPower
(
float
* sig_f_r,
float
* sig_f_i,
int
loBin,
int
hiBin)
62
{
63
float
pwr = 0.0f;
64
65
for
(
int
n = loBin; n < hiBin; n++)
66
{
67
pwr += ((sig_f_r[n]*sig_f_r[n])+(sig_f_i[n]*sig_f_i[n]));
68
}
69
70
return
pwr;
71
}
72
73
int
SpecPowerCalculation::Freq2Bin
(
float
fc)
74
{
75
int
bin;
76
77
// There is no round() in (older) MSVSs ...
78
bin = floor((
double
)fc *
mSigLen
);
79
bin %= (int)
mSigLen
;
80
81
return
bin;
82
}
83
FFT
void FFT(size_t NumSamples, bool InverseTransform, const float *RealIn, const float *ImagIn, float *RealOut, float *ImagOut)
Definition:
FFT.cpp:129
FFT.h
SpecPowerMeter.h
SpecPowerCalculation::CalcBinPower
float CalcBinPower(float *sig_f_r, float *sig_f_i, int loBin, int hiBin)
Definition:
SpecPowerMeter.cpp:61
SpecPowerCalculation::~SpecPowerCalculation
~SpecPowerCalculation()
Definition:
SpecPowerMeter.cpp:35
SpecPowerCalculation::mSigLen
const size_t mSigLen
Definition:
SpecPowerMeter.h:19
SpecPowerCalculation::mSigFI
Floats mSigFI
Definition:
SpecPowerMeter.h:23
SpecPowerCalculation::mSigI
Floats mSigI
Definition:
SpecPowerMeter.h:21
SpecPowerCalculation::Freq2Bin
int Freq2Bin(float fc)
Definition:
SpecPowerMeter.cpp:73
SpecPowerCalculation::CalcPower
float CalcPower(float *sig, float fc, float bw)
Definition:
SpecPowerMeter.cpp:39
SpecPowerCalculation::SpecPowerCalculation
SpecPowerCalculation(size_t sigLen)
Definition:
SpecPowerMeter.cpp:27
SpecPowerCalculation::mSigFR
Floats mSigFR
Definition:
SpecPowerMeter.h:22
Generated by
1.9.3