<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def estimate_noise_std(img, average=True):
 """Estimate standard deviation of noise in ``img``. The algorithm, given in [Immerkaer1996], estimates the noise in an image. Parameters img : array-like Array to estimate noise in. average : bool If ``True``, return the mean noise in the image, otherwise give a pointwise estimate. Returns ------- noise : float Examples -------- Create image with noise 1.0, verify result Also works with higher dimensional arrays The method can also estimate the noise pointwise (but with high uncertainity):
 References [Immerkaer1996] Immerkaer, J. *Fast Noise Variance Estimation*. Computer Vision and Image Understanding, 1996. """

    import scipy.signal
    import functools
    img = np.asarray(img, dtype='float')

    M = functools.reduce(np.add.outer, [[-1, 2, -1]] * img.ndim)

    convolved = scipy.signal.fftconvolve(img, M, mode='valid')
    if average:
        conv_var = np.sum(convolved ** 2) / convolved.size
    else:
        conv_var = convolved ** 2

        # Pad in order to retain shape
        conv_var = np.pad(conv_var, pad_width=1, mode='edge')

    scale = np.sum(np.square(M))
    sigma = np.sqrt(conv_var / scale)

    return sigma