Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

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);

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top