Waveform Helpers
Waveform injection and spectral plotting utilities.
Low-level helpers for inserting a pulse waveform into a flat datacube buffer, applying a matched filter via FFT convolution, resampling waveforms, and plotting pulse shapes with their spectra.
logger = logging.getLogger(__name__)
module-attribute
add_waveform_at_index(ar, waveform, index)
Adds a waveform to an array at a specified starting index.
This function modifies the target array ar in-place by adding the
waveform to it, starting at the given index. If the waveform extends
beyond the bounds of ar, it is truncated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ar
|
ndarray
|
The target array to be modified. |
required |
waveform
|
ndarray
|
The waveform to add to the target array. |
required |
index
|
int
|
The starting index in |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The modified target array |
Source code in src/rad_lab/waveform_helpers.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |
autocorrelate_waveform(waveform)
Calculates the autocorrelation of a waveform using the FFT method.
The autocorrelation is computed by multiplying the Fourier Transform of the waveform with its complex conjugate and then performing an inverse Fourier Transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
waveform
|
ndarray
|
The 1D input signal (real or complex). |
required |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray]
|
tuple[np.ndarray, np.ndarray]: A tuple containing: - np.ndarray: The autocorrelation result. - np.ndarray: An array of index shifts corresponding to the lags of the autocorrelation, centered at zero. |
Source code in src/rad_lab/waveform_helpers.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
find_width(x, y, interp_max=5, interp_count=0, interp_scale=2)
Recursively finds the full width at half maximum (FWHM) of a signal.
This function calculates the width of a pulse by finding the points where the signal crosses half of its maximum amplitude. If two such points are not found at the current resolution, it recursively interpolates the signal to a finer grid and tries again, up to a maximum number of interpolations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
The independent variable array (e.g., time or frequency). |
required |
y
|
ndarray
|
The dependent variable array (the signal). |
required |
interp_max
|
int
|
The maximum number of recursive interpolation steps. Defaults to 5. |
5
|
interp_count
|
int
|
The current interpolation step count, used for recursion. Defaults to 0. |
0
|
interp_scale
|
int
|
The factor by which to increase the number of points during each interpolation step. Defaults to 2. |
2
|
Returns:
| Type | Description |
|---|---|
tuple[float, float, float] | list[float]
|
tuple[float, float, float] or list[float]: A tuple containing: - float: The calculated pulse width (FWHM). - float: The x-value at the start of the pulse width (first half-max crossing). - float: The x-value at the end of the pulse width (last half-max crossing). Returns [np.nan, np.nan, np.nan] if the width cannot be found within the interpolation limit. |
Source code in src/rad_lab/waveform_helpers.py
63 64 65 66 67 68 69 70 71 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 | |
matchfilter_with_waveform(ar, waveform)
Performs matched filtering of a signal with a given waveform.
This function applies a matched filter to the input array ar using the
provided waveform as the template. The filter is implemented as a
convolution with the time-reversed complex conjugate of the waveform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ar
|
ndarray
|
The input signal array to be filtered. |
required |
waveform
|
ndarray
|
The template waveform for the matched filter. |
required |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray]
|
tuple[np.ndarray, np.ndarray]: A tuple containing: - np.ndarray: An array of index shifts corresponding to the output, centered at zero. - np.ndarray: The result of the matched filter convolution. |
Source code in src/rad_lab/waveform_helpers.py
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | |
moving_average(waveform, N_elements)
Calculates the moving average of a waveform.
This function smooths the input waveform by convolving it with a uniform kernel of a specified size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
waveform
|
ndarray
|
The 1D signal array to be averaged. |
required |
N_elements
|
int
|
The number of elements in the moving average window. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The smoothed waveform as a 1D array. |
Source code in src/rad_lab/waveform_helpers.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
plot_pulse_and_spectrum(t, mag, title=None, n_pad=0, print_bandwidth=True, spec_dec=False)
Plots a signal in the time and frequency domains.
Generates a two-panel plot showing the signal's magnitude over time and its frequency spectrum. The spectrum is calculated using an FFT, and the signal can be zero-padded to improve frequency resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
ndarray
|
The time array for the signal. |
required |
mag
|
ndarray
|
The signal magnitude array (can be complex). |
required |
title
|
str
|
The super-title for the entire figure. Defaults to None. |
None
|
n_pad
|
int
|
The number of zeros to append for the FFT calculation, improving frequency resolution. Defaults to 0. |
0
|
print_bandwidth
|
bool
|
If True, calculates and prints the signal's bandwidth (FWHM of the spectrum). Defaults to True. |
True
|
spec_dec
|
bool
|
If True, plots the spectrum magnitude in decibels (dB). Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[Figure, ndarray]
|
tuple[plt.Figure, np.ndarray[plt.Axes]]: A tuple containing: - plt.Figure: The matplotlib Figure object. - np.ndarray[plt.Axes]: An array of the two matplotlib Axes objects (time domain and frequency domain). |
Source code in src/rad_lab/waveform_helpers.py
129 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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
plot_pulse_and_xcorrelation(t, mag, title=None, print_width=True)
Plots a signal and its autocorrelation.
Generates a two-panel plot showing the signal's magnitude over time and its autocorrelation function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
ndarray
|
The time array for the signal. |
required |
mag
|
ndarray
|
The signal magnitude array (can be complex). |
required |
title
|
str
|
The super-title for the entire figure. Defaults to None. |
None
|
print_width
|
bool
|
If True, calculates and prints the width (FWHM) of the main autocorrelation lobe. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
tuple[Figure, ndarray]
|
tuple[plt.Figure, np.ndarray[plt.Axes]]: A tuple containing: - plt.Figure: The matplotlib Figure object. - np.ndarray[plt.Axes]: An array of the two matplotlib Axes objects (time domain and autocorrelation). |
Source code in src/rad_lab/waveform_helpers.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
zeropad_waveform(t, waveform, N_pad)
Zeropads a waveform and adjusts the corresponding time array.
This function appends a specified number of zeros to the end of a waveform and recalculates the time array to match the new length, preserving the original time step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
ndarray
|
The original 1D time array. |
required |
waveform
|
ndarray
|
The 1D signal array. |
required |
N_pad
|
int
|
The number of zero samples to append. |
required |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray]
|
tuple[np.ndarray, np.ndarray]: A tuple containing: - np.ndarray: The new, extended time array. - np.ndarray: The new, zero-padded waveform. |
Source code in src/rad_lab/waveform_helpers.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |