sgnts.transforms.correlate
¶
AdaptiveCorrelate
dataclass
¶
Bases: Correlate
flowchart TD
sgnts.transforms.correlate.AdaptiveCorrelate[AdaptiveCorrelate]
sgnts.transforms.correlate.Correlate[Correlate]
sgnts.base.base.TSTransform[TSTransform]
sgnts.base.base.TimeSeriesMixin[TimeSeriesMixin]
sgnts.transforms.correlate.Correlate --> sgnts.transforms.correlate.AdaptiveCorrelate
sgnts.base.base.TSTransform --> sgnts.transforms.correlate.Correlate
sgnts.base.base.TimeSeriesMixin --> sgnts.base.base.TSTransform
click sgnts.transforms.correlate.AdaptiveCorrelate href "" "sgnts.transforms.correlate.AdaptiveCorrelate"
click sgnts.transforms.correlate.Correlate href "" "sgnts.transforms.correlate.Correlate"
click sgnts.base.base.TSTransform href "" "sgnts.base.base.TSTransform"
click sgnts.base.base.TimeSeriesMixin href "" "sgnts.base.base.TimeSeriesMixin"
Adaptive Correlate filter with Strategy Pattern for transitions.
This element implements the Adaptive Finite Impulse Response (AFIR) theory for streaming data. It manages a queue of filter updates arriving asynchronously and ensures mathematical coherence during transitions via four core design principles:
Principle 1 (Event-Driven Updates): Filters arrive as discrete events with a validity offset. The element maintains a chronologically sorted queue of these states.
Principle 2 (Last-Write-Wins): If multiple updates arrive for the same offset, the most recent reception overwrites the previous ones.
Principle 3 (Clock Coherence): Continuous-time validity offsets are mapped to discrete integer sample boundaries to prevent sub-sample phase artifacts.
Principle 4 (Stationarity Preservation): Stride processing is segmented into intervals of local stationarity (Discrete) or smooth blending (Adiabatic) to avoid unphysical transients.
Notes
Startup behavior (no explicit initial conditions). This element
accepts filters=None. On startup, it emits gap buffers (no data) until a
filter bank is received on the dedicated filters sink pad
(filter_sink_name); the element reconfigures its shape and overlap from
the first bank as it becomes active. Subsequent updates are blended over
a stride as described below. During this gap startup, filter_dtype
declares the output dtype before any filters exist.
Thread safety. Marked thread_safe = True. With
Pipeline.run(threaded=N) the pad callbacks for this element are
dispatched onto worker threads.
Pad layout: 2 sink pads (data + filter) + 1 source pad. The two sink
pads' pull callbacks CAN run concurrently in the same wave; this is
the per-pad concurrency to reason about. internal runs alone (single
InternalPad).
Where the GIL-releasing work lives: internal() calls
scipy.signal.correlate (and scipy.signal.windows.cosine during
filter adaptation), both of which release the GIL, so significant
speedup is expected when multiple correlation branches run in parallel.
Per-pad concurrency analysis:
pullon the data sink pad: inheritedTimeSeriesMixin.pullonly; writes per-pad-keyedinbufs/metadatafor the data pad. Does NOT touchself.filter_deque.pullon the filter sink pad: overridden; callssuper().pull()(per-pad-keyed for the filter pad's own slot), then appends toself.filter_deque. The filter sink pad is the sole writer ofself.filter_deque, so concurrent same-element pulls cannot produce a same-deque write race.internal: readsself.filter_dequeand maypopleftfrom it; runs alone (no concurrent reader or writer in the same wave).
Future editors MUST preserve thread safety:
- Do NOT add new writers to
self.filter_dequefrom anypullpath other than the filter sink pad; that would introduce a same-deque write race across same-wave pulls. - Do NOT introduce additional element-level state mutated from
pullpaths outside of per-pad-keyed containers. self.filtersis reassigned during adaptation ininternal(); that is fine becauseinternalruns alone, but if you split adaptation logic intopullyou must moveself.filtersto per-call local state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filter_sink_name
|
str
|
Name of the sink pad receiving filter updates. Defaults to "filters". |
'filters'
|
verbose
|
bool
|
Enables diagnostic logging of filter scheduling decisions. |
False
|
transition_profile
|
TransitionProfile
|
A TransitionProfile instance defining the blending strategy between the old and new filters. Options are:
|
CosSquaredTransition()
|
filter_dtype
|
Optional[Any]
|
Optional[Any], the filter dtype. Must be specified if the filters differ from the dtype of the incoming data, so that the output dtype can be inferred correctly. If None, it is assumed that the output dtype is equal to the input dtype. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
Raises a value error if more than one filter update is passed per stride, or if a filter update would change the output dtype mid-stream |
Source code in src/sgnts/transforms/correlate.py
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 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 | |
filters_cur
property
¶
Returns the coefficients of the active (oldest) filter.
static_unaligned_sink_pads
property
¶
Mark the filters pad as asynchronous. This prevents the audio thread from blocking if filter updates are slow.
internal()
¶
Adaptive internal loop: manages filter lifecycle and handles mid-stride transitions.
Source code in src/sgnts/transforms/correlate.py
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 | |
pull(pad, frame)
¶
Pull data from sinks. Handles asynchronous filter updates.
Source code in src/sgnts/transforms/correlate.py
Correlate
dataclass
¶
Bases: TSTransform
flowchart TD
sgnts.transforms.correlate.Correlate[Correlate]
sgnts.base.base.TSTransform[TSTransform]
sgnts.base.base.TimeSeriesMixin[TimeSeriesMixin]
sgnts.base.base.TSTransform --> sgnts.transforms.correlate.Correlate
sgnts.base.base.TimeSeriesMixin --> sgnts.base.base.TSTransform
click sgnts.transforms.correlate.Correlate href "" "sgnts.transforms.correlate.Correlate"
click sgnts.base.base.TSTransform href "" "sgnts.base.base.TSTransform"
click sgnts.base.base.TimeSeriesMixin href "" "sgnts.base.base.TimeSeriesMixin"
Correlates input data with a fixed or dynamic filter.
This element performs a standard multi-channel correlation: Out = Data * Filter. It uses the sgn-ts AudioAdapter to manage overlap-save convolution history, ensuring that N-1 samples of history are maintained across stride boundaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_rate
|
int
|
The audio sample rate in Hz. |
required |
filters
|
Optional[Array]
|
Initial filter coefficients. Should be shape (channels, taps). If None, the element defaults to (1, 1) until an update arrives. |
None
|
latency
|
int
|
The output timing offset, in samples: an output computed from
input up to time
|
0
|
taps_reversed
|
bool
|
If True, the filter taps are pre-reversed so that scipy.correlate(data, taps_reversed) = convolution with original taps. This is standard for asymmetric (e.g., minimum-phase) FIR filters. When True, the temporal invariant is adjusted so that latency=0 still means "causal / zero-latency". |
False
|
pad_zeros
|
bool
|
boolean, whether to pad with zeros on startup or not. This determines whether the pipeline stalls initially or just starts producing values (with data zero-padded to match the filter length). |
False
|
method
|
Literal['auto', 'direct', 'fft']
|
str, the convolution backend passed to scipy.signal.correlate: "auto" (default), "direct", or "fft". scipy's "auto" heuristic can mis-select for overlap-save input shapes at small strides (picking "direct" when "fft" is several times faster for long filters), so exposing this lets callers force the fast path. |
'auto'
|
Notes
Latency and output timing. latency shifts the timestamps
written on the output buffers. Two distinct kinds of latency must be
kept apart:
- Physical (timestamp) latency is introduced here, and is real. The
published output is labeled
latencysamples behind, so a testpoint on the output frame, or a channel sent to a sink, reports exactly that latency, whatever follows this element. - Algorithmic latency (waiting in the execution loop) is not
introduced here. The element runs the same valid-mode correlation
over history the adapter already holds, leaves the sample values
unchanged, and emits each buffer as soon as it otherwise would;
nothing waits. A downstream element that synchronizes its inputs by
timestamp (via
AdapterConfig) may wait for a relabeled buffer to line up, but that is a separate, downstream effect, not always enforced.
Each output depends on a window of N input samples, and latency
chooses which one dates it: 0 the newest, N - 1 the oldest,
(N - 1) / 2 the center. The linear-phase value (N - 1) / 2
places a symmetric filter's features at their true time; the reference
covers the general, frequency-dependent case.
For the underlying signal processing, see the
Signal Processing Fundamentals
reference: Latency defines the
physical and algorithmic kinds with worked examples, and
Phase Delay and Group Delay
explains why the delay tau is in general frequency dependent.
Thread safety. Marked thread_safe = True. With
Pipeline.run(threaded=N) the pad callbacks (pull, new,
internal) for this element are dispatched onto worker threads.
Pad layout: 1 sink + 1 source pad (enforced by
@validator.one_to_one). There is therefore no same-element
pull/new concurrency to worry about: only one pull and one
new ever run at a time on this element. internal always runs
alone (single InternalPad).
Where the GIL-releasing work lives: internal() calls
scipy.signal.correlate, which releases the GIL, so this element
delivers significant wall-clock speedup when there are multiple parallel
correlation branches in the graph and their elements all opt in.
State touched per call:
pull(inheritedTimeSeriesMixin.pull): writes per-pad-keyed dicts (inbufs[pad],metadata[pad]); ORsself.at_EOS(idempotent for booleans).new(inheritedTSTransform.new): read-only lookup inself.outframes.internal: readsself.filters(set inconfigure()and read-only afterwards) andself.shape; writes the next output frame.
Future editors MUST preserve thread safety: do not relax the
one-to-one constraint without re-auditing self.filters access. Do
not introduce element-level state that is mutated from pull/new
outside of per-pad-keyed containers.
Source code in src/sgnts/transforms/correlate.py
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 | |
configure()
¶
Setup alignment based on filter length (N) and user latency property.
The SGN-TS temporal invariant is: T_out = T_in + (N-1) - L Where L is the compensation (shift). To achieve T_out = T_in + self.latency, we set L = (N-1) - self.latency.
When taps_reversed=True, pre-reversed taps + scipy.correlate = convolution. Convolution reverses the filter's time axis, so a causal kernel (energy at index 0) appears anti-causal (energy at index N-1) to the correlator. We compensate by mapping: effective_latency = (N-1) - user_latency. This way latency=0 means "zero latency" regardless of tap ordering.
Source code in src/sgnts/transforms/correlate.py
corr(data)
¶
Perform the mathematical correlation.
Uses scipy.signal.correlate(mode='valid') which effectively
reverses the filter taps. self.method selects scipy's
convolution backend ("auto"/"direct"/"fft").
Source code in src/sgnts/transforms/correlate.py
internal()
¶
Standard SGN-TS internal loop: pulls aligned input and executes transform.
Source code in src/sgnts/transforms/correlate.py
CosSquaredTransition
dataclass
¶
Bases: TransitionProfile
flowchart TD
sgnts.transforms.correlate.CosSquaredTransition[CosSquaredTransition]
sgnts.transforms.correlate.TransitionProfile[TransitionProfile]
sgnts.transforms.correlate.TransitionProfile --> sgnts.transforms.correlate.CosSquaredTransition
click sgnts.transforms.correlate.CosSquaredTransition href "" "sgnts.transforms.correlate.CosSquaredTransition"
click sgnts.transforms.correlate.TransitionProfile href "" "sgnts.transforms.correlate.TransitionProfile"
Standard Cosine-Squared crossfade.
Provides C1 continuity at the boundaries and constant power summation for uncorrelated noise. This is the preferred profile for smooth, adiabatic filter updates.
Source code in src/sgnts/transforms/correlate.py
DiscreteTransition
dataclass
¶
Bases: TransitionProfile
flowchart TD
sgnts.transforms.correlate.DiscreteTransition[DiscreteTransition]
sgnts.transforms.correlate.TransitionProfile[TransitionProfile]
sgnts.transforms.correlate.TransitionProfile --> sgnts.transforms.correlate.DiscreteTransition
click sgnts.transforms.correlate.DiscreteTransition href "" "sgnts.transforms.correlate.DiscreteTransition"
click sgnts.transforms.correlate.TransitionProfile href "" "sgnts.transforms.correlate.TransitionProfile"
Hard switch at the boundary.
This profile ensures that the output is piecewise stationary, matching the target filter exactly at the transition offset without any crossfading.
Source code in src/sgnts/transforms/correlate.py
FilterState
dataclass
¶
Internal immutable record of a filter's temporal validity.
Attributes:
| Name | Type | Description |
|---|---|---|
offset |
int
|
The start time (in framework ticks) of the filter. |
noffset |
int
|
The duration (in framework ticks) of pre-calculated validity. |
data |
Array
|
The filter coefficients (taps). |
Source code in src/sgnts/transforms/correlate.py
LinearTransition
dataclass
¶
Bases: TransitionProfile
flowchart TD
sgnts.transforms.correlate.LinearTransition[LinearTransition]
sgnts.transforms.correlate.TransitionProfile[TransitionProfile]
sgnts.transforms.correlate.TransitionProfile --> sgnts.transforms.correlate.LinearTransition
click sgnts.transforms.correlate.LinearTransition href "" "sgnts.transforms.correlate.LinearTransition"
click sgnts.transforms.correlate.TransitionProfile href "" "sgnts.transforms.correlate.TransitionProfile"
Linear constant-voltage crossfade.
Simple arithmetic blending of filter outputs. Useful for low-complexity scenarios or debugging.
Source code in src/sgnts/transforms/correlate.py
PlanckTaperTransition
dataclass
¶
Bases: TransitionProfile
flowchart TD
sgnts.transforms.correlate.PlanckTaperTransition[PlanckTaperTransition]
sgnts.transforms.correlate.TransitionProfile[TransitionProfile]
sgnts.transforms.correlate.TransitionProfile --> sgnts.transforms.correlate.PlanckTaperTransition
click sgnts.transforms.correlate.PlanckTaperTransition href "" "sgnts.transforms.correlate.PlanckTaperTransition"
click sgnts.transforms.correlate.TransitionProfile href "" "sgnts.transforms.correlate.TransitionProfile"
Planck-taper crossfade.
Uses the Planck-taper window, a C-infinity (all derivatives continuous) smooth step. Because the transition and every one of its derivatives vanish at both boundaries, it produces the gentlest spectral leakage of the available profiles during a filter handover.
See McKechan, Robinson & Sathyaprakash (2010), arXiv:1003.2939.
Source code in src/sgnts/transforms/correlate.py
ReverseDiscreteTransition
dataclass
¶
Bases: TransitionProfile
flowchart TD
sgnts.transforms.correlate.ReverseDiscreteTransition[ReverseDiscreteTransition]
sgnts.transforms.correlate.TransitionProfile[TransitionProfile]
sgnts.transforms.correlate.TransitionProfile --> sgnts.transforms.correlate.ReverseDiscreteTransition
click sgnts.transforms.correlate.ReverseDiscreteTransition href "" "sgnts.transforms.correlate.ReverseDiscreteTransition"
click sgnts.transforms.correlate.TransitionProfile href "" "sgnts.transforms.correlate.TransitionProfile"
Identity switch (keeps old filter).
Primarily used for testing skip_new optimization paths.
Source code in src/sgnts/transforms/correlate.py
TransitionProfile
dataclass
¶
Bases: ABC
flowchart TD
sgnts.transforms.correlate.TransitionProfile[TransitionProfile]
click sgnts.transforms.correlate.TransitionProfile href "" "sgnts.transforms.correlate.TransitionProfile"
Base class for filter transition strategies.
This follows the Strategy Pattern to allow different types of blending (Cos2, Linear, Discrete, Planck-taper) between old and new filters when an update occurs mid-stride.
Source code in src/sgnts/transforms/correlate.py
skip_new
property
¶
Optimization: if True, the new filter correlation can be skipped.
skip_old
property
¶
Optimization: if True, the old filter correlation can be skipped.
get_weights(n, backend)
abstractmethod
¶
Returns (old_weights, new_weights) for a stride of length n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of samples in the transition window. |
required |
backend
|
Any
|
The array backend (NumPy/Torch) to use. |
required |