new assignment 6
This commit is contained in:
1 parent
9ec2cbfe16
commit
cdc4b268be
5 files changed
+409
No files matched your search
@@ -0,0 +1,4 @@
|
||||
CH.PANIX
|
||||
HHZ
|
||||
47598 5.00000007E-02 63621.435000999998
|
||||
6.80219381E-10 9.04806396E-10 1.22883470E-09 1.61028624E-09 2.01385841E-09 2.43196618E-09 2.87917801E-09 3.37038109E-09 3.90618249E-09 4.47728121E-09 5.07713160E-09 5.70454040E-09 6.35287067E-09 7.00171254E-09 7.62611396E-09 8.21675528E-09 8.78707151E-09 9.35383326E-09 9.90858773E-09 1.04143707E-08 1.08410223E-08 1.12094227E-08 1.15954277E-08 1.20755637E-08 1.26545121E-08 1.32418378E-08 1.37085889E-08 1.39821843E-08 1.40986440E-08 1.41660212E-08 1.42723993E-08 1.44207268E-08 1.45436330E-08 1.45731329E-08 1.44907606E-08 1.43119001E-08 1.40326364E-08 1.36087879E-08 1.29996307E-08 1.22321362E-08 1.14091598E-08 1.06368034E-08 9.93180205E-09 9.19890830E-09 8.30608915E-09 7.19291959E-09 5.91877658E-09 4.61872451E-09 3.41785311E-09 2.37327957E-09 1.46083068E-09 5.77004111E-10 -4.52584026E-10 -1.81675641E-09 -3.58748209E-09 -5.58872015E-09 -7.41861061E-09 -8.69760530E-09 -9.39525790E-09 -9.93871474E-09 -1.09302478E-08 -1.26498279E-08 -1.47744954E-08 -1.66158838E-08 -1.77200086E-08 -1.83033322E-08 -1.91020622E-08 -2.07366906E-08 -2.31568027E-08 -2.56729287E-08 -2.75386167E-08 -2.85383273E-08 -2.90597715E-08 -2.96492164E-08 -3.05343981E-08 -3.15690194E-08 -3.25667884E-08 -3.35834294E-08 -3.48051721E-08 -3.61859200E-08 -3.72981539E-08 -3.76526721E-08 -3.72119651E-08 -3.65486166E-08 -3.64089736E-08 -3.70438791E-08 -3.79444849E-08 -3.82670251E-08 -3.75777880E-08 -3.62385570E-08 -3.50880818E-08 -3.47267068E-08 -3.50526932E-08 -3.54279202E-08 -3.52600544E-08 -3.44493500E-08 -3.33443531E-08 -3.23394431E-08 -3.15414432E-08 -3.07890318E-08 -2.99263938E-08 -2.90073281E-08 -2.82290298E-08 -2.76976220E-08 -2.72913248E-08 -2.67683848E-08 -2.60104756E-08 -2.51583021E-08 -2.45012739E-08 -2.42114151E-08 -2.41508378E-08 -2.39208404E-08 -2.31267947E-08 -2.16678657E-08 -1.98414440E-08 -1.81791044E-08 -1.71114749E-08 -1.66781984E-08 -1.64810707E-08 -1.59269185E-08 -1.46108619E-08 -1.25833441E-08 -1.03149658E-08 -8.38510417E-09 -7.11904269E-09 -6.42600684E-09 -5.92441207E-09 -5.22105426E-09 -4.12099599E-09 -2.64542788E-09 -9.14364029E-10 9.60088897E-10 2.88914959E-09 4.77596984E-09 6.52660193E-09 8.11837797E-09 9.64520552E-09 1.12613598E-08 1.30515989E-08 1.49501140E-08 1.68010530E-08 1.85152782E-08 2.01692423E-08 2.19331220E-08 2.38808902E-08 2.58672426E-08 2.76149823E-08 2.89634592E-08 3.00595140E-08 3.12970450E-08 3.30262147E-08 3.52699594E-08 3.76979798E-08 3.98909492E-08 4.16710755E-08 4.32158132E-08 4.48671003E-08 4.68166839E-08 4.89411498E-08 5.09021554E-08 5.23852073E-08 5.32586917E-08 5.35597096E-08 5.33979154E-08 5.29152899E-08 5.23220507E-08 5.19052179E-08 5.19269001E-08 5.24645394E-08 5.33298525E-08 5.41395551E-08 5.44792798E-08 5.40544711E-08 5.27884474E-08 5.08878983E-08 4.88515397E-08 4.73236774E-08 4.67566927E-08 4.70593378E-08 4.75496478E-08 4.73666013E-08 4.60905767E-08 4.40601440E-08 4.20739497E-08 4.07094269E-08 3.98630178E-08 3.89237300E-08 3.73903610E-08 3.53244864E-08 3.32214825E-08 3.14850794E-08 3.00776115E-08 2.86867401E-08 2.71606559E-08 2.56671822E-08 2.43656029E-08 2.29838566E-08 2.08830162E-08 1.77000175E-08 1.39693093E-08 1.09905267E-08 9.86311388E-09 1.04609601E-08 1.13135368E-08 1.06304254E-08 7.68411912E-09 3.39757489E-09 -3.71028541E-10 -2.26225105E-09 -2.40874742E-09 -2.24075825E-09 -3.34516881E-09 -6.32122310E-09 -1.04614610E-08 -1.43632217E-08 -1.69014776E-08 -1.78598416E-08 -1.78967294E-08 -1.80217103E-08 -1.90073823E-08 -2.10803339E-08 -2.39750886E-08 -2.72032050E-08 -3.03121155E-08 -3.29973098E-08 -3.50979263E-08 -3.66033674E-08 -3.77287712E-08 -3.89462009E-08 -4.07928695E-08 -4.34603002E-08 -4.64690082E-08 -4.88166094E-08 -4.96561725E-08 -4.90433223E-08 -4.80918807E-08 -4.82828497E-08 -5.03892501E-08 -5.38602443E-08 -5.71696432E-08 -5.88574274E-08 -5.84655915E-08 -5.67230565E-08 -5.49617774E-08 -5.42662519E-08 -5.49020349E-08 -5.62559279E-08 -5.72089363E-08 -5.67326772E-08 -5.44657581E-08 -5.09943909E-08 -4.76190358E-08 -4.56571776E-08 -4.56930458E-08 -4.72988830E-08 -4.94252461E-08 -5.11280085E-08 -5.20539558E-08 -5.23797148E-08 -5.24088790E-08 -5.22737729E-08 -5.19640757E-08 -5.15110159E-08 -5.10276159E-08 -5.05386275E-08 -4.98193842E-08 -4.84708806E-08 -4.62014889E-08 -4.30683293E-08 -3.94775768E-08 -3.59683732E-08 -3.29570007E-08 -3.05803205E-08 -2.86619617E-08 -2.67823523E-08 -2.44582949E-08 -2.14092637E-08 -1.77743296E-08 -1.40813023E-08 -1.09020215E-08 -8.39979197E-09 -6.14016660E-09 -3.39202511E-09 2.42931647E-10 4.35805969E-09 7.87166599E-09 9.77052395E-09 9.95549243E-09 9.49158530E-09 9.93805926E-09 1.21577468E-08 1.55219571E-08 1.82424316E-08 1.87065794E-08 Line truncated
|
||||
@@ -0,0 +1,27 @@
|
||||
# -------------------------------------------------------------
|
||||
# Functions for "fast" Fourier transform and Fourier synthesis
|
||||
# using numpy tools
|
||||
# -------------------------------------------------------------
|
||||
import numpy as np
|
||||
|
||||
def dft_fast_coeff(x):
|
||||
"""
|
||||
Evaluate Fourier coefficients using Numpy's fast Fourier transform.
|
||||
This routine only returns the coefficients for the positive frequencies.
|
||||
If N is even, it goes up to n=N/2.
|
||||
If N is odd, it goes up to n=(N-1)/2.
|
||||
:param x: array of function samples
|
||||
"""
|
||||
return np.fft.rfft(x, norm='forward')
|
||||
|
||||
|
||||
def dft_fast_synthesis(fc, outnum='even'):
|
||||
"""
|
||||
Use numpy's fast Fourier synthesis taking only Fourier coefficients for positive frequencies
|
||||
:param fc: aray of coefficients for positive frequencies only.
|
||||
:param outnum: specifies if output time series has an even or odd number of samples (default: 'even')
|
||||
"""
|
||||
ns = 2*fc.size-2
|
||||
if outnum == 'odd': ns = 2*fc.size-1
|
||||
return np.fft.irfft(fc, ns, norm='forward')
|
||||
|
||||
@@ -0,0 +1,284 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "d4e32124-6e9e-4467-8726-d2adbb3934dd",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Assignment 6\n",
|
||||
"Here we apply causal filters to a seismological data trace."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c5711338-01f0-4e88-996d-b6026eff5098",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%matplotlib inline\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"from setupFigure import SetupFigure\n",
|
||||
"from dftFast import dft_fast_coeff, dft_fast_synthesis\n",
|
||||
"from seismicTrace import SeismicTrace"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "bff77f1c-1f8a-4908-8419-dfed94f9f177",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Task 1: Function implementing causal filters"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0603d817-6c80-444f-a42d-8318f24fdbd7",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 1.1 Write a function that implements the first order causal low pass filter"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "88aba420-be35-4070-8b6d-386fd924d0e3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def lowpass_first_order(fc, df, nf, napp):\n",
|
||||
" \"\"\"\n",
|
||||
" Calculates samples of the lowpass first order causal filter transfer function (positive frequencies only)\n",
|
||||
" :param fc: corner frequency in Hz\n",
|
||||
" :param df: frequency stepping\n",
|
||||
" :param nf: number of frequencies\n",
|
||||
" :param napp: number of applications of filter\n",
|
||||
" \"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b3e445bf-630a-44f0-ae04-7d4829531b58",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 1.2 Write a function for the causal 2nd order lowpass with filter angle"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "89fda9ef-eca1-484f-9ba2-8ac9046c85f0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def lowpass_second_order(fc, df, nf, phi):\n",
|
||||
" \"\"\"\n",
|
||||
" Calculates samples of the lowpass second order causal filter transfer function (positive frequencies only)\n",
|
||||
" :param fc: corner frequency in Hz\n",
|
||||
" :param df: frequency stepping\n",
|
||||
" :param nf: number of frequencies\n",
|
||||
" :param phi: filter angle in degrees\n",
|
||||
" \"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9ee31665-36cb-4e5c-aa31-f8eb26cc2b30",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 1.3 Write a function fo the Butterworth lowpass of any order"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d21fcbb1-52a3-4fca-8fc8-921034122836",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def butterworth_lowpass(fc, df, nf, order):\n",
|
||||
" \"\"\"\n",
|
||||
" Calculates samples of the lowpass Butterworth filter transfer function (positive frequencies only)\n",
|
||||
" :param fc: corner frequency in Hz\n",
|
||||
" :param df: frequency stepping\n",
|
||||
" :param nf: number of frequencies\n",
|
||||
" :param order: filter order\n",
|
||||
" \"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "3786d68c-4348-4a24-b2f5-83db76c9b414",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Task 2: Application of filters to seismological data"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "259e787f-2f67-43c4-afda-0741087958ad",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 2.1 Read the trace of CH.PANIX as in the previous assignment and plot it."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c9938cab-7ae5-48ed-93d5-83215a350c18",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\"\"\"Here comes your code\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "b015a477-6372-4fc8-a91d-95cb8cbaa73f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 2.2 Compute the fast Fourier coefficients of the data and print number of frequencies and max frequency"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "56eb0313-e1a2-404e-9bc1-0ded3791f345",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\"\"\"Here comes your code\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f4ec9eea-a114-4e33-ac3f-76b7acfbc0ab",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 2.3 Apply the first order lowpass, do Fourier synthesis and plot the result. Use fc=0.1 Hz and vary corner frequency later. Set axis limits to zoom into the P-wave train."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "afc7c27a-173d-4339-b4ac-abfbd6b5e974",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\"\"\"Here comes your code\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "c3ccc36a-e666-4cca-b894-5a4e7c9aa3b7",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 2.4 Apply the first order lowpass 2 times, do Fourier synthesis and plot the result."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "50e6cc4c-4c9c-47a5-ba4f-04808b64db31",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\"\"\" Here comes your code\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f7c19547-cd98-49a2-a6d1-7e14e5a058e5",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 2.5 Apply the first order low pass 3 times, do Fourier synthesis and plot the result"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a54f6ee3-480e-4178-b4d6-04a01d47a3c7",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\"\"\"Here comes your code\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "6b8d3381-5bbe-4558-ae08-6f1c2292716d",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 2.6 Apply the second order lowpass for phi = 5 degrees, do Fourier synthesis and plot the result"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "45fd47e8-0400-4c3d-bb2a-1ba15bb27c84",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\"\"\"Here comes your code\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "251422f2-6af2-4aa6-83cb-aacd31ddde7b",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 2.7 Apply the second order lowpass for phi = 45 degrees, do Fourier synthesis and plot the result"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "0b292eda-a26d-443c-8c58-b16011d63403",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\"\"\"Here comes your code\"\"\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "66f56f5c-6b64-4933-9004-ea2a450f6bf0",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#### 2.8 Setup a loop over filter orders from 1 to 8 and apply the low pass Butterworth filter to the data. Do Fourier synthesis and plot the resulting traces in one graph in such a way that the traces are vertically shifted relative to each other. Here, it is usefull to normalize the traces before plotting."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "0de14535-8b80-4921-95b2-e4cb917f9bac",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\"\"\"Here comes you code\"\"\""
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"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.12.4"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
#----------------------------------------------------------------------------
|
||||
# Python class for a seismic trace
|
||||
#
|
||||
import numpy as np
|
||||
|
||||
class SeismicTrace(object):
|
||||
"""
|
||||
Class defining a seismic trace by station name, network name, component,
|
||||
number of samples, sampling interval and start time
|
||||
"""
|
||||
|
||||
def __init__(self, staname, comp, dt, tanf, vals):
|
||||
"""
|
||||
Initialize seismic trace object from data:
|
||||
:param staname: station name
|
||||
:param comp: component
|
||||
:param dt: sampling interval
|
||||
:param tanf: start time
|
||||
:param vals: values of trace (1D numpy array)
|
||||
"""
|
||||
self.staname = staname
|
||||
self.comp = comp
|
||||
self.dt = dt
|
||||
self.tanf = tanf
|
||||
self.vals = vals
|
||||
self.nsamp = vals.size
|
||||
self.tlen = self.dt*self.nsamp
|
||||
|
||||
|
||||
@classmethod
|
||||
def readFromAscii(cls, fid, comp='None'):
|
||||
"""
|
||||
Class method as second constructor to initialize trace from an Ascii file
|
||||
:param fid: file descriptor of already open Ascii file
|
||||
:param comp: optional component selection
|
||||
:return: an instance of the class
|
||||
"""
|
||||
staname = fid.readline().strip()
|
||||
if not staname:
|
||||
print("No station name found. Is file really an Ascii gather file")
|
||||
return None
|
||||
compf = fid.readline().strip()
|
||||
if comp != 'None' and comp != compf:
|
||||
print("Requested component %s not found.", comp)
|
||||
return None
|
||||
h = fid.readline().strip().split()
|
||||
vals = np.array([float(v) for v in fid.readline().strip().split()])
|
||||
dt = float(h[1]); tanf = float(h[2])
|
||||
return cls(staname, compf, dt, tanf, vals)
|
||||
|
||||
|
||||
def absoluteMaximum(self):
|
||||
"""
|
||||
return absolute maximum of trace
|
||||
"""
|
||||
return abs(max(self.vals))
|
||||
|
||||
|
||||
def writeToOpenAsciiFile(self, fid):
|
||||
"""
|
||||
write a seismic trace to an open Ascii file
|
||||
"""
|
||||
fid.write(self.staname + '\n')
|
||||
fid.write(self.comp + '\n')
|
||||
fid.write("{:12d} {:15.9f} {:20.9f}\n".format(self.nsamp, self.dt, self.tanf))
|
||||
for n in range(self.nsamp):
|
||||
fid.write("{:17.8e}".format(self.vals[n]))
|
||||
fid.write("\n")
|
||||
|
||||
|
||||
def printInfo(self):
|
||||
"""
|
||||
Print out some information about the trace
|
||||
"""
|
||||
print("Identification: ", self.staname, self.comp)
|
||||
print("Sampling interval: ", self.dt)
|
||||
print("Number of samples: ", self.nsamp)
|
||||
print("Start time after midnight: ", self.tanf)
|
||||
print("Length of trace: (N*DT) ", self.tlen)
|
||||
@@ -0,0 +1,15 @@
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
def SetupFigure(wx, wy, xlabel, ylabel, title, tls):
|
||||
"""
|
||||
Create a new figure frame
|
||||
"""
|
||||
fig = plt.figure(figsize=(wx,wy))
|
||||
ax = fig.add_subplot(1,1,1)
|
||||
ax.set_title(title, fontsize=tls+2)
|
||||
ax.grid(which='major', axis='both', ls=':')
|
||||
ax.set_ylabel(ylabel, fontsize=tls)
|
||||
ax.set_xlabel(xlabel, fontsize=tls)
|
||||
ax.tick_params(labelsize=tls)
|
||||
return fig, ax
|
||||
Reference in new issue
Block a user