sgnts.transforms.resampler
¶
Resampler
dataclass
¶
Bases: TSTransform
flowchart TD
sgnts.transforms.resampler.Resampler[Resampler]
sgnts.base.base.TSTransform[TSTransform]
sgnts.base.base.TimeSeriesMixin[TimeSeriesMixin]
sgnts.base.base.TSTransform --> sgnts.transforms.resampler.Resampler
sgnts.base.base.TimeSeriesMixin --> sgnts.base.base.TSTransform
click sgnts.transforms.resampler.Resampler href "" "sgnts.transforms.resampler.Resampler"
click sgnts.base.base.TSTransform href "" "sgnts.base.base.TSTransform"
click sgnts.base.base.TimeSeriesMixin href "" "sgnts.base.base.TimeSeriesMixin"
Up/down samples time-series data
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inrate
|
int
|
int, sample rate of the input frames |
required |
outrate
|
int
|
int, sample rate of the output frames |
required |
gstlal_norm
|
bool
|
boolean: If true it will normalize consistent with SGNL filter matching. If false it have a slightly more accurate normalization |
True
|
use_gstlal_cpu_upsample
|
bool
|
boolean: If true, use the fast C-based gstlal implementation (sgnl_cpu_interp) for upsampling only; raises ImportError at configure time if the package is not installed |
False
|
use_strided_downsample
|
bool | None
|
boolean: If None (the default), decide automatically whether scipy fft or strided correlation is used. Set to True/False do enable/ disable usage of this method for all kernel sizes. |
None
|
up_half_length
|
int
|
int, half length (in input-rate samples) of the upsampling
(anti-imaging) kernel. Defaults to |
UP_HALF_LENGTH
|
use_simd_resample
|
bool
|
bool: opt in to dispatching numpy-backend resampling (both
directions) to the |
False
|
Notes
Thread safety:
Marked thread_safe = True. With
Pipeline.run(threaded=N) the pad callbacks for this
element are dispatched onto worker threads.
Pad layout: 1 sink + 1 source pad
(enforced by ``@validator.one_to_one``). No same-element
``pull``/``new`` concurrency. ``internal`` runs alone.
Where the GIL-releasing work lives: ``internal()`` →
``process()`` → ``self.resample()``, which is one of
``scipy.signal.correlate``, ``torch.nn.functional.conv1d``,
or the C extension ``sgnl_cpu_interp.upsample_transposed``
— all release the GIL. Significant speedup expected with
multiple parallel resampling branches.
State touched per call:
- ``pull`` (inherited): per-pad-keyed dict writes.
- ``new`` (inherited): read-only lookup in ``self.outframes``.
- ``process``: reads ``self.thiskernel``, ``self.half_length``,
``self.resample`` (the bound method), and the
``adapter_config`` — all set in ``configure()`` and
read-only afterwards. Writes only the local output buffer.
**Future editors MUST preserve thread safety**: kernel and
adapter state must remain post-init read-only. Do not
relax the one-to-one constraint without re-auditing.
Source code in src/sgnts/transforms/resampler.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 127 128 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 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 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 299 300 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 329 330 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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 | |
downkernel(factor)
¶
Compute the kernel for downsampling. Modified from gstlal_interpolator.c
This is a sinc windowed sinc function kernel The baseline kernel is defined as
g[k] = sin(pi / f * (k-c)) / (pi / f * (k-c)) * (1 - (k-c)^2 / c / c) k != c g[k] = 1 k = c
Where:
f: downsample factor, must be power of 2, e.g., 2, 4, 8, ...
c: defined as half the full kernel length
You specify the half filter length at the target rate in samples, the kernel length is then given by:
kernel_length = half_length_at_original_rate * 2 * f + 1
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor
|
int
|
int, factor = inrate/outrate |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Array, the downsampling kernel |
Source code in src/sgnts/transforms/resampler.py
process(input_frame, output_frame)
¶
Resample input frame to output sample rate.
Source code in src/sgnts/transforms/resampler.py
resample(data, outshape)
¶
Resample data, dispatching on the backend of the data itself.
The numpy/scipy and torch/conv1d kernels are the sanctioned backend-specific "escape hatch"; which one runs is decided by the array, not by a pre-declared backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Array
|
Array, the data to be up/downsampled |
required |
outshape
|
tuple[int, ...]
|
tuple[int, ...], the shape of the output array |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Array, the resampled data (same backend as |
Source code in src/sgnts/transforms/resampler.py
resample_numpy(data0, outshape)
¶
Correlate the data with the kernel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data0
|
Array
|
Array, the data to be up/downsampled |
required |
outshape
|
tuple[int, ...]
|
tuple[int, ...], the shape of the output array |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Array, the resulting array of the up/downsamping |
Source code in src/sgnts/transforms/resampler.py
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | |
resample_simd(data0, outshape)
¶
Resample via the sgnl_cpu_interp SIMD C extension.
Same kernels and output-length contract as resample_numpy (the
extension generates the identical double-precision Lanczos-windowed
sinc; downsample output is rescaled to this element's normalization
convention via self._simd_down_scale).
Returns None when the input cannot be dispatched -- dtype outside the
extension's native set, or a buffer shorter than the kernel (where
resample_numpy defines the empty-output contract) -- in which
case the caller falls back to resample_numpy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data0
|
Array
|
Array, the data to be up/downsampled |
required |
outshape
|
tuple[int, ...]
|
tuple[int, ...], the shape of the output array |
required |
Returns:
| Type | Description |
|---|---|
Array | None
|
Array | None, the resampled data, or None to request fallback |
Source code in src/sgnts/transforms/resampler.py
resample_torch(data0, outshape)
¶
Correlate the data with the kernel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data0
|
Array
|
Array, the data to be up/downsampled |
required |
outshape
|
tuple[int, ...]
|
tuple[int, ...], the shape of the output array |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Array, the resulting array of the up/downsamping |
Source code in src/sgnts/transforms/resampler.py
upkernel(factor)
¶
Compute the kernel for upsampling. Modified from gstlal_interpolator.c
This is a sinc windowed sinc function kernel The baseline kernel is defined as
Where f is the interpolation factor (must be power of 2, e.g., 2, 4, 8, ...)
and c is defined as half the full kernel length.
You specify the half filter length at the original rate in samples, the kernel length is then given by:
kernel_length = half_length_at_original_rate * 2 * f + 1
Interpolation is then defined as a two step process. First the input data is zero filled to bring it up to the new sample rate, i.e., the input data, x, is transformed to x' such that:
x'[i] = x[i/f] if (i%f) == 0 = 0 if (i%f) > 0
y[i] = sum_{k=0}^{2c+1} x'[i-k] g[k]
Since more than half the terms in this series would be zero, the convolution is implemented by breaking up the kernel into f separate kernels each 1/f as large as the originalcalled z, i.e.,:
z[0][k/f] = g[kf] z[1][k/f] = g[kf+1] ... z[f-1][k/f] = g[k*f + f-1]
Now the convolution can be written as:
y[i] = sum_{k=0}^{2c/f+1} x[i/f] z[i%f][k]
which avoids multiplying zeros. Note also that by construction the sinc function has its zeros arranged such that z[0][:] had only one nonzero sample at its center. Therefore the actual convolution is:
y[i] = x[i/f] if i%f == 0 y[i] = sum_{k=0}^{2c/f+1} x[i/f] z[i%f][k] otherwise
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
factor
|
int
|
int, factor = outrate/inrate |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Array, the upsampling kernel |
Source code in src/sgnts/transforms/resampler.py
upsample_gstlal(data)
¶
Upsample using gstlal implementation.
Handles both numpy arrays and torch tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Input data (numpy array or torch tensor), shape (-1, n_samples) |
required |
Returns:
| Type | Description |
|---|---|
|
Upsampled data (same type as input), not reshaped |