去均值

去均值#

去均值是从每个数据点中减去所有数据点的平均值(也称直流分量),这一操作旨在消除仪器零点漂移,一般是数据处理的第一步。

我们以 2022 年 9 月 22 日发生在墨西哥的一个 Mw 6.8 级地震为例,该地震的详细信息见 https://earthquake.usgs.gov/earthquakes/eventpage/us7000ia36

使用 ObsPy 提供的 Client.get_waveforms() 方法下载 该地震在 ANMO 台站的事件波形资料。

from obspy import UTCDateTime
from obspy.clients.fdsn import Client
import matplotlib.pyplot as plt

client = Client("IRIS") 

# 下载 2022 年墨西哥 Mw 6.8 级地震在 ANMO 台站的波形数据
starttime = UTCDateTime("2022-09-22T06:18:00")
endtime = starttime + 720  # 下载 12 分钟数据
st = client.get_waveforms(
    network="IU",
    station="ANMO", 
    location="00", 
    channel="BHZ",
    starttime=starttime, 
    endtime=endtime,
)
st.plot();
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 from obspy import UTCDateTime
      2 from obspy.clients.fdsn import Client
      3 import matplotlib.pyplot as plt

File ~/micromamba/envs/seismo-learn/lib/python3.13/site-packages/obspy/__init__.py:44
     41     msg = ('pkg_resources is deprecated as an API')
     42     warnings.filterwarnings(
     43         'ignore', message=msg, category=DeprecationWarning, module='obspy')
---> 44     from obspy.core.utcdatetime import UTCDateTime  # NOQA
     45 from obspy.core.util import _get_version_string
     46 __version__ = _get_version_string(abbrev=10)

File ~/micromamba/envs/seismo-learn/lib/python3.13/site-packages/obspy/core/__init__.py:120
     12 """
     13 obspy.core - Core classes of ObsPy
     14 ==================================
   (...)    117 .. _NumPy: http://www.numpy.org
    118 """
    119 # don't change order
--> 120 from obspy.core.utcdatetime import UTCDateTime  # NOQA
    121 from obspy.core.util.attribdict import AttribDict  # NOQA
    122 from obspy.core.trace import Stats, Trace  # NOQA

File ~/micromamba/envs/seismo-learn/lib/python3.13/site-packages/obspy/core/utcdatetime.py:21
     18 import warnings
     20 import numpy as np
---> 21 from obspy.core.util.deprecation_helpers import ObsPyDeprecationWarning
     24 # based on https://www.myintervals.com/blog/2009/05/20/iso-8601, w/ week 53 fix
     25 _ISO8601_REGEX = re.compile(r"""
     26     ^
     27     ([\+-]?\d{4}(?!\d{2}\b))
   (...)     40     $
     41     """, re.VERBOSE)

File ~/micromamba/envs/seismo-learn/lib/python3.13/site-packages/obspy/core/util/__init__.py:22
     20 # import order matters - NamedTemporaryFile must be one of the first!
     21 from obspy.core.util.attribdict import AttribDict
---> 22 from obspy.core.util.base import (ALL_MODULES, DEFAULT_MODULES,
     23                                   NATIVE_BYTEORDER, NETWORK_MODULES,
     24                                   NamedTemporaryFile, _read_from_plugin,
     25                                   create_empty_data_chunk, get_example_file,
     26                                   get_script_dir_name, MATPLOTLIB_VERSION,
     27                                   SCIPY_VERSION, NUMPY_VERSION,
     28                                   CARTOPY_VERSION, CatchAndAssertWarnings)
     29 from obspy.core.util.misc import (BAND_CODE, CatchOutput, complexify_string,
     30                                   guess_delta, score_at_percentile,
     31                                   to_int_or_zero, SuppressOutput)
     32 from obspy.core.util.obspy_types import (ComplexWithUncertainties, Enum,
     33                                          FloatWithUncertainties)

File ~/micromamba/envs/seismo-learn/lib/python3.13/site-packages/obspy/core/util/base.py:26
     23 from pathlib import PurePath
     25 import numpy as np
---> 26 import pkg_resources
     27 from pkg_resources import get_entry_info, iter_entry_points
     29 from obspy.core.util.misc import to_int_or_zero, buffered_load_entry_point

ModuleNotFoundError: No module named 'pkg_resources'

ObsPy 提供了 obspy.core.trace.Trace.detrend() 方法可以实现去均值操作。

tr = st[0]
print(f"Mean value before demean: {tr.data.mean()}")

# 去均值处理
tr.detrend("demean")  

print(f"Mean value after demean: {tr.data.mean()}")

虽然原始波形在视觉上观察不到显著的直流分量,但是去均值(demean)操作成功地 将原始波形的均值从 1060.42 变为 4.04e-12,表明已经完成了去均值操作。