Data Processing
Supported processing operations are described below.
In most cases, a "safe" version of each function can be invoked to create a new object with the processed output.
Any function that can logically operate on a single-channel object will do so. Any function that operates on a SeisData object can be applied to the :data field of a SeisEvent object.
Basic Operations
These functions have no keywords that fundamentally change their behavior.
SeisBase.demean!
— Functiondemean!(S::SeisData[; chans=CC, irr=false])
Remove the mean from all channels i
with S.fs[i] > 0.0
. Specify irr=true
to also remove the mean from irregularly sampled channels (with S.fs[i] == 0.0). Specifying a channel list with chans=CC
restricts processing to channels CC.
demean!(C::SeisChannel)
Remove the mean from data in C
.
Ignores NaNs.
SeisBase.detrend!
— Functiondetrend!(S::SeisData[; chans=CC, n=1]))
Remove the linear trend from channels CC
. Ignores NaNs.
To remove a higher-order polynomial fit than a linear trend, choose n
>1.
detrend!(C::SeisChanel[; n=1]))
Remove the linear trend from data in C
. Ignores NaNs.
To remove a higher-order polynomial fit than a linear trend, choose n>1.
detrend! does not check for data gaps; if this is problematic, call ungap!(S, m=true) first!
SeisBase.env!
— Functionenv!(S::GphysData[, chans=CC, v=V])
env(S::GphysData)
Compute the envelope of channel data in S. Only affects regularly-sampled channels.
In-place conversion of S.x[i] ==> Env(S.x[i]) (≡ |H(S.x[i])|, where H denotes the Hilbert transform).
Keywords
- chans=CC: only process channels in CC (with fs > 0.0).
- v=V: verbosity.
SeisBase.nanfill!
— Functionnanfill!(S::SeisData)
nanfill!(C::SeisChannel)
For each channel i
in S
, replace all NaNs in S.x[i]
with the mean of non-NaN values.
SeisBase.resample!
— Functionresample!(S::SeisData [, chans=CC, fs=FS])
resample(S::SeisData [, chans=CC, fs=FS])
Resample data in S to FS
. If keyword fs
is not specified, data are resampled to the lowest non-zero value in S.fs[CC]
.Note that a poor choice of FS
can lead to upsampling and other undesirable behavior.
Use keyword chans=CC
to only resample channel numbers CC
. By default, all channels i
with S.fs[i] > 0.0
are resampled.
resample!(C::SeisChannel, fs::Float64)
resample(C::SeisChannel, fs::Float64)
Resample C.x
to fs
.
SeisBase.unscale!
— Functionunscale!(S::GphysData[, chans=CC, irr=false])
Divide out the gains of all channels i
where S.fs[i] > 0.0
. Specify irr=true
to also remove the gains of irregularly-sampled channels. Use keyword chans=CC
to only resample channel numbers CC
.
Customizable Operations
Convert Seismograms
Seismograms can be converted to or from displacement, velocity, or acceleration using convert_seis:
SeisBase.convert_seis!
— Functionconvert_seis!(S[, chans=CC, units_out=UU, v=V])
convert_seis(S, chans=CC, units_out=UU, v=V])
convert_seis!(C[, units_out=UU, v=V])
convert_seis(CC, units_out=UU, v=V)
Convert all seismic data channels in S
to velocity seismograms, differentiating or integrating as needed.
Keywords
units_out=UU
specifies output units.- Default: "m/s".
- Allowed: "m", "m/s", or "m/s2". (SeisBase uses Unicode (UTF-8) UCUM units.)
v=V
sets verbosity.chans=CC
restricts seismogram conversion to seismic data channels withinCC
.chans
can be an Integer, UnitRange, or Array{Int64,1}.- By default, all seismic data channels in
S
are converted (if needed). - This does not allow
convert_seis!
to work on non-seismic data.
convert_seis!
becomes less reversible as seismograms lengthen, particularly at Float32
precision, due to loss of significance. At single (Float32
) precision, seismograms with N ~ 10^6
samples are reconstructable after one conversion (e.g. "m" ==> "m/s"
can be reversed, with output approximately equal to the original data). After multiple conversions (i.e., "m" ==> "m/s²"
or "m/s²" ==> "m"
), Float32
data cannot be perfectly reconstructed in this way, though reconstruction errors are typically small.
Integration is always rectangular; irregularly-spaced seismic data are not processed by convert_seis. Summation uses an in-place variant of Kahan-Babuška-Neumaier summation
References
- Neumaier, A. (1974). "Rundungsfehleranalyse einiger Verfahren zur Summation endlicher Summen" [Rounding Error Analysis of Some Methods for Summing Finite Sums]. Zeitschrift für Angewandte Mathematik und Mechanik (in German). 54 (1): 39-51. doi:10.1002/zamm.19740540106.
Fill Gaps
SeisBase.ungap!
— Functionungap!(S[, chans=CC, m=true, tap=false])
ungap(S[, chans=CC, m=true, tap=false])
Fill time gaps in each channel of S with the mean of the channel data.
ungap!(C[, m=true, tap=false])
ungap(C[, m=true, tap=false])
As above for GphysChannel object C.
Keywords
chans=CC
: only ungap channelsCC
.m=false
: this flag fills gaps with NaNs instead of the mean.tap=true
: taper data before filling gaps.
If channel segments aren't in chronological order, call merge
before using ungap
.
Merge
Base.merge!
— Functionmerge!(S::SeisData, U::SeisData[, prune_only=true])
Merge channels of two SeisData structures.
Merge two GphysData structures. For timeseries data, a single-pass merge-and-prune operation is applied to value pairs whose sample times are separated by less than half the sampling interval.
merge!(S::SeisData[, prune_only=true])
"Flatten" a SeisData structure by merging channels with identical properties.
If prune_only=true
, the only action taken is deletion of empty and duplicate channels; merge!(S, U, prune_only=true)
is identical to an in-place S+U
.
Merge Behavior
Which channels merge?
- Channels merge if they have identical values for $:id$, $:fs$, $:loc$, $:resp$, and $:units$.
- An unset $:loc$, $:resp$, or $:units$ field matches any set value in the corresponding field of another channel.
What happens to merged fields?
- The essential properties above are preserved.
- Other fields are combined.
- Merged channels with different
:name
values use the name of the channel with the latest data before the merge; other names are logged to:notes
.
What does $merge!$ resolve?
Issue | Resolution |
---|---|
Empty channels | Delete |
Duplicated channels | Delete duplicate channels |
Duplicated windows in channel(s) | Delete duplicate windows |
Multiple channels, same properties [1 ] | Merge to a single channel |
Channel with out-of-order time windows | Sort in chronological order |
Overlapping windows, identical data, time-aligned | Windows merged |
Overlapping windows, identical data, small time offset [2 ] | Time offset corrected, windows merged |
Overlapping windows, non-identical data | Samples averaged, windows merged |
- [
1
]: "Properties" here are $:id$, $:fs$, $:loc$, $:resp$, and $:units$. - [
2
]: Data offset >4 sample intervals are treated as overlapping and non-identical.
When SeisBase Won't Merge SeisBase does not combine data channels if any of the five fields above are non-empty and different. For example, if a GphysData object S contains two channels, each with id "XX.FOO..BHZ", but one has fs=100 Hz and the other fs=50 Hz, merge! does nothing.
It's best to merge only unprocessed data. Data segments that were processed independently (e.g. detrended) will be averaged pointwise when merged, which can easily leave data in an unusuable state.
SeisBase.mseis!
— Functionmseis!(S::SeisData, U...)
Merge multiple SeisData structures at once. The first argument (merge target) must be a SeisData structure. Subsequent structures can be any type T <: Union{GphysData, GphysChannel, SeisEvent}.
mseis!(C::GphysChannel, U...)
Merge all channels in U that match channel C into object C. To be merged, a channel must match on fields :id
, :fs
, :loc
, :resp
, :units
.
See also: merge!
Seismic Instrument Response
SeisBase.translate_resp!
— Functiontranslate_resp!(S, resp_new[, chans=CC, wl=γ])
translate_resp(S, resp_new[, chans=CC, wl=γ])
Translate the instrument response of seismic data channels CC
in S
to resp_new
. Replaces field :resp
with resp_new
for all affected channels.
remove_resp!(S, chans=CC, wl=γ])
remove_resp(S, chans=CC, wl=γ])
Remove (flatten to DC) the instrument response of seismic data channels cha
in S
. Replaces field :resp
with the appropriate (all-pass) response.
translate_resp!(Ch, resp_new[, wl=γ])
translate_resp(Ch, resp_new[, wl=γ])
Translate the instrument response of seismic data in SeisChannel object Ch
to resp_new
. Replaces field :resp
with resp_new
.
remove_resp!(Ch[, chans=CC, wl=γ])
remove_resp(Ch[, chans=CC, wl=γ])
Remove (flatten to DC) the instrument response of seismic data in Ch
. Replaces field :resp
with the appropriate (all-pass) response.
Keywords
- chans=CC restricts response translation to channel(s)
CC
. By default, all seismic data channels have responses translated toresp_new
. - wl=γ sets the waterlevel to γ (default:
γ
= eps(Float32) ≈ ~1f-7)
The waterlevel is the minimum magnitude (absolute value) of the normalized old frequency response; in other words, if the old frequency response has a maximum magnitude of 1.0, then no response coefficient can be lower than g. This is useful to prevent "divide by zero" errors, but setting it too high will cause errors.
Interaction with the :resp field
translate_resp
and remove_resp
only work on a channel i
that satisfies S.resp[i] <: PZResp, PZResp64, MultiStageResp
. In the last case, S.resp[i].stage[1]
must be a PZResp or PZResp64, only the first stage of the response is changed, and the stage gain is ignored; instead, the sensitivity S.resp[i].stage[1].a0
is used.
Poles and zeros should be rad/s
Always check when loading from an unsupported data format. Responses read from station XML are corrected to rad/s automatically (most use rad/s); responses read from a SACPZ or SEED RESP file already use rad/s.
Response translation doesn't guarantee causality; if this is a problem, detrend and taper first!
Precision and Memory Optimization
To optimize speed and memory use, instrument response translation maps data to Complex{Float32} before translation; thus, with Float64 data, there can be minor rounding errors.
Instrument responses are also memory-intensive. The minimum memory consumption to translate the response of a gapless Float32 SeisChannel object is ~7x the size of the object itself.
More precisely, for an object S (of Type <: GphysData or GphysChannel), translation requires memory ~ 2 kB + the greater of (7x the size of the longest Float32 segment, or 3.5x the size of the longest Float64 segment). Translation uses four vectors – three complex and one real – that are updated and dynamically resized as the algorithm loops over each segment:
- Old response container: Array{Complex{Float32,1}}(undef, Nx)
- New response container: Array{Complex{Float32,1}}(undef, Nx)
- Complex data container: Array{Complex{Float32,1}}(undef, Nx)
- Real frequencies for FFT: Array{Float32,1}(undef, Nx)
...where Nx is the number of samples in the longest segment in S.
Causality
Response translation adds no additional processing to guarantee causality. At a minimum, most users will want to call $detrend!$ and $taper!$ before translating instrument responses.
Synchronize
SeisBase.sync!
— Functionsync!(S::GphysData)
Synchronize the start times of all data in S to begin at or after the last start time in S.
sync!(S[, s=TS, t=TT, pad=false, v=V])
Synchronize all data in S to start no earlier than TS
and terminate no later than TT
, with verbosity level V
.
By default, a channel with mean μᵢ = mean(S.x[i])
that begins after TS
is prepended with μᵢ
to begin exactly at TS
; similarly, if keyword t
is used, μᵢ
is appended so that data ends at TT
. If pad=false
, channels that begin after TS
or end before TT
are not extended in either direction.
For regularly-sampled channels, gaps between the specified and true times are filled with the mean; this isn't possible with irregularly-sampled data.
Specifying start time (s=
)
- s="last": (Default) sync to the last start time of any channel in
S
. - s="first": sync to the first start time of any channel in
S
. - A numeric value is treated as an epoch time (
?time
for details). - A DateTime is treated as a DateTime. (see Dates.DateTime for details.)
- Any string other than "last" or "first" is parsed as a DateTime.
Specifying end time (t=
)
- t="none": (Default) end times are not synchronized.
- t="last": synchronize all channels to end at the last end time in
S
. - t="first" synchronize to the first end time in
S
. - numeric, datetime, and non-reserved strings are treated as for
s=
.
See also: TimeSpec
, Dates.DateTime
, parsetimewin
sync!
calls prune!
; empty channels will be deleted.
Taper
SeisBase.taper!
— Functiontaper!(C[; t_max::Real=10.0, α::Real=0.05, N_min::Int64=10])
Cosine taper all time-series data in C. Tapers each segment of each channel that contains at least N_min
total samples.
taper!(S[; chans=CC, t_max::Real=10.0, α::Real=0.05, N_min::Int64=10])
Cosine taper each segment of time-series data in GphysChannel object C that contains at least N_min
total samples.
Does not modify irregularly-sampled data channels.
Keywords:
chans
: Only taper the specified channels.N_min
: Data segments with N < N_min total samples are not tapered.t_max
: Maximum taper edge in seconds.α
: Taper edge area; as for a Tukey window, the first and last 100*α% of
samples in each window are tapered, up to t_max
seconds of data.
See also: DSP.Windows.tukey
Zero-Phase Filter
SeisBase.filtfilt!
— Functionfiltfilt!(S::GphysData[; KWs])
Apply zero-phase filter to S.x.
filtfilt!(C::GphysChannel[; KWs])
Apply zero-phase filter to C.x
Keywords control filtering behavior; specify as e.g. filtfilt!(S, fl=0.1, np=2, rt="Lowpass").
Keywords
Name | Default | Type | Description |
---|---|---|---|
chans | (all) | [1] | channel numbers to filter |
fl | 1.0 | Float64 | lower corner frequency [Hz] [2] |
fh | 15.0 | Float64 | upper corner frequency [Hz] [2] |
np | 4 | Int64 | number of poles |
rp | 10 | Int64 | pass-band ripple (dB) |
rs | 30 | Int64 | stop-band ripple (dB) |
rt | "Bandpass" | String | response type (type of filter) |
dm | "Butterworth" | String | design mode (name of filter) |
- Allowed types are Integer, UnitRange, and Array{Int64, 1}.
- By convention, the lower corner frequency (fl) is used in a Highpass
filter, and fh is used in a Lowpass filter.
Default filtering KW values can be changed by adjusting the Shared Keywords, e.g., SeisBase.KW.Filt.np = 2
changes the default number of poles to 2.
See also: DSP.jl documentation
Troubleshooting NaNs in Output
NaNs in the output of filtering operations (e.g., filtfilt!, translate_resp!) are nearly always the result of zeros in the denominator of the filter transfer function.
This is not a bug in SeisBase.
In particular, the increased speed of data processing at 32-bit precision comes with an increased risk of NaN output. The reason is that 32-bit machine epsilon ($eps(Float32)$ in Julia) is ~1.0e-7
; by comparison, 64-bit machine epsilon is ~1.0e-16
.
Please check for common signal processing issues before reporting NaNs to SeisBase maintainers. For example:
Filtering
- Is the pass band too narrow?
- Is the lower corner frequency too close to DC?
- Is the filter order (or number of poles) too high?
Instrument Responses
- Are the roll-off frequencies of the old and new responses too far apart?
- Is the water level appropriate for the data scaling?
Suggested References
- Oppenheim, A.V., Buck, J.R. and Schafer, R.W., 2009. Discrete-time signal processing (3rd edition). Upper Saddle River, NJ, USA: Prentice Hall.
- Orfanidis, S.J., 1995. Introduction to signal processing. Upper Saddle River, NJ, USA: Prentice Hall.