From: Nick on
matrix A [3 0 0 0]
matrix B [1 0 3 0]

Ive been trying to have a patch image of A passed through B and
generate a peak when the two 3's match each other.

Ive been told its a convolution which broken down is

A1=fft(A);
B1=fft(B);

A2=real(A1);
B2=real(B1);

R=A2.*B2;
IR=ifft(R);

I dont really understand it and cant get anything resembling a bright
peak in teh correct place other than by chance

any help would be much appreciated

Nick
From: ysriraja on
Try xcorr.
xcorr2 for 2D cases
or normxcorr2.

Raja


Nick wrote:
> matrix A [3 0 0 0]
> matrix B [1 0 3 0]
>
> Ive been trying to have a patch image of A passed through B and
> generate a peak when the two 3's match each other.
>
> Ive been told its a convolution which broken down is
>
> A1=fft(A);
> B1=fft(B);
>
> A2=real(A1);
> B2=real(B1);
>
> R=A2.*B2;
> IR=ifft(R);
>
> I dont really understand it and cant get anything resembling a bright
> peak in teh correct place other than by chance
>
> any help would be much appreciated
>
> Nick

From: Matthew Crema on
Nick wrote:
> Ive been told its a convolution
What you're describing sounds more like cross-correlation
'help xcorr' or 'help xcorr2'

> which broken down is
> A1=fft(A);
> B1=fft(B);
>
> A2=real(A1);
> B2=real(B1);
You should not be taking the real part here. Convolution is equivalent
to multiplying the Fourier Transforms, and then taking the inverse
Fourier Transform of the result. It is incorrect to take the real part.

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/conv.html

> R=A2.*B2;
> IR=ifft(R);
It is good practice to take the real part here. It is not a problem in
your case, but IR can have a small imaginary part (very close to zero)
due to round-off error.

However, why not just use "conv" which should give you the same result
(with some extra zeros)?

IR1=conv(A,B)
IR2=real(ifft(fft(A).*fft(B)))

-Matt
From: Dave Robinson on
Matthew Crema wrote:
>
>
> Nick wrote:
>> Ive been told its a convolution
> What you're describing sounds more like cross-correlation
> 'help xcorr' or 'help xcorr2'
>
>> which broken down is
>> A1=fft(A);
>> B1=fft(B);
>>
>> A2=real(A1);
>> B2=real(B1);
> You should not be taking the real part here. Convolution is
> equivalent
> to multiplying the Fourier Transforms, and then taking the inverse
> Fourier Transform of the result. It is incorrect to take the real
> part.
>
> <http://www.mathworks.com/access/helpdesk/help/techdoc/ref/conv.html>
>
>> R=A2.*B2;
>> IR=ifft(R);
> It is good practice to take the real part here. It is not a
> problem in
> your case, but IR can have a small imaginary part (very close to
> zero)
> due to round-off error.
>
> However, why not just use "conv" which should give you the same
> result
> (with some extra zeros)?
>
> IR1=conv(A,B)
> IR2=real(ifft(fft(A).*fft(B)))
>
> -Matt
>
If you use Matt's second equation for IR2, you need to be careful of
a nasty problem that can catch the unwary user out. You can get what
is known as overlap error, which distorts the output convolution.

It is wise to embed both A and B into zero filled buffers of size

length(A) + length(B) - 1

prior to doing the transform stuff, this way you can guarantee that
the distortion doesn't occur.

Regards

Dave Robinson
From: nick on
thanks, i'll give all this a try.

only thing is i'm eventually working up to 3d ie spaial and temporal.


But i'll give ur suggestions a go first

cheers

Nick

Dave Robinson wrote:
>
>
> Matthew Crema wrote:
>>
>>
>> Nick wrote:
>>> Ive been told its a convolution
>> What you're describing sounds more like cross-correlation
>> 'help xcorr' or 'help xcorr2'
>>
>>> which broken down is
>>> A1=fft(A);
>>> B1=fft(B);
>>>
>>> A2=real(A1);
>>> B2=real(B1);
>> You should not be taking the real part here. Convolution is
>> equivalent
>> to multiplying the Fourier Transforms, and then taking the
> inverse
>> Fourier Transform of the result. It is incorrect to take the
> real
>> part.
>>
>> <http://www.mathworks.com/access/helpdesk/help/techdoc/ref/conv.html>
>>
>>> R=A2.*B2;
>>> IR=ifft(R);
>> It is good practice to take the real part here. It is not a
>> problem in
>> your case, but IR can have a small imaginary part (very close
to
>> zero)
>> due to round-off error.
>>
>> However, why not just use "conv" which should give you the same
>> result
>> (with some extra zeros)?
>>
>> IR1=conv(A,B)
>> IR2=real(ifft(fft(A).*fft(B)))
>>
>> -Matt
>>
> If you use Matt's second equation for IR2, you need to be careful
> of
> a nasty problem that can catch the unwary user out. You can get
> what
> is known as overlap error, which distorts the output convolution.
>
> It is wise to embed both A and B into zero filled buffers of size
>
> length(A) + length(B) - 1
>
> prior to doing the transform stuff, this way you can guarantee that
> the distortion doesn't occur.
>
> Regards
>
> Dave Robinson