filter.py
#
Utility functions to filter (multidimensional) data.
moving_window(a, window_length, step_size=1)
#
Create moving windows of given window length over input array (as view).
Parameters:
-
a
(ndarray
) –1D input array.
-
window_length
(int
) –Length of moving window.
-
step_size
(int
, default:1
) –Step size of moving window (default: 1).
Returns:
-
view
(ndarray
) –View of array according to
window_length
andstep_size
.
References#
Source code in pseudo_3D_interpolation\functions\filter.py
moving_average(a, win=3)
#
Apply simple non-weighted moving average.
Parameters:
-
a
(ndarray
) –1D input data.
-
win
(int
, default:3
) –Number of data points within moving window (default:
3
).
Returns:
-
ndarray
–Moving average of input data.
Reference#
Source code in pseudo_3D_interpolation\functions\filter.py
moving_average_convolve(x, win=3)
#
Compute moving average of array with given window length.
Parameters:
-
x
(array
) –1D input data.
-
win
(int
, default:3
) –Number of data points within moving window (default:
3
).
Returns:
-
array
–Moving average of input data.
Source code in pseudo_3D_interpolation\functions\filter.py
moving_median(a, win=3, padded=False)
#
Apply moving median of given window size. Optional padding of input array using half the window size to avoid edge effects.
Parameters:
-
a
(ndarray
) –Input data (1D).
-
win
(int
, default:3
) –Number of data points within moving window (default:
3
). -
padded
(bool
, default:False
) –Pad start and end of array (default:
False
).
Returns:
-
ndarray
–Moving median of input data.
Source code in pseudo_3D_interpolation\functions\filter.py
moving_window_2D(a, w, dx=1, dy=1, writeable=False)
#
Create an array of moving windows (as view) into the input array using given step sizes in both dimensions.
Parameters:
-
a
(ndarray
) –2D input array.
-
w
(tuple
) –Moving window shape.
-
dx
(int
, default:1
) –Horizontal step size (columns, e.g. traces) (default: 1).
-
dy
(int
, default:1
) –vertical step size (rows, e.g. time samples) (default: 1).
-
writeable
(bool
, default:False
) –Set if view should be writeable (default: False). Use with care!
Returns:
-
view
(ndarray
) –4D array representing view of input array.
References#
Source code in pseudo_3D_interpolation\functions\filter.py
median_abs_deviation(x, axis=-1)
#
Return the median absolute deviation (MAD) from given input array.
Parameters:
-
x
(ndarray
) –Input array.
Returns:
-
mad
(ndarray
) –Median absolute deviation (MAD) of input array.
Source code in pseudo_3D_interpolation\functions\filter.py
median_abs_deviation_double(x, axis=-1)
#
Return the median absolute deviation (MAD) for unsymmetric distributions. Computes the deviation from median for both sides (left & right).
Parameters:
-
x
(ndarray
) –Input array.
-
axis
(TYPE
, default:-1
) –Axis to compute median on (default: -1).
Returns:
-
mad
(ndarray
) –Median absolute deviation (MAD) of input array.
References#
Source code in pseudo_3D_interpolation\functions\filter.py
smooth(data, window_len=11, window='hanning')
#
Smooth the data using a window with requested size.
This method is based on the convolution of a scaled window with the signal. The signal is prepared by introducing reflected copies of the signal (with the window size) in both ends so that transient parts are minimized in the begining and end part of the output signal.
Parameters:
-
data
(ndarray
) –1D input data array.
-
window_len
(int
, default:11
) –Input window length, should be odd integer (default: 11).
-
window
(str
, default:'hanning'
) –Tpye of smoothing window function (default: 'hanning').
Returns:
-
out
–smoothed input data
References#
Source code in pseudo_3D_interpolation\functions\filter.py
zscore_filter(data, axis=-1)
#
Z-score filter for outlier detection. Return array of outlier indices.
Source code in pseudo_3D_interpolation\functions\filter.py
moving_zscore_filter(data, win, axis=-1)
#
Return array of outlier indices using moving z-score filter for outlier detection of length win
.
Source code in pseudo_3D_interpolation\functions\filter.py
iqr_filter(a, axis=-1)
#
Inter-quartile range (IQR) filter for outlier detection. Return array of outlier indices.
Source code in pseudo_3D_interpolation\functions\filter.py
mad_filter(a, threshold=3, axis=-1, mad_mode='single')
#
Median Absolute Deviation (MAD) filter. Return array of outlier indices.
Source code in pseudo_3D_interpolation\functions\filter.py
moving_mad_filter(a, win, threshold=3, axis=-1, mad_mode='single')
#
Moving Median Absolute Deviation (MAD) filter of length win
. Return array of outlier indices.
Source code in pseudo_3D_interpolation\functions\filter.py
polynominal_filter(data, order=3, kind='high')
#
Apply polynominal filter to input data.
Parameters:
-
data
(ndarray
) –Input data.
-
order
(int
, default:3
) –Filter order (default:
3
). -
kind
(str
, default:'high'
) –Filter kind (default:
high
).
Returns:
-
data
(ndarray
) –Filtered input data.
Source code in pseudo_3D_interpolation\functions\filter.py
filter_interp_1d(data, method='IQR', kind='cubic', win=11, threshold=3.0, filter_boundaries=True)
#
Remove outliers using the IQR (inter-quartile range) method and
interpolate using user-specified kind
(default: 'cubic').
Return outlier-removed and interpolated input array.
Parameters:
-
data
(ndarray
) –Input data (1D).
-
method
(str
, default:'IQR'
) –Filter method to use (default:
IQR
). -
kind
(str
, default:'cubic'
) –Interpolation method for scipy.interpolate.interp1d (default:
cubic
). -
win
(int
, default:11
) –Size of moving window if required by chosen method (default:
11
). -
threshold
(float
, default:3.0
) –Threshold used for median absolute deviation (MAD) (default:
3.0
). -
filter_boundaries
(bool
, default:True
) –Filter flagged outlier indices at start and end of input array to avoid edge effects (if present despite padding) (default:
True
).
Returns:
-
data_interp
(ndarray
) –Filtered and interpolated data.
Source code in pseudo_3D_interpolation\functions\filter.py
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 |
|
sta_lta_filter(a, nsta, nlta, axis=-1)
#
Compute the STA/LTA ratio (short-time-average / longe-time-average) by continuously calculating the average values of the absolute amplitude of a seismic trace in two consecutive moving-time windows.
Parameters:
-
a
(ndarray
) –Seismic trace (1D) or section (2D).
-
nsta
(int
) –Length of short time average window (samples).
-
nlta
(int
) –Length of long time average window (samples).
-
axis
(int
, default:-1
) –Axis for which to compute STA/LTA ratio (default: -1).
Returns:
-
ndarray
–Either 1D or 2D array of STA/LTA ratio (per trace).
References#
-
Withers et al. (1998) A comparison of select trigger algorithms for automated global seismic phase and event detection, http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.116.245&rep=rep1&type=pdf ↩
-
Trnkoczy, A. (2012) Understanding and parameter setting of STA/LTA trigger algorithm, https://gfzpublic.gfz-potsdam.de/rest/items/item_4097_3/component/file_4098/content ↩
-
ObsPy, https://docs.obspy.org/_modules/obspy/signal/trigger.html#classic_sta_lta_py ↩
Source code in pseudo_3D_interpolation\functions\filter.py
detect_seafloor_reflection(data, idx_slice_start=None, nsta=None, nlta=None, win=30, threshold=None, win_mad=None, win_mad_post=None, win_median=11, n=5, post_detection_filter=True)
#
Detect seafloor reflection using the STA/LTA algorithm. Its commonly applied in seismology that evaluates the ratio of short- and long-term energy density. The initially sample indices found by the STA/LTA algorithm are used to create individual search windows per trace (idx - win <= x <= idx + win). Return indices of maximum amplitude(s) within individual search windows (shape: (ntraces,)).
Parameters:
-
data
(ndarray
) –Input seismic section (samples x traces).
-
idx_slice_start
(ndarray
, default:None
) –Index of first non-padded sample in original data.
-
nsta
(int
, default:None
) –Length of short time average window (in samples). If
None
: 0.1% of total samples. -
nlta
(int
, default:None
) –Length of long time average window (in samples). If
None
: 5% of total samples. -
win
(int
, default:30
) –Number of samples to pad search window with (default:
30
). Set search window towin
samples deeper andwin
x 2 samples shallower than baseline. -
threshold
(float
, default:None
) –Threshold for seafloor amplitude detection after STA/LTA computation. If None, using background STA/LTA amplitudes from water column (default).
-
win_mad
(int
, default:None
) –Number of traces used for Median Absolute Deviation (MAD) filtering. If None (default), this window is set to 5% of total traces.
-
win_mad_post
(int
, default:None
) –Number of traces used for Median Absolute Deviation (MAD) filtering (after detection). If None (default), this window is set to 1% of total traces.
-
win_median
(int
, default:11
) –Number of traces for rolling median filter, should be odd integer (default:
11
). -
n
(int
, default:5
) –Number of n hightest amplitudes for each trace (default:
5
). -
post_detection_filter
(bool
, default:True
) –Apply optional Median Absolute Deviation (MAD) filtering after actual seafloor detection (default:
True
).
Returns:
-
ndarray
–Indices of samples at maximum amplitude (per trace).
Source code in pseudo_3D_interpolation\functions\filter.py
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 |
|
butterworth_filter(data, btype, cutoff, fs, order=9, axis=-1)
#
Apply butterworth filter to input signal. Can be lowpass
, highpass
, or bandpass
.
Parameters:
-
data
(ndarray
) –Input data.
-
cutoff
(float | tuple
) –Cutoff frequency (in Hz).
-
fs
(float
) –Sampling frequency (in Hz).
-
order
(int
, default:9
) –Butterworth filter order (default: 9).
-
axis
(int
, default:-1
) –The axis of x to which the filter is applied (default: -1).
Returns:
-
y
(ndarray
) –Filtered input signal.
References#
Source code in pseudo_3D_interpolation\functions\filter.py
filter_frequency(data, freqs, fs, filter_type, gpass=1, gstop=10, axis=-1)
#
Apply freqeuncy filter by specifing passband and stopband frequencies. Possible filter types:
bandpass
: freqs = [f1, f2, f3, f4]lowpass
: freqs = [f_stopband, f_cutoff]highpass
: freqs = [f_cutoff, f_stopband]
Parameters:
-
data
(ndarray
) –Input data.
-
freqs
(list
) –List of frequencies defining filter (same unit as
fs
!). -
fs
(float
) –Sampling frequency (in Hz).
-
filter_type
(str
) –Filter type to apply (
bandpass
,lowpass
,highpass
) -
gpass
(int
, default:1
) –The maximum loss in the passband (dB).
-
gstop
(int
, default:10
) –The minimum attenuation in the stopband (dB).
-
axis
(int
, default:-1
) –The axis of x to which the filter is applied (default:
-1
).
Returns:
-
ndarray
–Filtered input data.