After doing FFT and IFFT I can hear only noise in my headphones... Here is the code:
做了FFT和IFFT之后,我只能听到耳机里的噪音……这是代码:
double* spectrum = new double[n];
fftw_plan plan;
plan = fftw_plan_r2r_1d(n, data, spectrum, FFTW_REDFT10, FFTW_ESTIMATE);
fftw_execute(plan);
fftw_destroy_plan(plan);
plan = fftw_plan_r2r_1d(n, spectrum, data, FFTW_REDFT01, FFTW_ESTIMATE);
fftw_execute(plan);
fftw_destroy_plan(plan);
Maybe I've chosen wrong FFT type?
P.S. data is the initial signal
也许我选错了FFT类型?P.S.数据是初始信号
UPDATE
Ok, so now the code is
现在代码是
fftw_complex* spectrum = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * n);
fftw_plan plan;
plan = fftw_plan_dft_r2c_1d(n, data, spectrum, FFTW_ESTIMATE);
fftw_execute(plan);
fftw_destroy_plan(plan);
plan = fftw_plan_dft_c2r_1d(n, spectrum, data, FFTW_ESTIMATE);
fftw_execute(plan);
fftw_destroy_plan(plan);
The problem remains the same, my data array is corrupted.
问题仍然是一样的,我的数据数组被损坏了。
UPDATE #2
So, the problem is in my transform size and normalizing. If I user real-to-real FFTW_REDFT10 and FFTW_REDFT01 transforms which transform sizes i need to use? 2*n? Or something else? And then I need to normalize my output signal by dividing each element by 2*n?
Thank to all for replying.
问题在于变换的大小和规范化。如果我使用真实到真实的FFTW_REDFT10和FFTW_REDFT01转换,需要使用哪些转换大小?2 * n ?还是别的?然后我需要用每个元素除以2*n来规范输出信号?谢谢大家的回复。
UPDATE #3
Thanks to all for replying again. I've solved the problem with your help. Here is the working code:
谢谢大家的回复。我在你的帮助下解决了这个问题。以下是工作代码:
// FFT
fftw_complex* spectrum = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * n);
fftw_plan plan;
plan = fftw_plan_dft_r2c_1d(n, data, spectrum, FFTW_ESTIMATE);
fftw_execute(plan);
fftw_destroy_plan(plan);
// some filtering here
// IFFT
plan = fftw_plan_dft_c2r_1d(n, spectrum, data, FFTW_ESTIMATE);
fftw_execute(plan);
fftw_destroy_plan(plan);
// normalizing
for (int i = 0; i < n; i++) {
data[i] = data[i] / n;
}
2 个解决方案
#1
5
I don't see where you are normalizing your output. You must divide your output values by the number of elements in the data array to normalize the data back to original range of values.
我看不出你把输出常态化到哪里去了。您必须将输出值除以数据数组中的元素数量,以便将数据规范化回原始值范围。
See the FFTW Manual 4.8.2, last paragraph (I have V3.2 manual).
见FFTW手册4.8.2,最后一段(我有V3.2手册)。
#2
2
You're performing a real-to-real FFT (actually, FFTW is computing a DCT, or discrete cosine transform, internally, but it results in the same thing). Be careful about the number of points that are computed in the output spectrum array. A real-to-real transform only has n/2+1 actual values placed into the array.
您正在执行一个真实到真实的FFT(实际上,FFTW是在内部计算DCT或离散余弦变换,但结果是一样的)。注意在输出频谱数组中计算的点的数量。实-实转换只在数组中包含n/2+1的实际值。
If, as you indicated, you compute a real-to-complex transform, you will generate both sides of the spectrum (they are complex conjugates of each other) but your output array will need to be resized to accommodate complex values plus the DC result.
如您所示,如果您计算一个实数-复数变换,您将生成频谱的两边(它们是彼此的复共轭),但是您的输出数组需要调整大小,以适应复值和直流结果。
#1
5
I don't see where you are normalizing your output. You must divide your output values by the number of elements in the data array to normalize the data back to original range of values.
我看不出你把输出常态化到哪里去了。您必须将输出值除以数据数组中的元素数量,以便将数据规范化回原始值范围。
See the FFTW Manual 4.8.2, last paragraph (I have V3.2 manual).
见FFTW手册4.8.2,最后一段(我有V3.2手册)。
#2
2
You're performing a real-to-real FFT (actually, FFTW is computing a DCT, or discrete cosine transform, internally, but it results in the same thing). Be careful about the number of points that are computed in the output spectrum array. A real-to-real transform only has n/2+1 actual values placed into the array.
您正在执行一个真实到真实的FFT(实际上,FFTW是在内部计算DCT或离散余弦变换,但结果是一样的)。注意在输出频谱数组中计算的点的数量。实-实转换只在数组中包含n/2+1的实际值。
If, as you indicated, you compute a real-to-complex transform, you will generate both sides of the spectrum (they are complex conjugates of each other) but your output array will need to be resized to accommodate complex values plus the DC result.
如您所示,如果您计算一个实数-复数变换,您将生成频谱的两边(它们是彼此的复共轭),但是您的输出数组需要调整大小,以适应复值和直流结果。