Multi-Sensor
Tools for analysing relationships between multiple measurement channels: correlation matrices, coherence matrices, the cross-spectral density matrix (input to FDD), and the conditioned forms — multiple and partial coherence — that answer whether a channel is redundant given the rest of the array.

dspkit.multisensor.correlation_matrix(data)
Pairwise Pearson correlation matrix for multi-channel data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
(array_like, shape(n_channels, N))
|
Each row is a time series from one sensor. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
R |
(ndarray, shape(n_channels, n_channels))
|
Correlation matrix with values in [-1, 1].
|
Source code in dspkit/multisensor.py
dspkit.multisensor.coherence_matrix(data, fs, window='hann', nperseg=None, noverlap=None, detrend='constant', min_segments=8)
Pairwise magnitude-squared coherence matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
(array_like, shape(n_channels, N))
|
Each row is a time series from one sensor. |
required |
fs
|
float
|
Sampling frequency [Hz]. |
required |
window
|
str
|
Window function (default |
'hann'
|
nperseg
|
int or None
|
Welch segment length. Defaults to |
None
|
noverlap
|
int or None
|
Overlap between segments. |
None
|
detrend
|
str or False
|
Per-segment detrending. |
'constant'
|
min_segments
|
int
|
Warn below this many Welch segments (default 8). Fewer than two segments raises instead — see Notes. |
8
|
Returns:
| Name | Type | Description |
|---|---|---|
freqs |
(ndarray, shape(M))
|
Frequency vector [Hz]. |
C |
(ndarray, shape(n_channels, n_channels, M))
|
Coherence matrix. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the parameters give fewer than two Welch segments, in which case
every off-diagonal entry would be identically 1.0 by construction.
See |
Notes
Every entry here is a pairwise coherence: channels i and j are compared
with the rest of the array ignored, so a pair that only looks related
because both follow a third channel looks exactly like a pair that is
directly related. partial_coherence is the same matrix with the other
channels conditioned out, and is the one to read when the array has more
than two sensors.
See Also
partial_coherence, multiple_coherence
Source code in dspkit/multisensor.py
46 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 | |
dspkit.multisensor.psd_matrix(data, fs, window='hann', nperseg=None, noverlap=None, detrend='constant')
Cross-spectral density matrix (power spectral density matrix).
Computes the full n_channels × n_channels CSD matrix at each frequency. This is the input required for Frequency Domain Decomposition (FDD).
The matrix is Hermitian at each frequency: G[i,j,f] = conj(G[j,i,f]).
Diagonal entries G[i,i,f] are real-valued (auto-PSD).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
(array_like, shape(n_channels, N))
|
Each row is a time series from one sensor. |
required |
fs
|
float
|
Sampling frequency [Hz]. |
required |
window
|
str
|
Window function (default |
'hann'
|
nperseg
|
int or None
|
Welch segment length. Defaults to |
None
|
noverlap
|
int or None
|
Overlap between segments. |
None
|
detrend
|
str or False
|
Per-segment detrending. |
'constant'
|
Returns:
| Name | Type | Description |
|---|---|---|
freqs |
(ndarray, shape(M))
|
Frequency vector [Hz]. |
G |
(ndarray, shape(n_channels, n_channels, M), complex)
|
Cross-spectral density matrix. |
Source code in dspkit/multisensor.py
dspkit.multisensor.multiple_coherence(data, fs, window='hann', nperseg=None, noverlap=None, detrend='constant', ridge=1e-10, min_segments=None)
Multiple coherence: each channel against all the others, per frequency.
gamma_i²(f) = 1 - 1 / (G_ii(f) · inv(G)(f)_ii) — the fraction of
channel i's power at frequency f that a linear combination of every
other channel accounts for. It is the frequency-domain coefficient of
determination of channel i regressed on the rest of the array, and it
answers "is this sensor redundant?" in the only form that question has a
defensible answer: as a curve, not a score.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
(array_like, shape(n_channels, N))
|
Each row is a time series from one sensor. |
required |
fs
|
float
|
Sampling frequency [Hz]. |
required |
window
|
str
|
Window function (default |
'hann'
|
nperseg
|
int or None
|
Welch segment length. Defaults to |
None
|
noverlap
|
int or None
|
Overlap between segments. Defaults to |
None
|
detrend
|
str or False
|
Per-segment detrending. |
'constant'
|
ridge
|
float
|
Ridge added to the unit diagonal of the normalised CSD matrix before inversion (default 1e-10). See Notes — this is a numerical floor, not a statistical shrinkage. |
1e-10
|
min_segments
|
int or None
|
Warn below this many Welch segments. Defaults to |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
freqs |
(ndarray, shape(M))
|
Frequency vector [Hz]. |
gamma2 |
(ndarray, shape(n_channels, M))
|
Multiple coherence of each channel against all the others, in [0, 1]. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the Welch parameters give |
Notes
Regularisation. The matrix is inverted with a ridge, not with
numpy.linalg.pinv. The reason is the case this function exists to
detect: when a channel is an exact linear combination of the others, G is
exactly rank-deficient, and the true multiple coherence is 1 — which
requires inv(G)_ii -> inf. A pseudo-inverse truncates precisely that
direction and returns a finite inv(G)_ii, small enough that the
formula goes negative (measured: -0.6 to -2.9 on three channels where
the third is the sum of the first two), which a clip to [0, 1] would then
present as 0.0 — the most redundant array that can exist, reported as
perfectly independent. A ridge keeps the limit intact and returns
1 - O(ridge). The default 1e-10 bounds the condition number at about
1e10, which float64 absorbs; raise it if the inverse looks noisy, at the
cost of biasing values away from 1 by roughly that amount.
The bias floor is the number to compare against, not zero. With
q = n_channels - 1 other channels and n_d Welch segments, the
expected multiple coherence of independent channels is about
q / n_d. Measured on four independent noise channels, N = 20480::
n_d = 3 mean 1.000 (q / n_d = 1.00, and G is singular here)
n_d = 4 mean 0.759 (0.75)
n_d = 9 mean 0.343 (0.33)
n_d = 19 mean 0.167 (0.16)
n_d = 39 mean 0.079 (0.077)
That one formula covers both the noise floor and the failure mode: it
reaches 1.0 exactly at n_d = q, which is where the matrix goes
singular, so "every coherence comes back at 1.0" is the same statement as
"the floor has risen to 1". At n_d = n_channels the matrix is
invertible again but the fit has no residual degrees of freedom, so the
values are still meaningless — hence the refusal at n_d <= n_channels
rather than only below it. This function reports the curve, not the excess
over the floor; the floor is yours to subtract, and min_segments warns
while it is large.
What this will not tell you: which of the other channels does the
explaining (use partial_coherence), whether the relationship is
causal, or whether a low value means independence or a nonlinear
relationship that no linear predictor can reach (see
dspkit.statistics.mutual_information). It is also blind to any
redundancy that only appears outside the analysis band, and it says
nothing about whether a redundant sensor is worth keeping — a duplicate
channel is redundant and also the only thing that will catch the other
one failing.
See Also
partial_coherence, coherence_matrix, psd_matrix
Examples:
>>> import numpy as np
>>> from dspkit.multisensor import multiple_coherence
>>> rng = np.random.default_rng(0)
>>> x, y, w = rng.normal(size=(3, 20000))
>>> data = np.vstack([x, y, x + y, w]) # channel 2 is x + y, channel 3 is not
>>> f, g2 = multiple_coherence(data, 1000.0, nperseg=256)
>>> bool(np.median(g2[2]) > 0.99), bool(np.median(g2[3]) < 0.1)
(True, True)
Source code in dspkit/multisensor.py
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 | |
dspkit.multisensor.partial_coherence(data, fs, window='hann', nperseg=None, noverlap=None, detrend='constant', ridge=1e-10, min_segments=None)
Partial coherence: pairwise, with every other channel conditioned out.
gamma_ij²(f) = |inv(G)_ij|² / (inv(G)_ii · inv(G)_jj) — the coherence
that survives after the linear contribution of the remaining channels has
been removed from both i and j. Where coherence_matrix cannot tell
a direct relationship from one mediated by a third sensor, this can: for a
chain x -> y -> z the ordinary coherence of x and z is high, while their
partial coherence given y falls to the noise floor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
(array_like, shape(n_channels, N))
|
Each row is a time series from one sensor. |
required |
fs
|
float
|
Sampling frequency [Hz]. |
required |
window
|
str
|
Window function (default |
'hann'
|
nperseg
|
int or None
|
Welch segment length. Defaults to |
None
|
noverlap
|
int or None
|
Overlap between segments. Defaults to |
None
|
detrend
|
str or False
|
Per-segment detrending. |
'constant'
|
ridge
|
float
|
Ridge added to the unit diagonal of the normalised CSD matrix before
inversion (default 1e-10). See |
1e-10
|
min_segments
|
int or None
|
Warn below this many Welch segments. Defaults to |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
freqs |
(ndarray, shape(M))
|
Frequency vector [Hz]. |
C |
(ndarray, shape(n_channels, n_channels, M))
|
Partial coherence matrix, mirroring |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the Welch parameters give |
Notes
With exactly two channels there is nothing to condition on, and this
reduces to coherence_matrix — a useful check, not a use case.
Conditioning is linear and simultaneous at each frequency: the other
channels are removed as a frequency-domain linear predictor, which is the
right operation for a linear time-invariant structure and the wrong one if
the mediation is nonlinear, or if the mediating channel was not measured.
An unmeasured common cause is invisible here, exactly as it is in ordinary
coherence — conditioning removes what is in data, and nothing else.
The bias floor applies here too: with n_d segments and n_ch - 2
conditioning channels, independent pairs sit near
1 / (n_d - n_ch + 2) rather than at 0. Conditioning also spends
degrees of freedom, so partial coherence is noisier than the pairwise
coherence it refines; the more channels are conditioned out, the more
segments are needed to see the same contrast.
See Also
multiple_coherence, coherence_matrix, psd_matrix
Examples:
>>> import numpy as np
>>> from dspkit.multisensor import partial_coherence
>>> rng = np.random.default_rng(0)
>>> x = rng.normal(size=20000)
>>> y = np.convolve(x, np.ones(8) / 8, "same") + 0.02 * rng.normal(size=20000)
>>> z = np.convolve(y, np.ones(8) / 8, "same") + 0.02 * rng.normal(size=20000)
>>> f, C = partial_coherence(np.vstack([x, y, z]), 1000.0, nperseg=256)
>>> bool(np.median(C[0, 2]) < 0.1) # x and z, given y
True
Source code in dspkit/multisensor.py
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 | |