pysmo.tools.signal
Functions used in signal processing.
Functions:
| Name | Description |
|---|---|
bandpass |
Apply a bandpass filter to the input seismogram. |
bandstop |
Apply a bandstop filter to the input seismogram. |
delay |
Cross correlates two seismograms to determine signal delay. |
envelope |
Calculates the envelope of a gaussian filtered seismogram. |
filter |
Apply a specified filter to the input seismogram. |
gauss |
Returns a gaussian filtered seismogram. |
highpass |
Apply a highpass filter to the input seismogram. |
lowpass |
Apply a lowpass filter to the input seismogram. |
mccc |
Multi-Channel Cross-Correlation (MCCC) for relative arrival times. |
multi_delay |
Calculates delays and correlation coefficients for a list of seismograms against a template. |
multi_multi_delay |
Calculates pairwise delays and correlation coefficients for a sequence of seismograms. |
bandpass
bandpass(
seismogram: T,
freqmin: float = 0.1,
freqmax: float = 0.5,
corners: int = 2,
zerophase: bool = False,
clone: bool = False,
) -> T | None
Apply a bandpass filter to the input seismogram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seismogram
|
T
|
The input seismogram to be filtered. |
required |
freqmin
|
float
|
The minimum frequency of the bandpass filter (in Hz). |
0.1
|
freqmax
|
float
|
The maximum frequency of the bandpass filter (in Hz). |
0.5
|
corners
|
int
|
The number of corners (poles) for the Butterworth filter. |
2
|
zerophase
|
bool
|
If True, apply the filter in both forward and reverse directions to achieve zero phase distortion. |
False
|
clone
|
bool
|
If True, return a new Seismogram object with the filtered data. If False, modify the input seismogram in place. |
False
|
Returns:
| Type | Description |
|---|---|
T | None
|
A new Seismogram object containing the filtered data when called with |
Source code in src/pysmo/tools/signal/_filter/_butter.py
bandstop
bandstop(
seismogram: T,
freqmin: float = 0.1,
freqmax: float = 0.5,
corners: int = 2,
zerophase: bool = False,
clone: bool = False,
) -> T | None
Apply a bandstop filter to the input seismogram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seismogram
|
T
|
The input seismogram to be filtered. |
required |
freqmin
|
float
|
The minimum frequency of the bandstop filter (in Hz). |
0.1
|
freqmax
|
float
|
The maximum frequency of the bandstop filter (in Hz). |
0.5
|
corners
|
int
|
The number of corners (poles) for the Butterworth filter. |
2
|
zerophase
|
bool
|
If True, apply the filter in both forward and reverse directions to achieve zero phase distortion. |
False
|
clone
|
bool
|
If True, return a new Seismogram object with the filtered data. If False, modify the input seismogram in place. |
False
|
Returns:
| Type | Description |
|---|---|
T | None
|
A new Seismogram object containing the filtered data when called with |
Source code in src/pysmo/tools/signal/_filter/_butter.py
delay
delay(
seismogram1: Seismogram,
seismogram2: Seismogram,
total_delay: bool = False,
max_shift: Timedelta | None = None,
abs_max: bool = False,
) -> tuple[Timedelta, float]
Cross correlates two seismograms to determine signal delay.
This function is a wrapper around the
scipy.signal.correlate function. The default
behaviour is to call the correlate function with mode="full" using
the full length data of the input seismograms. This is the most robust
option, but also the slowest.
If an approximate delay is known (e.g. because a particular phase is being
targeted using a computed arrival time), the search space can be limited
by setting the max_shift parameter to a value. The length of the
seismogram data used for the cross-correlation is then set such that the
calculated delay lies within +/- max_shift.
Note
max_shift intentionally does not take the begin times of the
seismograms into consideration. Thus, calling delay() with
total_delay=True may return a delay that is larger than
max_shift.
Implications of setting the max_shift parameter are as follows:
- This mode requires the seismograms to be of equal length.
- If the true delay (i.e. the amount of time the seismograms should be
shifted by) lies within the
max_shiftrange, and also produces the highest correlation, the delay time returned is identical for both methods. - If the true delay lies outside the
max_shiftrange and produces the highest correlation, the delay time returned will be incorrect whenmax_shiftis set. - In the event that the true delay lies within the
max_shiftrange but the maximum signal correlation occurs outside, it will be correctly retrieved when themax_shiftparameter is set, while not setting it yields an incorrect result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seismogram1
|
Seismogram
|
First seismogram to use for cross correlation. |
required |
seismogram2
|
Seismogram
|
Second seismogram to use for cross correlation. |
required |
total_delay
|
bool
|
Include the difference in |
False
|
max_shift
|
Timedelta | None
|
Maximum (absolute) length of the delay. |
None
|
abs_max
|
bool
|
Return the delay corresponding to absolute maximum. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
delay |
Timedelta
|
Time delay of the second seismogram with respect to the first. |
ccnorm |
float
|
Normalised cross correlation value of the overlapping
seismograms after shifting (uses
|
Examples:
To illustration the use of the delay() function, we read a seismogram
from a SAC file and then generate a second seismogram from it by
modifying it with a shift in the data and a shift in the begin time.
This means the true delay is known, and we are able to compare the
computed delays with this known delay:
>>> from pysmo import MiniSeismogram
>>> from pysmo.classes import SAC
>>> from pysmo.functions import detrend, clone_to_mini
>>> from pysmo.tools.signal import delay
>>> from datetime import timedelta
>>> import numpy as np
>>>
>>> # Create a Seismogram from a SAC file and detrend it:
>>> seis1 = SAC.from_file("example.sac").seismogram
>>> detrend(seis1)
>>>
>>> # Create a second seismogram from the first with
>>> # a different begin_time and a shift in the data:
>>> seis2 = clone_to_mini(MiniSeismogram, seis1)
>>> nroll = 1234
>>> seis2.data = np.roll(seis2.data, nroll)
>>> begin_time_delay = timedelta(seconds=100)
>>> seis2.begin_time += begin_time_delay
>>>
>>> # The signal delay is the number of samples shifted * delta:
>>> (signal_delay := nroll * seis1.delta).total_seconds()
24.68
>>>
>>> # Call the delay function with the two seismograms and verify
>>> # that the caclulated_delay is equal to the known signal delay:
>>> calculated_delay, _ = delay(seis1, seis2)
>>> calculated_delay == signal_delay
True
>>>
As we know what the true delay is, we can mimic a scenario where an
approximate delay is known prior to the cross-correlation, which can be
used to limit the search space and speed up the calculation. Here we
assign a value of the known signal delay plus 1 second to the
max_shift parameter:
>>> max_shift = signal_delay + timedelta(seconds=1)
>>> calculated_delay, _ = delay(seis1, seis2, max_shift=max_shift)
>>> # As before, the calculated delay should be equal to the signal delay:
>>> calculated_delay == signal_delay
True
>>>
Setting total_delay=True, we also takes into account the difference
in begin_time between the two seismograms:
>>> calculated_delay, _ = delay(seis1, seis2, total_delay=True, max_shift=signal_delay+timedelta(seconds=1))
>>> # With `total_delay=True`, the calculated delay should be equal to
>>> # the signal delay plus the begin time difference:
>>> calculated_delay == signal_delay + (seis1.begin_time - seis2.begin_time)
True
>>>
To demonstrate usage of the abs_max parameter, we flip the sign of
the second seismogram data:
>>> seis2.data *= -1
>>> calculated_delay, ccnorm = delay(seis1, seis2)
>>> # Without `abs_max=True`, the calculated delay is no longer equal
>>> # to the true signal delay (as expected):
>>> calculated_delay == signal_delay
False
>>> # The normalised cross correlation value is also not very high
>>> ccnorm
np.float64(0.4267205)
>>>
>>> calculated_delay, ccnorm = delay(seis1, seis2, abs_max=True)
>>> # with `abs_max=True`, the signal delay is again retrieved:
>>> calculated_delay == signal_delay
True
>>> # And, as the signals are completely opposite, the normalised
>>> # cross correlation value is -1:
>>> np.testing.assert_approx_equal(ccnorm, -1)
>>>
Source code in src/pysmo/tools/signal/_delay.py
29 30 31 32 33 34 35 36 37 38 39 40 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 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 | |
envelope
Calculates the envelope of a gaussian filtered seismogram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seismogram
|
T
|
Name of the seismogram object passed to this function. |
required |
Tn
|
float
|
Center period of Gaussian filter [in seconds] |
required |
alpha
|
float
|
Set alpha (which determines filterwidth) |
required |
clone
|
bool
|
If True, return a new Seismogram object with the filtered data. If False, modify the input seismogram in place. |
False
|
Returns:
| Type | Description |
|---|---|
T | None
|
Seismogram containing the envelope |
Examples:
>>> from pysmo.classes import SAC
>>> from pysmo.tools.signal import envelope
>>> seis = SAC.from_file("example.sac").seismogram
>>> Tn = 50 # Center Gaussian filter at 50s period
>>> alpha = 50 # Set alpha (which determines filterwidth) to 50
>>> envelope_seis = envelope(seis, Tn, alpha, clone=True)
>>>
Source code in src/pysmo/tools/signal/_filter/_gauss.py
filter
filter(
seismogram: T,
filter_name: FilterName,
clone: bool = False,
**filter_options: float | int | bool
) -> T | None
Apply a specified filter to the input seismogram.
This function is a convenience wrapper that calls other filters in this module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seismogram
|
T
|
The input seismogram to be filtered. |
required |
filter_name
|
FilterName
|
The type of filter to apply. |
required |
clone
|
bool
|
If True, return a new Seismogram object with the filtered data. If False, modify the input seismogram in place. |
False
|
**filter_options
|
float | int | bool
|
Filter parameters passed to the specified filter function. |
{}
|
Returns:
| Type | Description |
|---|---|
T | None
|
A new Seismogram object containing the filtered data when called with |
Examples:
>>> from pysmo.classes import SAC
>>> from pysmo.tools.signal import filter
>>> seis = SAC.from_file("example.sac").seismogram
>>>
>>> # create a new filtered seismogram with a lowpass filter
>>> filtered_seis = filter(seis, "lowpass", freqmax=0.5, clone=True)
>>>
>>> # or update in place with a bandpass filter
>>> filter(seis, "bandpass", freqmin=0.1, freqmax=0.5)
>>>
Source code in src/pysmo/tools/signal/_filter/_filter.py
gauss
Returns a gaussian filtered seismogram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seismogram
|
T
|
Name of the SAC object passed to this function. |
required |
Tn
|
float
|
Center period of Gaussian filter [in seconds] |
required |
alpha
|
float
|
Set alpha (which determines filterwidth) |
required |
Returns:
| Type | Description |
|---|---|
T | None
|
Gaussian filtered seismogram. |
Examples:
>>> from pysmo.classes import SAC
>>> from pysmo.tools.signal import gauss
>>> seis = SAC.from_file("example.sac").seismogram
>>> Tn = 50 # Center Gaussian filter at 50s period
>>> alpha = 50 # Set alpha (which determines filterwidth) to 50
>>> gauss_seis = gauss(seis, Tn, alpha, clone=True)
>>>
Source code in src/pysmo/tools/signal/_filter/_gauss.py
highpass
highpass(
seismogram: T,
freqmin: float = 0.1,
corners: int = 2,
zerophase: bool = False,
clone: bool = False,
) -> T | None
Apply a highpass filter to the input seismogram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seismogram
|
T
|
The input seismogram to be filtered. |
required |
freqmin
|
float
|
The minimum frequency of the highpass filter (in Hz). |
0.1
|
corners
|
int
|
The number of corners (poles) for the Butterworth filter. |
2
|
zerophase
|
bool
|
If True, apply the filter in both forward and reverse directions to achieve zero phase distortion. |
False
|
clone
|
bool
|
If True, return a new Seismogram object with the filtered data. If False, modify the input seismogram in place. |
False
|
Returns:
| Type | Description |
|---|---|
T | None
|
A new Seismogram object containing the filtered data when called with |
Source code in src/pysmo/tools/signal/_filter/_butter.py
lowpass
lowpass(
seismogram: T,
freqmax: float = 0.5,
corners: int = 2,
zerophase: bool = False,
clone: bool = False,
) -> T | None
Apply a lowpass filter to the input seismogram.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seismogram
|
T
|
The input seismogram to be filtered. |
required |
freqmax
|
float
|
The maximum frequency of the lowpass filter (in Hz). |
0.5
|
corners
|
int
|
The number of corners (poles) for the Butterworth filter. |
2
|
zerophase
|
bool
|
If True, apply the filter in both forward and reverse directions to achieve zero phase distortion. |
False
|
clone
|
bool
|
If True, return a new Seismogram object with the filtered data. If False, modify the input seismogram in place. |
False
|
Returns:
| Type | Description |
|---|---|
T | None
|
A new Seismogram object containing the filtered data when called with |
Source code in src/pysmo/tools/signal/_filter/_butter.py
mccc
mccc(
seismograms: Sequence[Seismogram],
min_cc: float = 0.5,
damping: float = 0.1,
) -> tuple[list[Timedelta], list[Timedelta], Timedelta]
Multi-Channel Cross-Correlation (MCCC) for relative arrival times.
Computes all pairwise cross-correlation delays using
multi_multi_delay, then solves
for self-consistent relative time shifts using a weighted least-squares
inversion with a zero-mean constraint and Tikhonov regularization. Pairs
whose correlation coefficient falls below min_cc are excluded from the
inversion.
The returned times list sums to zero by construction, so the values
represent relative shifts around the group mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seismograms
|
Sequence[Seismogram]
|
Sequence of Seismogram objects. All must share the same sampling interval. |
required |
min_cc
|
float
|
Minimum correlation coefficient required to include a pair in the inversion. |
0.5
|
damping
|
float
|
Tikhonov regularization strength. Set to 0 to disable. |
0.1
|
Returns:
| Name | Type | Description |
|---|---|---|
times |
list[Timedelta]
|
List of relative arrival times. |
errors |
list[Timedelta]
|
List of standard errors. |
rmse |
Timedelta
|
Root-mean-square error of the fit. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any seismogram has a different sampling rate than the
others (raised by |
Examples:
Create seismograms with known shifts and recover them with mccc:
>>> from pysmo import MiniSeismogram
>>> from pysmo.tools.signal import mccc
>>> import numpy as np
>>>
>>> # Build three seismograms with known shifts (in samples):
>>> data = np.sin(np.linspace(0, 8 * np.pi, 1000))
>>> shifts = [0, 5, -10]
>>> seismograms = [MiniSeismogram(data=np.roll(data, s)) for s in shifts]
>>>
>>> # Run MCCC inversion:
>>> times, errors, rmse = mccc(seismograms)
>>>
>>> # The relative times sum to approximately zero:
>>> abs(sum(t.total_seconds() for t in times)) < 1e-5
True
>>>
>>> # Pairwise differences recover the known shifts
>>> # (times[i] - times[j] ≈ (shifts[j] - shifts[i]) * delta):
>>> round((times[1] - times[0]).total_seconds())
-5
>>> round((times[2] - times[0]).total_seconds())
10
>>>
References
VanDecar, J. C., and R. S. Crosson. “Determination of Teleseismic Relative Phase Arrival Times Using Multi-Channel Cross-Correlation and Least Squares.” Bulletin of the Seismological Society of America, vol. 80, no. 1, Feb. 1990, pp. 150–69, https://doi.org/10.1785/BSSA0800010150.
Source code in src/pysmo/tools/signal/_delay.py
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 | |
multi_delay
multi_delay(
template: Seismogram,
seismograms: Sequence[Seismogram],
abs_max: bool = False,
) -> tuple[list[Timedelta], list[float]]
Calculates delays and correlation coefficients for a list of seismograms against a template.
This function uses FFT-based cross-correlation to efficiently compute delays
for multiple seismograms at once against a single template. This is faster
than calling delay in a loop, as the template
FFT is computed only once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template
|
Seismogram
|
Template seismogram object. |
required |
seismograms
|
Sequence[Seismogram]
|
Sequence of Seismogram objects. |
required |
abs_max
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
delays |
list[Timedelta]
|
Delays of each input seismogram relative to template. |
coeffs |
list[float]
|
Correlation coefficients at maximum correlation for each seismogram. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any seismogram has a different sampling rate than the template. |
Examples:
Create a template seismogram and several shifted copies, then use
multi_delay to recover the known shifts:
>>> from pysmo import MiniSeismogram
>>> from pysmo.tools.signal import multi_delay
>>> import numpy as np
>>>
>>> # Create a template seismogram with sinusoidal data:
>>> data = np.sin(np.linspace(0, 8 * np.pi, 1000))
>>> template = MiniSeismogram(data=data)
>>>
>>> # Create shifted copies (shifts in samples):
>>> shifts = [0, 10, -5]
>>> seismograms = [MiniSeismogram(data=np.roll(data, s)) for s in shifts]
>>>
>>> # Calculate delays for all seismograms at once:
>>> delays, coeffs = multi_delay(template, seismograms)
>>> [d.total_seconds() for d in delays]
[0.0, 10.0, -5.0]
>>>
Use abs_max=True for polarity-insensitive matching:
>>> flipped = MiniSeismogram(data=-np.roll(data, 10))
>>> delays, coeffs = multi_delay(template, [flipped], abs_max=True)
>>> delays[0].total_seconds()
10.0
>>> coeffs[0] < 0
True
>>>
Source code in src/pysmo/tools/signal/_delay.py
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 | |
multi_multi_delay
multi_multi_delay(
seismograms: Sequence[Seismogram], abs_max: bool = False
) -> tuple[NDArray[timedelta64], NDArray[floating]]
Calculates pairwise delays and correlation coefficients for a sequence of seismograms.
This function cross-correlates every seismogram with every other seismogram
in the sequence using FFT-based cross-correlation. All FFTs are computed once
and combined via broadcasting, making this significantly faster than calling
delay for each pair individually.
The result at delays[i, j] is the delay of seismogram j relative to
seismogram i (treating i as the reference). The delay matrix is
antisymmetric: delays[i, j] == -delays[j, i], and the diagonal is zero.
Note
Unlike most pysmo functions, this returns numpy.timedelta64 values
rather than Timedelta. This avoids the overhead
of an object array and allows efficient vectorized arithmetic in
downstream functions such as mccc.
Convert individual elements with pandas.Timedelta(delays[i, j]) if
needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seismograms
|
Sequence[Seismogram]
|
Sequence of Seismogram objects. |
required |
abs_max
|
bool
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
delays |
NDArray[timedelta64]
|
2D array of shape |
coeffs |
NDArray[floating]
|
2D array of shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any seismogram has a different sampling rate than the others. |
Examples:
Create seismograms with known shifts and compute all pairwise delays:
>>> from pysmo import MiniSeismogram
>>> from pysmo.tools.signal import multi_multi_delay
>>> from pandas import Timedelta
>>> import numpy as np
>>>
>>> data = np.sin(np.linspace(0, 8 * np.pi, 1000))
>>> seismograms = [
... MiniSeismogram(data=data.copy()),
... MiniSeismogram(data=np.roll(data, 5)),
... MiniSeismogram(data=np.roll(data, -10)),
... ]
>>>
>>> delays, coeffs = multi_multi_delay(seismograms)
>>> delays.shape
(3, 3)
>>> # delay of seismogram 1 relative to seismogram 0:
>>> Timedelta(delays[0, 1]).total_seconds()
5.0
>>> # delay of seismogram 2 relative to seismogram 0:
>>> Timedelta(delays[0, 2]).total_seconds()
-10.0
>>> # antisymmetric: delays[i, j] == -delays[j, i]
>>> Timedelta(delays[1, 0]).total_seconds()
-5.0
>>>
Source code in src/pysmo/tools/signal/_delay.py
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 | |