1

I'm implementing an algorithm where I need to compute the linear convolution and cross-correlation between two 1D vectors a and b using the FFT. If the length of a is m and the length of b is n, then the total length of the resulting convolution will be m + n - 1.

However, the algorithm requires the output of the convolution to be the same length as the inputs. Since m = n, the input vectors are the same length.

In Matlab, given that both vectors have been zero-padded to length m + n - 1, the convolution is computed as:

ifft(fft(a).*fft(b))

Alternately, the cross-correlation between the two vectors is computed as:

fftshift(ifft(fft(a).*conj(fft(b))))

The output is of length m + n - 1. I need to zero-pad the vectors to ensure that circular convolution does not occur when using the FFT.

However, I would like the output length to be the same as the length of the inputs. An associated question (also on stackoverflow) shows how the correlation of two images can be trimmed.

How do I trim the 1D output vector so that it is the same length as the input vectors?

Community
  • 1
  • 1
Nicholas Kinar
  • 1,440
  • 5
  • 24
  • 36

2 Answers2

1

In MATLAB, conv(a, b, 'same') returns the central part of the convolution that is the same size as a. For cross correlation, you can use xcorr, which will give you the cross correlation result. I guess you need to crop the result by yourself to get the same size as input.

chaohuang
  • 3,965
  • 4
  • 27
  • 35
  • Thanks, chaohuang. How does `conv(a, b, 'same')` return the central part of the convolution? Which indices are used? In addition, using `xcorr`, how would I appropriately crop the result? Would this be the same central part of the response? – Nicholas Kinar Sep 05 '12 at 15:18
  • You can use help to see how to use `conv` or do some simple tests. Since there is no rule for cropping the convolution/correlation results, choosing the central parts is just one way to do it. You can choose whatever is most suitable for you. – chaohuang Sep 05 '12 at 15:24
  • Rather than using the `conv` function, I would like to do the convolution using the FFT. How does `conv` internally crop the result? Is there maybe a reference book/paper showing how the cropping is done, even if there is no set rule? – Nicholas Kinar Sep 05 '12 at 15:38
  • It is probably best in this situation to use `conv(a, b, 'same')` – Nicholas Kinar Sep 09 '12 at 18:37
0

there are formulas:

c = a conv b;

where, a, b and c are in time domain; After trans to frequency domain, the formula change to

C = A * B;

where, A, B and C are fft result of a, b, and c respectively, and all of them are in frequency domain; so c = ifft(C);

xychen
  • 31
  • 4