def despeckle_local(M, stds=<NUM_LIT:2>, width=<NUM_LIT:2>):
N = np.array(M, dtype=np.float64)<EOL>n, m = M.shape<EOL>for i, j in itertools.product(range(width, n - width),<EOL>range(width, m - width)):<EOL><INDENT>square = M[i - width:i + width, j - width:j + width]<EOL>avg = np.average(square)<EOL>std = np.std(square)<EOL>if M[i, j] >= avg + stds * std:<EOL><INDENT>N[i, j] = avg<EOL><DEDENT><DEDENT>return (N + N.T) / <NUM_LIT:2><EOL>
Replace outstanding values (above stds standard deviations)
    in a matrix by the average of a surrounding window of desired width.