Convolution of 2D functions - Matlab

61 Views Asked by At

I need to compute the convolution of two functions in $\mathbb{R}^2$ numerically. Basically, I have two functions, $f(x,y)$ and $g(x,y)$, and I want to compute $f\cdot g$. I have a rectangular domain $[a,b]\times[c,d]$. How do I compute it?

I have tried with Matlab Function conv2 but maybe I have not understood how it works if it fits the continuous case...any ideas? Thank you very much in advance.

Here is my code. I don't know if it is correct, in particular I don't know how to check the result. Am I computing in the right way the value of conv2 over the discretization grid?

%choose a rectangular domain an Discretization steps dx for variable x and dy for variable y dx=0.5; dy=0.1; x=(-15:dx:15); y=(-10:dy:10);

Nx=length(x); Ny=length(y);

%my function g is g(x,y):= (1/(1+x^2))y %compute the value of g in the grid g=zeros(Nx,Ny); for i=1:Nx for j=1:Ny g(i,j)=(1/(1+x(i)^2))(y(j)); end end

%compute f

y0=2.5; sigmay=sqrt(0.1); sigmax=sqrt(2); for i=1:Nx for j=1:Ny f(i,j)=(1/(2pisigmaxsigmay))exp(-x(i)^2/(2sigmax^2))(exp(-(y(j)+y0)^2/(2sigmay^2))+exp(-(y(j)-y0)^2/(2sigmay^2))); end end

%compute the 2D convolution. %I want to know the value of g*f on the points of my 2d grid

%Is it correct? At the end, do I have to multiply conv_eval for dx and dy?

conv_eval=conv2(g,f,'same');

1

There are 1 best solutions below

1
On

Thank you very much for your reply. Here is my code. I don't know if it is correct, in particular I don't know how to check the result. Am I computing in the right way the value of conv2 over the discretization grid?

%choose a rectangular domain an Discretization steps dx for variable x and dy for variable y dx=0.5; dy=0.1; x=(-15:dx:15); y=(-10:dy:10);

Nx=length(x); Ny=length(y);

%my function g is g(x,y):= (1/(1+x^2))y %compute the value of g in the grid g=zeros(Nx,Ny); for i=1:Nx for j=1:Ny g(i,j)=(1/(1+x(i)^2))(y(j)); end end

%compute f

y0=2.5; sigmay=sqrt(0.1); sigmax=sqrt(2); for i=1:Nx for j=1:Ny f(i,j)=(1/(2pisigmaxsigmay))exp(-x(i)^2/(2sigmax^2))(exp(-(y(j)+y0)^2/(2sigmay^2))+exp(-(y(j)-y0)^2/(2sigmay^2))); end end

%compute the 2D convolution. %I want to know the value of g*f on the points of my 2d grid

%Is it correct? At the end, do I have to multiply conv_eval for dx and dy?

conv_eval=conv2(g,f,'same');