From 85977c1a3afbad87c3079d24e72904663a3dfff0 Mon Sep 17 00:00:00 2001 From: "Kasper D. Fischer" Date: Mon, 30 May 2022 13:32:09 +0200 Subject: [PATCH] add exercises on cross correlation --- .../1-simple_cross_correlation.ipynb | 366 +++++++ 03-Cross_Correlation/2-enhanced_picker.ipynb | 153 +++ 03-Cross_Correlation/3-ambient_noise.ipynb | 911 ++++++++++++++++++ README.md | 6 + 4 files changed, 1436 insertions(+) create mode 100644 03-Cross_Correlation/1-simple_cross_correlation.ipynb create mode 100644 03-Cross_Correlation/2-enhanced_picker.ipynb create mode 100644 03-Cross_Correlation/3-ambient_noise.ipynb diff --git a/03-Cross_Correlation/1-simple_cross_correlation.ipynb b/03-Cross_Correlation/1-simple_cross_correlation.ipynb new file mode 100644 index 0000000..8ef2564 --- /dev/null +++ b/03-Cross_Correlation/1-simple_cross_correlation.ipynb @@ -0,0 +1,366 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Cross correlation exercise\n", + "\n", + "This exercise focuses on the use of cross correlation of similar waveforms. The first example uses the cross correlation to enhance the pick of a seismic onset.\n", + "\n", + "In signal processing, cross-correlation is a measure of similarity of two series as a function of the displacement of one relative to the other. This is also known as a sliding dot product or sliding inner-product. It is commonly used for searching a long signal for a shorter, known feature. In seismology cross-correlation is used to compare seismograms from the same event recorded at different stations. It can give information about differences due to longer wave distances or different wave paths. \n", + "\n", + "For seismograms recorded at two Stations (1,2), the cross-correlation is defined as\n", + "\n", + "$$\\begin{equation} f_{21}(t)=\\int_{- \\infty}^\\infty f_2(\\tau)f_1(\\tau + t)d\\tau \\end{equation} $$\n", + "\n", + "where $t$ is the displacement, also known as lag." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "In the frequency range the equation is\n", + "\n", + "\\begin{equation}\n", + " F_{21}(\\omega)=F_2(\\omega)F_1^\\ast (\\omega) \\quad.\n", + "\\end{equation}\n", + "\n", + "$F_1^\\ast $ denotes the complex conjugate of $F_1$. In an autocorrelation, which is the cross-correlation of a signal with itself, there will always be a peak at a lag of zero, and its size will be the signal energy. Furthermore, the definition of correlation always includes a standardising factor in such a way that correlations have values between −1 and +1.\n", + "\n", + "As an example, consider two real valued functions $f_1$ and $f_2$ differing only by an unknown shift along the x-axis. One can use the cross-correlation to find how much $f_2$ must be shifted along the x-axis to make it identical to $f_1$. The formula essentially slides the $f_2$ function along the x-axis, calculating the integral of their product at each position. When the functions match, the value of $f_{21}(t)$ is maximized." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "We will use this to detect the nuclear weapons tests of the DPRK (North Korea) in the recordings of station BUG. The largest event from Sep. 3, 2017 is used as template event." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "### Matplotlib settings and Imports" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# show figures inline\n", + "%matplotlib inline\n", + "import matplotlib.pyplot as plt\n", + "plt.style.use('ggplot') # Matplotlib style sheet - nicer plots!\n", + "plt.rcParams['figure.figsize'] = 12, 8 # Slightly bigger plots by default\n", + "\n", + "# python imports\n", + "from obspy import read, UTCDateTime\n", + "from matplotlib.pyplot import xcorr\n", + "import numpy as np\n", + "\n", + "#from obspy.signal.cross_correlation import xcorr_pick_correction # 1st exercise\n", + "#from obspy.signal.trigger import coincidence_trigger # 2nd exercise\n", + "#from pprint import pprint" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "### Get waveform of template event" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "## compute and plot cross correlation of recordings of two events\n", + "\n", + "nsec = 300 # 5 minutes\n", + "\n", + "# use FDSNws to get the data from station BUG\n", + "from obspy.clients.fdsn import Client\n", + "client = Client(\"BGR\")\n", + "\n", + "t1 = UTCDateTime(\"2017-09-03T03:40:00Z\")\n", + "t2 = t1 + nsec\n", + "template = client.get_waveforms(\"GR\", \"BUG\", \"\", \"BHZ\", t1, t2, attach_response=True)\n", + "template.remove_response(output=\"VEL\");" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [], + "source": [ + "# try different starttime, enttime and filtersettings to display the onset of the event at BUG\n", + "st = template.copy()\n", + "st.plot(starttime=t1+10, endtime=t1+200);\n", + "st.filter('bandpass', freqmin=0.8, freqmax=3.0).plot(starttime=t1+10, endtime=t1+200);" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "### Get more Events" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Test Event 1\n", + "t1 = UTCDateTime(\"2016-09-09T00:40:00Z\")\n", + "t2 = t1 + nsec\n", + "st1 = client.get_waveforms(\"GR\", \"BUG\", \"\", \"BHZ\", t1, t2, attach_response=True)\n", + "st1.remove_response(output=\"VEL\")\n", + "\n", + "st_ = st1.copy()\n", + "st_.plot(starttime=t1+90, endtime=t1+160);\n", + "st_.filter('bandpass', freqmin=0.8, freqmax=3.0).plot(starttime=t1+90, endtime=t1+160);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [], + "source": [ + "# Test Event 2\n", + "t1 = UTCDateTime(\"2016-01-06T01:40:00Z\")\n", + "t2 = t1 + nsec\n", + "st2 = client.get_waveforms(\"GR\", \"BUG\", \"\", \"BHZ\", t1, t2, attach_response=True)\n", + "st2.remove_response(output=\"VEL\")\n", + "\n", + "st_ = st2.copy()\n", + "st_.plot(starttime=t1+90, endtime=t1+160);\n", + "st_.filter('bandpass', freqmin=0.8, freqmax=3.0).plot(starttime=t1+90, endtime=t1+160);" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "### Calculate Cross-correlation of template event data and test event data" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# time window for cross-corelation\n", + "nsec1 = 90\n", + "nsec2 = 160\n", + "\n", + "# extract data of template event\n", + "st_ = template.copy()\n", + "t1 = UTCDateTime(\"2017-09-03T03:40:00Z\")\n", + "st_.filter('bandpass', freqmin=0.8, freqmax=3.0).detrend().trim(starttime=t1+nsec1, endtime=t1+nsec2)\n", + "st_.trim(starttime=t1, endtime=t1+nsec, pad=True, fill_value=0.0)\n", + "tr_template = st_[0].copy()\n", + "\n", + "# extract data of 1st and 2nd test event\n", + "st_ = st1.copy()\n", + "t1 = UTCDateTime(\"2016-09-09T00:40:00Z\")+0\n", + "st_.filter('bandpass', freqmin=0.8, freqmax=3.0).trim(starttime=t1, endtime=t1+nsec, pad=True, fill_value=0.0)\n", + "tr_event1 = st_[0].copy()\n", + "\n", + "st_ = st2.copy()\n", + "t1 = UTCDateTime(\"2016-01-06T01:40:00Z\")+0\n", + "st_.filter('bandpass', freqmin=0.8, freqmax=3.0).trim(starttime=t1, endtime=t1+nsec, pad=True, fill_value=0.0)\n", + "tr_event2 = st_[0].copy()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [], + "source": [ + "# plot template and test events\n", + "tr_template.normalize().plot();\n", + "tr_event1.normalize().plot();\n", + "tr_event2.normalize().plot();" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [], + "source": [ + "# set parameters\n", + "cc_maxlag = nsec # maximum lag time [s]\n", + "samp_rate = tr_template.stats['sampling_rate']\n", + "shift_len = int(cc_maxlag * samp_rate)\n", + "\n", + "# calculate cross correlation\n", + "a_t1 = xcorr(tr_template.data, tr_event1.data, maxlags=shift_len) # matplotlib\n", + "cc_lags = a_t1[0] #lag vector\n", + "cc = a_t1[1] #correlation vector\n", + "\n", + "# extract parameters from result\n", + "cc_t = np.linspace(-cc_maxlag, cc_maxlag, shift_len*2+1)\n", + "cc_max = max(cc)\n", + "cc_shift = cc.argmax() - len(cc)/2\n", + "cc_shift_t = cc_shift / samp_rate\n", + "\n", + "# plot result\n", + "fig = plt.figure(1)\n", + "plt.clf()\n", + "plt.rcParams[\"figure.figsize\"] = [25,10]\n", + "plt.plot(cc_t, cc, 'k')\n", + "plt.title('cross-correlation of template and test event 1, max %.2f at %.2f s shift' %(cc_max, cc_lags[np.argmax(cc)]/samp_rate))\n", + "plt.xlabel('time lag [sec]')\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [], + "source": [ + "# set parameters\n", + "cc_maxlag = nsec # maximum lag time [s]\n", + "samp_rate = tr_template.stats['sampling_rate']\n", + "shift_len = int(cc_maxlag * samp_rate)\n", + "\n", + "# calculate cross correlation\n", + "a_t2 = xcorr(tr_template.data, tr_event2.data, maxlags=shift_len) # matplotlib\n", + "cc_lags = a_t2[0] #lag vector\n", + "cc = a_t2[1] #correlation vector\n", + "\n", + "# extract parameters from result\n", + "cc_t = np.linspace(-cc_maxlag, cc_maxlag, shift_len*2+1)\n", + "cc_max = max(cc)\n", + "cc_shift = cc.argmax() - len(cc)/2\n", + "cc_shift_t = cc_shift / samp_rate\n", + "\n", + "# plot result\n", + "fig = plt.figure(1)\n", + "plt.clf()\n", + "plt.rcParams[\"figure.figsize\"] = [25,10]\n", + "plt.plot(cc_t, cc, 'k')\n", + "plt.title('cross-correlation of template and test event 2, max %.2f at %.2f s shift' %(cc_max, cc_lags[np.argmax(cc)]/samp_rate))\n", + "plt.xlabel('time lag [sec]')\n", + "plt.show()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Exercise\n", + "\n", + "* create a plot showing the template and the test events in one plot with the onset times alligned" + ] + } + ], + "metadata": { + "celltoolbar": "Slideshow", + "interpreter": { + "hash": "b40d316b6eb93f8d3cd4b30bf241a4edb2b8ca4ea8a7206ab7ccdcc909e9c051" + }, + "kernelspec": { + "display_name": "Python 3.6.13 ('obspy')", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.13" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/03-Cross_Correlation/2-enhanced_picker.ipynb b/03-Cross_Correlation/2-enhanced_picker.ipynb new file mode 100644 index 0000000..b8af6fc --- /dev/null +++ b/03-Cross_Correlation/2-enhanced_picker.ipynb @@ -0,0 +1,153 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Simple cross correlation to enhance pick accuracy\n", + "\n", + "This part is taken from the ObsPy tutorial at https://docs.obspy.org/master/tutorial/code_snippets/xcorr_pick_correction.html.\n", + "\n", + "This example shows how to align the waveforms of phase onsets of two earthquakes in order to correct the original pick times that can never be set perfectly consistent in routine analysis. A parabola is fit to the concave part of the cross correlation function around its maximum, following the approach by [Deichmann 1992](https://docs.obspy.org/master/citations.html#deichmann1992).\n", + "\n", + "To adjust the parameters (i.e. the used time window around the pick and the filter settings) and to validate and check the results the options plot and filename can be used to open plot windows or save the figure to a file.\n", + "\n", + "See the documentation of [xcorr_pick_correction()](https://docs.obspy.org/master/packages/autogen/obspy.signal.cross_correlation.xcorr_pick_correction.html#obspy.signal.cross_correlation.xcorr_pick_correction) for more details." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [], + "source": [ + "# show figures inline\n", + "%matplotlib inline\n", + "import matplotlib.pyplot as plt\n", + "plt.style.use('ggplot') # Matplotlib style sheet - nicer plots!\n", + "plt.rcParams['figure.figsize'] = 12, 8 # Slightly bigger plots by default\n", + "\n", + "# python imports\n", + "from obspy import read, UTCDateTime\n", + "from obspy.signal.cross_correlation import xcorr_pick_correction" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [], + "source": [ + "# read example data of two small earthquakes\n", + "path = \"https://examples.obspy.org/BW.UH1..EHZ.D.2010.147.%s.slist.gz\"\n", + "st1 = read(path % (\"a\", ))\n", + "st2 = read(path % (\"b\", ))\n", + "\n", + "# display info and plot data\n", + "print(st1)\n", + "st1.plot();\n", + "print(st2)\n", + "st2.plot();" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [], + "source": [ + "# select the single traces to use in correlation.\n", + "# to avoid artifacts from preprocessing there should be some data left and\n", + "# right of the short time window actually used in the correlation.\n", + "tr1 = st1.select(component=\"Z\")[0]\n", + "tr2 = st2.select(component=\"Z\")[0]\n", + "\n", + "# these are the original pick times set during routine analysis\n", + "t1 = UTCDateTime(\"2010-05-27T16:24:33.315000Z\")\n", + "t2 = UTCDateTime(\"2010-05-27T16:27:30.585000Z\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# estimate the time correction for pick 2 without any preprocessing and open\n", + "# a plot window to visually validate the results\n", + "dt, coeff = xcorr_pick_correction(t1, tr1, t2, tr2, 0.05, 0.2, 0.25, plot=True)\n", + "print(\"No preprocessing:\")\n", + "print(\" Time correction for pick 2: %.6f s\" % dt)\n", + "print(\" Correlation coefficient: %.2f\" % coeff)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [], + "source": [ + "# estimate the time correction with bandpass prefiltering\n", + "dt, coeff = xcorr_pick_correction(t1, tr1, t2, tr2, 0.05, 0.2, 0.25, plot=True,\n", + " filter=\"bandpass\",\n", + " filter_options={'freqmin': 1, 'freqmax': 10})\n", + "print(\"Bandpass prefiltering:\")\n", + "print(\" Time correction for pick 2: %.6f s\" % dt)\n", + "print(\" Correlation coefficient: %.2f\" % coeff)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "celltoolbar": "Slideshow", + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.2" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/03-Cross_Correlation/3-ambient_noise.ipynb b/03-Cross_Correlation/3-ambient_noise.ipynb new file mode 100644 index 0000000..1c4310e --- /dev/null +++ b/03-Cross_Correlation/3-ambient_noise.ipynb @@ -0,0 +1,911 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "
\n", + "
\n", + "
\n", + "
Ambient Seismic Noise Analysis
\n", + "
Cross Correlation
\n", + "
\n", + "
\n", + "
\n", + "\n", + "In this tutorial you will try to reproduce one of the figures in Shapiro _et al._. To see which one, execute the second code block below. \n", + "\n", + "Reference: *High-Resolution Surface-Wave Tomography from Ambient Seismic Noise*, Nikolai M. Shapiro, et al. **Science** 307, 1615 (2005);\n", + "DOI: 10.1126/science.1108339\n", + "\n", + "##### Authors:\n", + "* Celine Hadziioannou\n", + "* Ashim Rijal\n", + "---\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "### In this notebook\n", + "We will reproduce figure B below. This figure compares: \n", + "1) the seismogram from an event near station MLAC, recorded at station PHL (top)\n", + "2) the \"Greens function\" obtained by correlating noise recorded at stations MLAC and PHL (center and bottom)\n", + "\n", + "All bandpassed for periods between 5 - 10 seconds. \n", + "\n", + "\n", + "\n", + "---" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "code_folding": [ + 0 + ], + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [], + "source": [ + "# Configuration step (Please run it before the code!)\n", + "\n", + "# MatplotLib.PyPlot\n", + "%matplotlib inline\n", + "import matplotlib.pyplot as plt\n", + "plt.style.use('ggplot') # Matplotlib style sheet - nicer plots!\n", + "plt.rcParams['figure.figsize'] = 12, 8\n", + "\n", + "# NumPy\n", + "import numpy as np\n", + "\n", + "# ObsPy\n", + "from obspy.core import UTCDateTime, read\n", + "from obspy.clients.fdsn import Client\n", + "try: # depends on obspy version; this is for v1.1.0\n", + " from obspy.geodetics import gps2dist_azimuth as gps2DistAzimuth\n", + "except ImportError:\n", + " from obspy.core.util import gps2DistAzimuth\n", + "\n", + "# ignore warnings from filters\n", + "import warnings\n", + "warnings.filterwarnings('ignore')" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "### 1. Read in noise data \n", + "Read the noise data for station MLAC into a stream. \n", + "\n", + "Then, **read in** noise data for station PHL.\n", + "Add this to the stream created above.\n", + "\n", + "These two data files contain 90 days of vertical component noise for each station.\n", + "#### If you need data for more than 90 days, it can be downloaded form IRIS database. ###" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "code_folding": [ + 0 + ], + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [], + "source": [ + "# Shapiro et al. use noise data from MLAC and PHL stations\n", + "\n", + "num_of_days = 90 # no of days of data: change if more than 90days of data is required\n", + "if num_of_days <= 90:\n", + " # get noise data for station MLAC\n", + " stn = read('https://raw.github.com/ashimrijal/NoiseCorrelation/master/data/noise.CI.MLAC.LHZ.2004.294.2005.017.mseed')\n", + " # get noise data for the station PHL and add it to the previous stream\n", + " stn += read('https://raw.github.com/ashimrijal/NoiseCorrelation/master/data/noise.CI.PHL.LHZ.2004.294.2005.017.mseed')\n", + " # if you have data stored locally, comment the stn = and stn += lines above\n", + " # then uncomment the following 3 lines and adapt the path: \n", + " # stn = obspy.read('./noise.CI.MLAC.LHZ.2004.294.2005.017.mseed')\n", + " # stn += obspy.read('noise.CI.PHL.LHZ.2004.294.2005.017.mseed')\n", + " # ste = obspy.read('event.CI.PHL.LHZ.1998.196.1998.196.mseed')\n", + "else:\n", + " # download data from IRIS database\n", + " client = Client(\"IRIS\") # client specification\n", + " t1 = UTCDateTime(\"2004-10-20T00:00:00.230799Z\") # start UTC date/time\n", + " t2 = t1+(num_of_days*86400) # end UTC date/time\n", + " stn = client.get_waveforms(network=\"CI\", station=\"MLAC\",location=\"*\", channel=\"*\",\n", + " starttime=t1, endtime=t2) # get data for MLAC\n", + " stn += client.get_waveforms(network=\"CI\", station=\"PHL\", location=\"*\", channel=\"*\",\n", + " starttime=t1, endtime=t2) # get data for PHL and add it to the previous stream" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "### 2. Preprocess noise ###\n", + "***Preprocessing 1***\n", + "* Just to be sure to keep a 'clean' original stream, first **copy** the noise stream with [st.copy()](https://docs.obspy.org/packages/autogen/obspy.core.stream.Stream.copy.html)\n", + "The copied stream is the stream you will use from now on. \n", + "\n", + "* In order to test the preprocessing without taking too long, it's also useful to first **trim** this copied noise data stream to just one or a few days. This can be done with [st.trim()](https://docs.obspy.org/packages/autogen/obspy.core.stream.Stream.trim.html), after defining your start- and endtime. \n", + "\n", + "\n", + "Many processing functionalities are included in Obspy. For example, you can remove any (linear) trends with [st.detrend()](https://docs.obspy.org/packages/autogen/obspy.core.stream.Stream.detrend.html), and taper the edges with [st.taper()](https://docs.obspy.org/packages/autogen/obspy.core.stream.Stream.taper.html). \n", + "Different types of filter are also available in [st.filter()](https://docs.obspy.org/packages/autogen/obspy.core.stream.Stream.filter.html). \n", + "\n", + "* first **detrend** the data. \n", + "* next, apply a **bandpass filter** to select the frequencies with most noise energy. The secondary microseismic peak is roughly between 0.1 and 0.2 Hz. The primary microseismic peak between 0.05 and 0.1 Hz. Make sure to use a zero phase filter! *(specify argument zerophase=True)*" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "code_folding": [ + 0 + ], + "slideshow": { + "slide_type": "subslide" + } + }, + "outputs": [], + "source": [ + "# Preprocessing 1\n", + "\n", + "stp = stn.copy() # copy stream\n", + "t = stp[0].stats.starttime\n", + "stp.trim(t, t + 4 * 86400) # shorten stream for quicker processing\n", + "\n", + "stp.detrend('linear') # remove trends using detrend\n", + "stp.taper(max_percentage=0.05, type='cosine') # taper the edges\n", + "stp.filter('bandpass', freqmin=0.1, freqmax=0.2, zerophase=True) # filter data of all traces in the streams" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "***Preprocessing 2***\n", + "\n", + "Some additional useful processing functions are provided in the following **Functions**\n", + "\n", + "* For each trace in the stream, apply **spectral whitening** on the frequency range you chose before (either [0.1 0.2]Hz or [0.05 0.1]Hz), using function **``whiten``**. \n", + "\n", + "\n", + "* For the **time normalization**, the simplest option is to use the one-bit normalization option provided in function **``normalize``**. \n", + "\n", + "* *Optional: play around with different normalization options, such as clipping to a certain number of standard deviations, or using the running absolute mean normalization.*" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "A brief *desription of individual* **functions** (see the next cell) are as follows:\n", + "\n", + "1) **whiten**:\n", + "\n", + " spectral whitening of trace `tr` using a cosine tapered boxcar between `freqmin` and `freqmax`\n", + " (courtesy Gaia Soldati & Licia Faenza, INGV)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "code_folding": [ + 0, + 3, + 46, + 57, + 93, + 130, + 150 + ], + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "def whiten(tr, freqmin, freqmax):\n", + " \n", + " nsamp = tr.stats.sampling_rate\n", + " \n", + " n = len(tr.data)\n", + " if n == 1:\n", + " return tr\n", + " else: \n", + " frange = float(freqmax) - float(freqmin)\n", + " nsmo = int(np.fix(min(0.01, 0.5 * (frange)) * float(n) / nsamp))\n", + " f = np.arange(n) * nsamp / (n - 1.)\n", + " JJ = ((f > float(freqmin)) & (f lim] = lim\n", + " tr.data[tr.data < -lim] = -lim\n", + "\n", + " elif norm_method == \"clipping_iter\":\n", + " lim = clip_factor * np.std(np.abs(tr.data))\n", + " \n", + " # as long as still values left above the waterlevel, clip_weight\n", + " while tr.data[np.abs(tr.data) > lim] != []:\n", + " tr.data[tr.data > lim] /= clip_weight\n", + " tr.data[tr.data < -lim] /= clip_weight\n", + "\n", + " elif norm_method == 'ramn':\n", + " lwin = tr.stats.sampling_rate * norm_win\n", + " st = 0 # starting point\n", + " N = lwin # ending point\n", + "\n", + " while N < tr.stats.npts:\n", + " win = tr.data[st:N]\n", + "\n", + " w = np.mean(np.abs(win)) / (2. * lwin + 1)\n", + " \n", + " # weight center of window\n", + " tr.data[st + lwin / 2] /= w\n", + "\n", + " # shift window\n", + " st += 1\n", + " N += 1\n", + "\n", + " # taper edges\n", + " taper = get_window(tr.stats.npts)\n", + " tr.data *= taper\n", + "\n", + " elif norm_method == \"1bit\":\n", + " tr.data = np.sign(tr.data)\n", + " tr.data = np.float32(tr.data)\n", + "\n", + " return tr" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "6) **get_window**:\n", + "\n", + " Return tukey window of length N\n", + " N: length of window\n", + " alpha: alpha parameter in case of tukey window.\n", + " 0 -> rectangular window\n", + " 1 -> cosine taper\n", + " returns: window (np.array)\n", + " \n", + "Doc of [scipy.signal.get_window](https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.signal.get_window.html)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "code_folding": [ + 0, + 3, + 46, + 57, + 93, + 130, + 150 + ], + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "def get_window(N, alpha=0.2):\n", + "\n", + " window = np.ones(N)\n", + " x = np.linspace(-1., 1., N)\n", + " ind1 = (abs(x) > 1 - alpha) * (x < 0)\n", + " ind2 = (abs(x) > 1 - alpha) * (x > 0)\n", + " window[ind1] = 0.5 * (1 - np.cos(np.pi * (x[ind1] + 1) / alpha))\n", + " window[ind2] = 0.5 * (1 - np.cos(np.pi * (x[ind2] - 1) / alpha))\n", + " return window" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "### Actual preprocessing happens here -- this can take a while!" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "code_folding": [ + 0 + ], + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Preprocessing 2\n", + "st = stp.copy() # copy stream\n", + "\n", + "for tr in st:\n", + " tr = normalize(tr, norm_method=\"1bit\")\n", + " tr = whiten(tr, 0.1, 0.2)\n", + "print ('done!')" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "#### Cross-correlation ####\n", + "Once you're happy with the preprocessing, you can calculate the **cross-correlation** using **``correlateNoise``** function. The cross-correlation are computed by slices of a few hours each (specified in *corrwin*). \n", + "\n", + "**For correlateNoise function**\n", + "* input: stream, list of stations (here: ['MLAC', 'PHL']), slice length in seconds\n", + "* output: all individual correlations, stack\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "code_folding": [ + 0 + ], + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Cross-correlate\n", + "xcorr, stack = correlateNoise(st, ['MLAC','PHL'], 7200)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "The resulting stack can be **plotted** with **``plotStack``** function. Since it doesn't make much sense to look at a 2 hour long correlation signal, you can decide to plot only the central part by specifying a ``maxlag`` (in seconds). " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "code_folding": [ + 0 + ], + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Plotting\n", + "\n", + "plotStack(st, stack, 400)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "If you're only working with a few days of noise (after trimming), this plot probably doesn't look very nice. You could go back to the code block named 'preprocessing 1', and keep a longer noise record (10 days works quite well already). " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "#### Compare to event trace ####\n", + "In 1998, a M = 5.1 event occurred next to station MLAC. This event was recorded at PHL and we read this data.\n", + "\n", + "* **read** the event data to a separate stream" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "ste = read('https://raw.github.com/ashimrijal/NoiseCorrelation/master/data/event.CI.PHL.LHZ.1998.196.1998.196.mseed')\n", + "# if data is stored locally, uncomment the following line and comment the line above:\n", + "#ste = obspy.read('./event.CI.PHL.LHZ.1998.196.1998.196.mseed')" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "subslide" + } + }, + "source": [ + "#### Preprocess event ####\n", + "\n", + "The event signal should be processed in a similar way to the noise. \n", + "\n", + "* **detrend** in the same way as before\n", + "* **bandpass filter** for the same frequencies as chosen above\n", + "* apply **spectral whitening** to each trace in the event stream to ensure the spectrum is comparable to the noise. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "code_folding": [ + 0 + ], + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Preprocessing\n", + "\n", + "ste.detrend('linear')\n", + "ste.filter('bandpass', freqmin=0.1, freqmax=0.2, zerophase=True)\n", + "for tr in ste:\n", + " tr = whiten(tr, 0.1, 0.2)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "#### Plot! ####\n", + "\n", + "A plotting function is provided to plot both signals alongside: **``plotXcorrEvent``**. \n", + "\n", + "* input: event stream, noise stream, stack, maxlag" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "code_folding": [ + 0 + ], + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "# Plotting\n", + "\n", + "plotXcorrEvent(ste, stn, stack, 400)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "celltoolbar": "Slideshow", + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file diff --git a/README.md b/README.md index 7be9dfd..63d3170 100644 --- a/README.md +++ b/README.md @@ -13,3 +13,9 @@ This has been forked from [Seismo Live](http://seismo-live.org). The source code ### 02 - Fourier-Transform and Basic Filtering This has been forked from [Seismo Live](http://seismo-live.org). The source code is available at https://github.com/krischer/seismo_live (licensed under a ***CC BY-NC-SA 4.0 License***. © 2015-2022 Lion Krischer). + +### 03 - Cross Correlation + +1. Simple Cross Correlation: Use the cross-correlation to detect and determine the time shift of two test events with one template event. +2. Enhanced Picker: This has been taken from the [ObsPy Tutorial](https://docs.obspy.org/master/tutorial/code_snippets/xcorr_pick_correction.html). ObsPy is licensed under the [LGPL v3.0](https://www.gnu.de/documents/lgpl-3.0.en.html) +3. Ambient Seismic Noise Analysis from [Seismo Live](http://seismo-live.org). The source code is available at https://github.com/krischer/seismo_live (licensed under a ***CC BY-NC-SA 4.0 License***. © 2015-2019 Lion Krischer).