import numpy as np
from scipy import integrate, signal
[docs]def norm_L2(a,x):
a_squared = np.squeeze(a**2)
n = len(np.shape(a_squared))
for i in range(n):
a_squared = integrate.simps(a_squared,x=x[i],axis=0)
return np.sqrt(a_squared)
[docs]def autocorrelation(H, pad=None):
"""
Calculates the autocorrelation of an array via ffts.
.. math::
\\text{autocovariance}(H,x) = \\text{fft}^{-1}(|\\text{fft}(H - \\overline{H},k)|^2,x)
\\text{autocorrelation}(H,x) = \\text{autocovariance}(x) / \\text{autocovariance}(0)
:param H: input array
:param pad: Pads each axis of the input with pad[i] zeros before and after each FFT.
"""
#pad = np.array(H.shape) if pad is None else pad
#H = H - np.mean(H)
#ft_signal = np.fft.fftn(H,pad)
#B = np.fft.ifftn(np.abs(ft_signal)**2,pad)
reverse = [slice(n,0,-1) for n in H.shape]
autocovariance = signal.fftconvolve(H,H[reverse],mode="full")[reverse]
return autocovariance / autocovariance.flat[0]