我目前正在尝试写一些傅里叶变换算法。我开始用一个简单的DFT算法的数学定义描述:离散傅里叶变换
public class DFT {
public static Complex[] Transform(Complex[] input) {
int N = input.Length;
Complex[] output = new Complex[N];
double arg = -2.0 * Math.PI/(double)N;
for (int n = 0; n < N; n++) {
output[n] = new Complex();
for (int k = 0; k < N; k++)
output[n] += input[k] * Complex.Polar(1, arg * (double)n * (double)k);
}
return output;
}
}
所以我测试了这个算法用下面的代码:
private int samplingFrequency = 120;
private int numberValues = 240;
private void doCalc(object sender, EventArgs e) {
Complex[] input = new Complex[numberValues];
Complex[] output = new Complex[numberValues];
double t = 0;
double y = 0;
for (int i = 0; i < numberValues; i++) {
t = (double)i/(double)samplingFrequency;
y = Math.Sin(2 * Math.PI * t);
input[i] = new Complex(y, 0);
}
output = DFT.Transform(input);
printFunc(input);
printAbs(output);
}
改造工作正常,但只有当numberValues是多采样频率的数量(在这种情况下:120,240,360,...)。这就是我的结果为240倍的值:
http://s1.directupload.net/images/110928/n3m8hqg6.jpg
转型刚刚工作的罚款。
如果我试图计算280个值我得到这样的结果:
http://s7.directupload.net/images/110928/qizoiqbt.jpg
为什么我得到一个不正确的结果,如果我改变我的计算值是多少? 我不确定这里的问题是否与我的代码有关,或者是对DFT数学定义的误解。无论哪种方式,任何人都可以帮我解决我的问题吗?谢谢。
你能告诉我为什么你的变换函数的输入是一个复杂的数组?当然,如果你从一个标准的声波(时域)转换,它会是一个单一的双数组? –
好吧,我明白了 - 虚数部分设置为0,所以它基本上等于如果它只是一个双倍。顺便说一句,在你的代码中,'Complex.Polar'应该是'Complex.FromPolarCoordinates'。 –
你对地块有什么用处? – GorillaApe