Audacity 3.2.0
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
28 : mSigLen(sigLen)
29 , mSigI{ sigLen, true }
30 , mSigFR{ sigLen }
31 , mSigFI{ sigLen }
32{
33}
34
36{
37}
38
39float 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
61float 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
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
void FFT(size_t NumSamples, bool InverseTransform, const float *RealIn, const float *ImagIn, float *RealOut, float *ImagOut)
Definition: FFT.cpp:129
float CalcBinPower(float *sig_f_r, float *sig_f_i, int loBin, int hiBin)
const size_t mSigLen
int Freq2Bin(float fc)
float CalcPower(float *sig, float fc, float bw)
SpecPowerCalculation(size_t sigLen)