Perform Convolution in Frequency Domain Using FFTW? 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:


Create double buffers for x (audio signal) and h (impulse response) of lengths n and nIR respectively.
Populate each buffer with random numbers between 0 and 1.
Create two complex buffers X and 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 nOut elements (i.e. 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 fftMulti to hold the frequency products of the two arrays. Its size is nc = nOut / 2 + 1 because FFTW doesn't store the redundant half of the FFT
Loop for ( i = 0; i &lt; nc; i++ ) and perform complex multiplication, i.e. fftMulti[i][0] = X[i][0] * H[i][0] - X[i][1] * H[i][1]; for the real component and fftMulti[i][1] = X[i][0] * H[i][1] + X[i][1] * H[i][0]; for the imaginary component
Create double array convolvedSig of size nOut. This array will hold the IFFT of fftMulti
Create and execute a complex-to-real FFT plan of size nOut, i.e. plan_backwardConv = fftw_plan_dft_c2r_1d(nOut, fftMulti, convolvedSig, FFTW_ESTIMATE);
Normalize convolvedSig by dividing each element by nOut


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 X and 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?
You need to zero pad x and h. 

nZeros = length(x) + length(h) -1

Example in MATLAB:

clear all; close all; home

xSamples = 10;
hSamples = 7;

x = randn(1,xSamples);

h = randn(1,hSamples);

nZeros = xSamples + hSamples - 1;

X = fft(x,nZeros);
H = fft(h,nZeros);

x_conv_h = ifft(X.*H);


figure(1)
plot(real(x_conv_h))
hold on; grid on
plot(conv(x,h),'.r')
legend('fft based convolution','convolution')