1
- # -*- coding: utf-8 -*-
2
1
"""
3
2
pysteps.utils.spectral
4
3
======================
@@ -96,19 +95,21 @@ def mean(X, shape):
96
95
return np .real (X [0 , 0 ]) / (shape [0 ] * shape [1 ])
97
96
98
97
99
- def rapsd (Z , fft_method = None , return_freq = False , d = 1.0 , normalize = False , ** fft_kwargs ):
98
+ def rapsd (
99
+ field , fft_method = None , return_freq = False , d = 1.0 , normalize = False , ** fft_kwargs
100
+ ):
100
101
"""Compute radially averaged power spectral density (RAPSD) from the given
101
102
2D input field.
102
103
103
104
Parameters
104
105
----------
105
- Z : array_like
106
- A 2d array of shape (M,N ) containing the input field.
106
+ field : array_like
107
+ A 2d array of shape (m, n ) containing the input field.
107
108
fft_method: object
108
109
A module or object implementing the same methods as numpy.fft and
109
- scipy.fftpack. If set to None, Z is assumed to represent the shifted
110
- discrete Fourier transform of the input field, where the origin is at
111
- the center of the array
110
+ scipy.fftpack. If set to None, field is assumed to represent the
111
+ shifted discrete Fourier transform of the input field, where the
112
+ origin is at the center of the array
112
113
(see numpy.fft.fftshift or scipy.fftpack.fftshift).
113
114
return_freq: bool
114
115
Whether to also return the Fourier frequencies.
@@ -122,7 +123,7 @@ def rapsd(Z, fft_method=None, return_freq=False, d=1.0, normalize=False, **fft_k
122
123
-------
123
124
out: ndarray
124
125
One-dimensional array containing the RAPSD. The length of the array is
125
- int(L /2)+1 (if L is even) or int(L /2) (if L is odd), where L =max(M,N ).
126
+ int(l /2) (if l is even) or int(l /2)+1 (if l is odd), where l =max(m,n ).
126
127
freq: ndarray
127
128
One-dimensional array containing the Fourier frequencies.
128
129
@@ -131,45 +132,45 @@ def rapsd(Z, fft_method=None, return_freq=False, d=1.0, normalize=False, **fft_k
131
132
:cite:`RC2011`
132
133
"""
133
134
134
- if len (Z .shape ) != 2 :
135
+ if len (field .shape ) != 2 :
135
136
raise ValueError (
136
- f"{ len (Z .shape )} dimensions are found, but the number "
137
+ f"{ len (field .shape )} dimensions are found, but the number "
137
138
"of dimensions should be 2"
138
139
)
139
140
140
- if np .sum (np .isnan (Z )) > 0 :
141
- raise ValueError ("input array Z should not contain nans" )
141
+ if np .sum (np .isnan (field )) > 0 :
142
+ raise ValueError ("input field should not contain nans" )
142
143
143
- M , N = Z .shape
144
+ m , n = field .shape
144
145
145
- YC , XC = arrays .compute_centred_coord_array (M , N )
146
- R = np .sqrt (XC * XC + YC * YC ).round ()
147
- L = max (Z .shape [0 ], Z .shape [1 ])
146
+ yc , xc = arrays .compute_centred_coord_array (m , n )
147
+ r_grid = np .sqrt (xc * xc + yc * yc ).round ()
148
+ l = max (field .shape [0 ], field .shape [1 ])
148
149
149
- if L % 2 == 0 :
150
- r_range = np .arange (0 , int (L / 2 ) + 1 )
150
+ if l % 2 == 1 :
151
+ r_range = np .arange (0 , int (l / 2 ) + 1 )
151
152
else :
152
- r_range = np .arange (0 , int (L / 2 ))
153
+ r_range = np .arange (0 , int (l / 2 ))
153
154
154
155
if fft_method is not None :
155
- F = fft_method .fftshift (fft_method .fft2 (Z , ** fft_kwargs ))
156
- F = np .abs (F ) ** 2 / F .size
156
+ psd = fft_method .fftshift (fft_method .fft2 (field , ** fft_kwargs ))
157
+ psd = np .abs (psd ) ** 2 / psd .size
157
158
else :
158
- F = Z
159
+ psd = field
159
160
160
161
result = []
161
162
for r in r_range :
162
- MASK = R == r
163
- F_vals = F [ MASK ]
164
- result .append (np .mean (F_vals ))
163
+ mask = r_grid == r
164
+ psd_vals = psd [ mask ]
165
+ result .append (np .mean (psd_vals ))
165
166
166
167
result = np .array (result )
167
168
168
169
if normalize :
169
170
result /= np .sum (result )
170
171
171
172
if return_freq :
172
- freq = np .fft .fftfreq (L , d = d )
173
+ freq = np .fft .fftfreq (l , d = d )
173
174
freq = freq [r_range ]
174
175
return result , freq
175
176
else :
0 commit comments