关于 c :Can\’t apply FFT on a simple cosine wave
Can't apply FFT on a simple cosine wave
我已经阅读了很多关于离散傅里叶变换的文章,但我发现自己很难将它应用于简单的余弦波。我正在使用 Kiss_fft 库来计算一组数据的 DFT,并使用位图库来可视化结果。
这是 C 代码:
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 |
#define FIXED_POINT 32 #include"kiss_fft.h" int main() { const int width = 512; const int height = 512; const int align_center = 256; const int fft_siz = width; const int is_inverse = 0; Bitmap bmp_t("fft_time.bmp", width, height); Bitmap bmp_f("fft_frq.bmp", width, height); kiss_fft_cpx* in = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * fft_siz); kiss_fft_cpx* out= (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * fft_siz); kiss_fft_cfg cfg = kiss_fft_alloc(fft_siz, is_inverse, NULL, NULL); // initialize input data for(int i = 0; i < fft_siz; i++) { // I think I shouldn't add align_center to in[i].r // but should wait till line (1*) in[i].r = align_center + 128 * cos(0.128f * i); in[i].i = 0; // line (1*) bmp_t.setPixel(i, in[i].r, 255, 255, 255); } kiss_fft(cfg, in, out); // visualize output for(int i = 1; i < fft_siz; i ++) bmp_f.setPixel(out[i].r, out[i].i, 255, 255, 255); free(in); free(out); free(cfg); kiss_fft_cleanup(); bmp_t.write(); bmp_f.write(); return 0; } |
这是输入:

以及我得到的输出:

结果感觉完全不对。我做错了什么?
相关讨论
- 输入和输出在我看来是空白的
- 不要在 C 中强制转换 malloc() 的返回值。
仅绘制 FFT 输出的实部并不是很有意义 - 改为绘制幅度 (sqrt(re*re+im*im))。更好的是,以 dB (10*log10(re*re+im*im))
为单位绘制对数幅度
相关讨论
- 行。输出 sqrt(re*re+im*im) 为白色,10*log10(re*re+im*im) 为红色。我用谷歌搜索了 fft 的 cos,它似乎是正确的。
- 是的 - 另请注意,如果您在 FFT 之前添加窗口函数,您将获得更尖锐的峰值。您还应该只绘制前 N/2 个点。
THE END
二维码