I have defined an integral operator hm[func_, m_, n_]
that discretizes a continuous 2D function into its pixel counterpart where Dm[x_,y_,m_,n_]
is the kernel of the operator. This kernel uses Mathematica's 2D UnitBox
function.
Definition of the operator:
Dm[x_,y_,m_,n_] := 1/(PixelWidth)^2 UnitBox[(x - m*PixelPitch /2)/PixelWidth,
(y - n*PixelPitch /2)/PixelWidth] /.{PixelWidth-> 1.2, PixelPitch -> 1.5};
hm[func_,m_,n_] := NIntegrate[Dm[x, y, m, n] func, {x, -Infinity, Infinity},
{y, -Infinity, Infinity}, WorkingPrecision -> 10,
PrecisionGoal -> 10, AccuracyGoal -> 10];
I define an example function that becomes the input to hm[func_,m_,n_]
:
ExampleFunc[x_, y_] := Exp[-(x^2/2 + y^2/2)];
Here I define the pixel coordinates and generate the output, i.e. the discretized function, using hm[func_,m_,n_]
:
NumberOfPixelsX = 6;
NumberOfPixelsY = 10;
AllPixelCords = Table[{i, j}, {i, NumberOfPixelsY/2, -NumberOfPixelsY/2, -1}, {j, NumberOfPixelsX/2, -NumberOfPixelsX/2, -1}];
FlattenedAllPixelCords = Flatten[AllPixelCords, {1, 2}];
PixelCoordinates = DeleteCases[DeleteCases[FlattenedAllPixelCords, {_, 0}, Infinity], {0, _}, Infinity]; (* Remove pixel coordinates with 0's*)
OperatorResult = hm[ExampleFunc[x, y], PixelCoordinates[[All, 1]], PixelCoordinates[[All, 2]]];
DiscretizedFunction = Partition[OperatorResult, NumberOfPixelsX];
MatrixPlot[DiscretizedFunction]
My problem is that this operation takes a lot of time to complete when I increase the number of pixels, i.e. when I use greater values for NumberOfPixelsX
and NumberOfPixelsY
. Is there a way to speed this up?.
NumberOfPixelsX/Y
to be? $\endgroup$NumberOfPixelsX = 512
andNumberOfPixelsY = 1024
. I am interested in this kernel because I need its value to be 1 for a specific pixel coordinate (PixelCoordinates
) and 0 for all the other coordinates. You can plotPlot3D[Dm[x,y,1,1]ExampleFunc[x, y], {x,-2,2},{y,-2,2}]
to see whatDm
is doing and then I integrate over that area. I want to keep the kernel general because I want to apply a different functions to it. $\endgroup$