I'm trying to convolve two signals $x(n)$ and $h(n)$ in C by using the FFTW library's functions to perform a Fourier transform on each, multiply the appropriate complex components together, and take the IFFT of the resultant product. Unfortunately, my IFFT'd result isn't correct, and I'm not sure what I'm doing incorrectly.
I'm testing using a modified version of code I found online (.c and Makefile here), which populates two arrays with random numbers between 0 and 1. My process is as follows:
x(audio signal) and
h(impulse response) of lengths
- Populate each buffer with random numbers between 0 and 1.
- Create two
H, each of length
nOut = n + nIR - 1
- Create and then execute two real-to-complex FFT plans, one for each of the signals, both using
plan_forward = fftw_plan_dft_r2c_1d ( nOut, x, X, FFTW_ESTIMATE );and
plan_forwardIR = fftw_plan_dft_r2c_1d (nOut, h, H, FFTW_ESTIMATE);
- Create complex array
fftMultito hold the frequency products of the two arrays. Its size is
nc = nOut / 2 + 1because FFTW doesn't store the redundant half of the FFT
for ( i = 0; i < nc; i++ )and perform complex multiplication, i.e.
fftMulti[i] = X[i] * H[i] - X[i] * H[i];for the real component and
fftMulti[i] = X[i] * H[i] + X[i] * H[i];for the imaginary component
nOut. This array will hold the IFFT of
- Create and execute a complex-to-real FFT plan of size
plan_backwardConv = fftw_plan_dft_c2r_1d(nOut, fftMulti, convolvedSig, FFTW_ESTIMATE);
convolvedSigby dividing each element by
The values in
convolvedSig are not correct (they are way too high), but I'm not sure what I'm doing wrong. I also created arrays and complex-to-real plans for my unmodified signals
H (also of size
nOut), and those IFFTs worked just fine (i.e. values were exactly the same as before I performed FFTs on them).
Using my process and/or my code, can someone please help me identify what I'm doing incorrectly?