FFT工作正常,但是当我想采用IFFT时,我总是从结果中看到相同的图形。无论原始信号如何,结果都很复杂,图形始终相同。
实部图中的是-sin,其周期=帧大小
在虚部中,它是具有相同周期的-cos
哪里可能有问题?
原始信号:
IFFT实际值(在图片上仅为帧的一半):
我使用的算法FFT。
double** FFT(double** f, int s, bool inverse) {
if (s == 1) return f;
int sH = s / 2;
double** fOdd = new double*[sH];
double** fEven = new double*[sH];
for (int i = 0; i < sH; i++) {
int j = 2 * i;
fOdd[i] = f[j];
fEven[i] = f[j + 1];
}
double** sOdd = FFT(fOdd, sH, inverse);
double** sEven = FFT(fEven, sH, inverse);
double**spectr = new double*[s];
double arg = inverse ? DoublePI / s : -DoublePI / s;
double*oBase = new double[2]{ cos(arg),sin(arg) };
double*o = new double[2]{ 1,0 };
for (int i = 0; i < sH; i++) {
double* sO1 = Mul(o, sOdd[i]);
spectr[i] = Sum(sEven[i], sO1);
spectr[i + sH] = Dif(sEven[i], sO1);
o = Mul(o, oBase);
}
return spectr;
}
答案 0 :(得分:2)
“蝴蝶”部分错误地应用了系数:
for (int i = 0; i < sH; i++) {
double* sO1 = sOdd[i];
double* sE1 = Mul(o, sEven[i]);
spectr[i] = Sum(sO1, sE1);
spectr[i + sH] = Dif(sO1, sE1);
o = Mul(o, oBase);
}
旁注:
我保留了你的记谱法但却令人困惑:
fOdd
有索引0,2,4,6 ......所以它应该是fEven
fEven
有索引1,3,5,7 ......所以它应该是fOdd
真的sOdd
应为sLower
而sEven
应为sUpper
,因为它们分别对应于频谱的0:s/2
和s/2:s-1
元素:
sLower = FFT(fEven, sH, inverse); // fEven is 0, 2, 4, ...
sUpper = FFT(fOdd, sH, inverse); // fOdd is 1, 3, 5, ...
然后蝴蝶变成:
for (int i = 0; i < sH; i++) {
double* sL1 = sLower[i];
double* sU1 = Mul(o, sUpper[i]);
spectr[i] = Sum(sL1, sU1);
spectr[i + sH] = Dif(sL1, sU1);
o = Mul(o, oBase);
}
如果这样写,则比较pseudocode example on wikipedia更容易。
@Dai是正确的,你会泄漏大量的内存
答案 1 :(得分:1)
关于内存,您可以使用std::vector
来封装动态分配的数组,并确保在执行离开范围时它们被解除分配。您可以使用unique_ptr<double[]>
,但性能提升不值得IMO,您将失去at()
方法的安全性。
(根据@ Robb的回答)
其他一些提示:
f
&#34;和&#34; s
&#34;使你的程序更难阅读和维护。size_t
表示索引,而不是int
const
预防性地防止错误,以防止意外突变只读数据。像这样:
#include <vector>
using namespace std;
vector<double> fastFourierTransform(const vector<double> signal, const bool inverse) {
if( signal.size() < 2 ) return signal;
const size_t half = signal.size() / 2;
vector<double> lower; lower.reserve( half );
vector<double> upper; upper.reserve( half );
bool isEven = true;
for( size_t i = 0; i < signal.size(); i++ ) {
if( isEven ) lower.push_back( signal.at( i ) );
else upper.push_back( signal.at( i ) );
isEven = !isEven;
}
vector<double> lowerFft = fastFourierTransform( lower, inverse );
vector<double> upperFft = fastFourierTransform( upper, inverse );
vector<double> result;
result.reserve( signal.size() );
double arg = ( inverse ? 1 : -1 ) * ( DoublePI / signal.size() );
// Ideally these should be local `double` values passed directly into `Mul`.
unique_ptr<double[]> oBase = make_unique<double[]>( 2 );
oBase[0] = cos(arg);
oBase[1] = sin(arg);
unique_ptr<double[]> o = make_unique<double[]>( 2 );
o[0] = 0;
o[1] = 0;
for( size_t i = 0; i < half; i++ ) {
double* lower1 = lower.at( i );
double* upper1 = Mul( o, upper.at( i ) );
result.at( i ) = Sum( lower1, upper1 );
result.at( i + half ) = Dif( lower1, upper1 );
o = Mul( o, oBase );
}
// My knowledge of move-semantics of STL containers is a bit rusty - so there's probably a better way to return the output 'result' vector.
return result;
}