Spectral utils
Utility functions for spectral analysis. Defaults are chosen to replicate MATLAB's default behavior.
csd
csd(x, y, fs, num_windows=8, window_type='hamming', window_len=None, nfft=None, detrend=False, onesided=True)
Cross spectral density via Welch's method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Two input signals of identical shape; the longest axis is time. |
required |
y
|
ndarray
|
Two input signals of identical shape; the longest axis is time. |
required |
fs
|
float
|
Sampling frequency (Hz). |
required |
num_windows
|
int
|
Number of (50%-overlapping) Welch windows when |
8
|
window_type
|
str
|
Window passed to |
'hamming'
|
window_len
|
int
|
Window length in samples. If None, derived from |
None
|
nfft
|
int
|
FFT length. Defaults to |
None
|
detrend
|
bool
|
If True, detrend each segment before transforming. |
False
|
onesided
|
bool
|
If True, return the one-sided spectrum. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
f |
ndarray
|
Frequency vector (Hz). |
Pxy |
ndarray
|
Complex cross spectral density (units of x*y / Hz). |
Source code in src/pytoast/utils/spectral_utils.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
get_frequency_range
get_frequency_range(f, f_low=None, f_high=None)
Index range into f covering [f_low, f_high].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
ndarray
|
Monotonically increasing frequency vector (Hz). |
required |
f_low
|
float
|
Lower frequency bound (Hz). If None, start at index 0. |
None
|
f_high
|
float
|
Upper frequency bound (Hz). If None, end at |
None
|
Returns:
| Type | Description |
|---|---|
tuple of int
|
|
Source code in src/pytoast/utils/spectral_utils.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
get_window_len
get_window_len(N, num_windows)
Welch-method window length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
N
|
int
|
Number of samples in the time series. |
required |
num_windows
|
int
|
Number of (50%-overlapping) windows desired. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Window length in samples. |
Source code in src/pytoast/utils/spectral_utils.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
plot_spectral_fit
plot_spectral_fit(x, y, x_fit, y_fit, eps, xlabel='Wavenumber $k$ (rad/m)', ylabel='Spectral density', title=None, out_file=None)
Save a log-log plot of a spectral curve fit to a PNG file.
Draws the observed spectrum as scattered points together with the fitted -5/3 curve. The caller supplies the data points and the pre-computed fit line, so this function is agnostic to the specific model or independent variable (wavenumber or angular frequency).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Independent variable of the observed data points over the fit range (e.g. wavenumber in rad/m or angular frequency in rad/s). |
required |
y
|
ndarray
|
Observed spectral density at |
required |
x_fit
|
ndarray
|
Independent variable of the fitted curve (e.g. a linspace over the fit range). |
required |
y_fit
|
ndarray
|
Fitted spectral density evaluated at |
required |
eps
|
float
|
Dissipation rate estimate (m^2/s^3), shown in the legend label. |
required |
xlabel
|
str
|
Label for the x-axis. Defaults to a wavenumber label. |
'Wavenumber $k$ (rad/m)'
|
ylabel
|
str
|
Label for the y-axis. Defaults to a generic spectral density label. |
'Spectral density'
|
title
|
str
|
Axis title. If None, no title is drawn. |
None
|
out_file
|
str
|
Destination PNG path. If None, a temporary file (suffix ".png") is created. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Path to the saved PNG file. |
Source code in src/pytoast/utils/spectral_utils.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
psd
psd(x, fs, num_windows=8, window_type='hamming', window_len=None, nfft=None, detrend=False, onesided=True)
Power spectral density via Welch's method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Input signal. The longest axis is treated as time. |
required |
fs
|
float
|
Sampling frequency (Hz). |
required |
num_windows
|
int
|
Number of (50%-overlapping) Welch windows when |
8
|
window_type
|
str
|
Window passed to |
'hamming'
|
window_len
|
int
|
Window length in samples. If None, derived from |
None
|
nfft
|
int
|
FFT length. Defaults to |
None
|
detrend
|
bool
|
If True, detrend each segment before transforming. |
False
|
onesided
|
bool
|
If True, return the one-sided spectrum. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
f |
ndarray
|
Frequency vector (Hz). |
Pxx |
ndarray
|
Power spectral density (units of |
Source code in src/pytoast/utils/spectral_utils.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |