/
processing.rst
354 lines (253 loc) · 13.3 KB
/
processing.rst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
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
###############
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.
.. function: demean!(S::GphysData[, 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. Specify chans=CC to restrict
to channel number(s) CC. Ignores NaNs.
:raw-html:`<br /><br />`
.. function: detrend!(S::GphysData[, n=1, chans=CC, irr=false])
Remove the polynomial trend of degree n from every regularly-sampled channel
in S using a least-squares polynomial fit. Specify chans=CC to restrict
to channel number(s) CC. Ignores NaNs.
:raw-html:`<br /><br />`
.. function: env!(S::GphysData[, chans=CC])
Compute the envelope of channel data in S. Only affects regularly-sampled
channels.
:raw-html:`<br /><br />`
.. function:: nanfill!(S)
For each channel **i** in **S**, replace all NaNs in **S.x[i]** with the mean
of non-NaN values.
:raw-html:`<br /><br />`
.. function:: resample!(S::GphysData [, chans=CC, fs=FS])
.. function:: resample!(C::SeisChannel, fs::Float64)
Resample data in S to FS Hz. 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 bad behaviors.
Use keyword **chans=CC** to only resample channel numbers **CC**. By default,
all channels **i** with **S.fs[i] > 0.0** are resampled.
:raw-html:`<br /><br />`
.. function:: unscale!(S[, chans=CC, irr=false])
Divide the gains from all channels **i** with **S.fs[i] > 0.0**. Specify
chans=CC to restrict to channel number(s) CC. Specify **irr=true** to also
remove gains of irregularly-sampled channels.
***********************
Customizable Operations
***********************
Convert Seismograms
===================
Seismograms can be converted to or from displacement, velocity, or acceleration
using convert_seis:
.. function:: convert_seis!(S[, chans=CC, units_out=UU, v=V])
.. function:: convert_seis!(C[, units_out=UU, v=V])
Converts all seismic data channels in `S` to velocity seismograms,
differentiating or integrating as needed.
Keywords
********
* units_out=UU: specify output units: "m", "m/s" (default), or "m/s²"
* chans=CC: restrict seismogram conversion to seismic data channels in CC
* v=V: verbosity
Behavior and Usage Warnings
***************************
**Long Seismograms**: convert_seis becomes less reversible as seismograms lengthen,
especially at Float32 precision, due to `loss of significance
<https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems>`_.
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.
**Rectangular Integration**: 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 <https://github.com/JuliaMath/KahanSummation.jl>`_.
.....
Fill Gaps
=========
.. function:: ungap!(S[, chans=CC, m=true, tap=false])
.. function:: ungap!(C[, m=true, tap=false])
Fill time gaps in each channel with the mean of the channel data.
Keywords
********
* chans=CC: only ungap channels CC.
* m=false: this flag fills gaps with NaNs instead of the mean.
* tap=true: taper data before filling gaps.
.....
.. _merge:
Merge
=====
.. function:: merge!(S::GphysData, U::GphysData)
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.
:raw-html:`<br /><br />`
.. function:: merge!(S::GphysData)
"Flatten" a GphysData structure by merging data from identical channels.
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?
-----------------------------
.. csv-table::
:header: Issue, Resolution
:delim: |
:widths: 1, 1
Empty channels | Delete
Duplicated channels | Delete duplicate channels
Duplicated windows in channel(s) | Delete duplicate windows
Multiple channels, same properties\ :sup:`(a)` | 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\ :sup:`(a)` | Time offset corrected, windows merged
Overlapping windows, non-identical data | Samples averaged, windows merged
:sup:`(a)` "Properties" here are ``:id``, ``:fs``, ``:loc``, ``:resp``, and ``:units``.
:raw-html:`<br />`
:sup:`(b)` Data offset >4 sample intervals are treated as overlapping and non-identical.
When SeisIO Won't Merge
------------------------
SeisIO 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.
:raw-html:`<br /><br />`
.. function:: mseis!(S::GphysData, U::GphysData, ...)
Merge multiple GphysData structures into S.
.....
Seismic Instrument Response
===========================
.. function:: translate_resp!(S, resp_new[, chans=CC, wl=g])
.. function:: translate_resp!(Ch, resp_new[, wl=g])
Translate the instrument response of seismic data channels to **resp_new**.
Replaces field **:resp** with **resp_new** for all affected channels.
:raw-html:`<br /><br />`
.. function:: remove_resp!(S, chans=CC, wl=g])
.. function:: remove_resp!(Ch, wl=g])
Remove (flatten to DC) the instrument response of **Ch**, or of seismic data
channels **CC** in **S**. Replaces **:resp** with the appropriate (all-pass)
response.
Keywords
********
* **C=cha** restricts response translation for GphysData object **S** to channel(s) **cha**. Accepts an Integer, UnitRange, or Array{Int64,1} argument; does *not* accept string IDs. By default, all seismic data channels in **S** have their responses translated to **resp_new**.
* **wl=g** sets the waterlevel to g (default: g = eps(Float32) ~ 1.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.
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
===========
.. function:: sync!(S::GphysData[, s=ST, t=EN, v=VV])
Synchronize all data in S to start at `ST` and terminate at `EN` with verbosity level VV.
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`.
.....
Taper
=====
.. function:: taper!(S[, keywords])
Cosine taper each channel in S around time gaps. Specify chans=CC to restrict
to channel number(s) CC. Does not modify irregularly-sampled data channels.
:raw-html:`<br /><br />`
.. function:: taper!(C[, keywords])
Cosine taper each segment of time-series data in GphysChannel object C that
contains at least `N_min` total samples. Returns if C is irregularly sampled.
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*:math:`\alpha`% of samples in each window are tapered, up to `t_max` seconds of data.
.....
Zero-Phase Filter
=================
.. function:: filtfilt!(S::GphysData[; KWs])
Apply a zero-phase filter to regularly-sampled data in **S**. Irregularly-sampled data are never processed by filtfilt!.
:raw-html:`<br /><br />`
.. function:: filtfilt!(C::SeisChannel[; KWs])
Apply zero-phase filter to **C.x**. Filtering is applied to each contiguous data
segment in C separately.
Keywords
********
.. csv-table::
:header: KW, Default, Type, Description
:delim: |
:widths: 1, 2, 1, 4
chans | [] | :sup:`(a)` | channel numbers to filter
fl | 1.0 | Float64 | lower corner frequency [Hz] \ :sup:`(b)`
fh | 15.0 | Float64 | upper corner frequency [Hz] \ :sup:`(b)`
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)
| :sup:`(a)` Allowed types are Integer, UnitRange, and Array{Int64, 1}.
| :sup:`(b)` 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 :ref:`shared keywords<dkw>`, e.g., ``SeisIO.KW.Filt.np = 2`` changes the default number of poles to 2.
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 SeisIO.
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 SeisIO 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
********************
1. Oppenheim, A.V., Buck, J.R. and Schafer, R.W., 2009. Discrete-time signal processing (3rd edition). Upper Saddle River, NJ, USA: Prentice Hall.
2. Orfanidis, S.J., 1995. Introduction to signal processing. Upper Saddle River, NJ, USA: Prentice Hall.