I have a function that accepts a \$255\times1024\$ array and remaps it to another array in order to account for some hardware related distortion (lines that should be straight are curved by the lens). At the moment it does exactly what I want, but slowly (roughly 30 second runtime).
Specifically, I'm looking at the nested for loops that take 18 seconds to run, and the interpolation that takes 10s. Is there any way to optimize/speed up this process?
EDIT: Nested for loops have been optimized as per vps' answer. Am now only interested in optimizing the interpolation function (if that's even possible).
def smile(Z): p2p = np.poly1d([ -3.08049538e-07, 3.61724996e-04, -7.78775408e-02, 3.36876203e+00]) Y = np.flipud(np.rot90(np.tile(np.linspace(1,255,255),(1024,1)))) X = np.tile(np.linspace(1,1024,1024),(255,1)) for m in range(0,255): for n in range(0,1024): X[m,n] = X[m,n] - p2p(m+1) x = X.flatten() y = Y.flatten() z = Z.flatten() xy = np.vstack((x,y)).T grid_x, grid_y = np.mgrid[1:1024:1024j, 1:255:255j] newgrid = interpolate.griddata(xy, z,(grid_x,grid_y), method = 'linear',fill_value = 0).T return newgrid