44import numpy as np
55from . import arrays
66
7- def rapsd (Z , fft_method = None , ** fft_kwargs ):
7+ def rapsd (Z , fft_method = None , return_freq = False , ** fft_kwargs ):
88 """Compute radially averaged power spectral density (RAPSD) from the given
99 2D input field.
1010
@@ -17,12 +17,16 @@ def rapsd(Z, fft_method=None, **fft_kwargs):
1717 scipy.fftpack. If set to None, Z is assumed to represent the shifted
1818 discrete Fourier transform of the input field, where the origin is at
1919 the center of the array (see numpy.fft.fftshift or scipy.fftpack.fftshift).
20+ return_freq: bool
21+ Whether to also return the Fourier frequencies.
2022
2123 Returns
2224 -------
2325 out : ndarray
2426 One-dimensional array containing the RAPSD. The length of the array is
2527 int(L/2)+1 (if L is even) or int(L/2) (if L is odd), where L=max(M,N).
28+ freq : ndarray
29+ One-dimensional array containing the Fourier frequencies.
2630
2731 References
2832 ----------
@@ -47,6 +51,7 @@ def rapsd(Z, fft_method=None, **fft_kwargs):
4751
4852 if fft_method is not None :
4953 F = fft_method .fftshift (fft_method .fft2 (Z , ** fft_kwargs ))
54+ F = np .abs (F )** 2 / F .size
5055 else :
5156 F = Z
5257
@@ -55,5 +60,10 @@ def rapsd(Z, fft_method=None, **fft_kwargs):
5560 MASK = R == r
5661 F_vals = F [MASK ]
5762 result .append (np .mean (F_vals ))
58-
59- return np .array (result )
63+
64+ if return_freq :
65+ freq = np .fft .fftfreq (L )
66+ freq = freq [r_range ]
67+ return np .array (result ), freq
68+ else :
69+ return np .array (result )
0 commit comments