solutions for assignments 4 and 5 added

This commit is contained in:
wolle committed 2025-05-27 13:52:58 +02:00
1 parent 7d6d611f64
commit 9327a231a9
3 files changed
+939

No files matched your search

+439
View File
@@ -0,0 +1,439 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "1559cf77-eb5b-4e56-8bd9-ae61a108fe92",
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from setupFigure import SetupFigure\n",
"from dftSlow import dft_coeff, dft_synthesis"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b101d00c-d9a2-4222-879b-ac340f89f576",
"metadata": {},
"outputs": [],
"source": [
"def dft_fast_coeff(x):\n",
" \"\"\"\n",
" Evaluate Fourier coefficients using Numpy's fast Fourier transform.\n",
" This routine only returns the coefficients for the positive frequencies.\n",
" If N is even, it goes up to n=N/2.\n",
" If N is odd, it goes up to n=(N-1)/2.\n",
" :param x: array of function samples\n",
" \"\"\"\n",
" return np.fft.rfft(x, norm='forward')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "14467eda-e7f2-423d-9b78-b7cc9672f22c",
"metadata": {},
"outputs": [],
"source": [
"def dft_fast_synthesis(fc, outnum='even'):\n",
" \"\"\"\n",
" Use numpy's fast Fourier synthesis taking only Fourier coefficients for positive frequencies\n",
" :param fc: aray of coefficients for positive frequencies only.\n",
" :param outnum: specifies if output time series has an even or odd number of samples (default: 'even')\n",
" \"\"\"\n",
" ns = 2*fc.size-2\n",
" if outnum == 'odd': ns = 2*fc.size-1\n",
" return np.fft.irfft(fc, ns, norm='forward')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "18ffbc73-5210-4b9a-b3e2-28d5aafd33d9",
"metadata": {},
"outputs": [],
"source": [
"def boxcar(dt, period, tup, tdown):\n",
" \"\"\"\n",
" Calculate samples of a boxcar function\n",
" :param dt: sampling interval\n",
" :param period: time range is 0 <= t < period (multiple of dt)\n",
" :param tup: time where boxcar goes from 0 to 1\n",
" :param tdown: time where boxcar goes from 1 to 0\n",
" \"\"\"\n",
" ns = int(period/dt)\n",
" t = dt*np.arange(0, ns)\n",
" return t, np.where(t < tup, 0, 1)*np.where(t > tdown, 0, 1)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3435e191-57a4-425a-9618-a7597b11916d",
"metadata": {},
"outputs": [],
"source": [
"def gaussian(dt, period, tmax, hwidth):\n",
" \"\"\"\n",
" Calculate samples of a Gaussian function\n",
" :param dt: sampling interval\n",
" :param period: time range is 0 <= t < period (multiple of dt)\n",
" :param tmax: time of maximum of Gaussian\n",
" :param hwidth: half width of Gaussian\n",
" \"\"\"\n",
" ns = int(period/dt)\n",
" t = dt*np.arange(0, ns)\n",
" return t, np.exp(-(t-tmax)**2/hwidth**2)"
]
},
{
"cell_type": "markdown",
"id": "8a7e3fe9-484a-49cc-af35-03b532eb62cd",
"metadata": {},
"source": [
"## Task 1: Compare \"slow\" Fourier transform with the fast version of numpy"
]
},
{
"cell_type": "markdown",
"id": "971cc8b7-7ade-4755-b5ad-9cc83e66fed5",
"metadata": {},
"source": [
"Again set up the boxcar function as in the previous assignment and use the provided functions to compute the Fourier coefficients by both methods. Verify that both methods yield the same results by printing the first 20 coefficients."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f5da468c-fded-40b4-a691-6adc93fbf110",
"metadata": {},
"outputs": [],
"source": [
"t, bx = boxcar(1, 1000, 300, 500)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "fe9a16bd-1f5c-4a28-9b4b-ef7274c5f529",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1000 1000 501\n",
" 0 2.010000e-01+0.000000e+00j 2.010000e-01+0.000000e+00j\n",
" 1 -1.520194e-01-1.104485e-01j -1.520194e-01-1.104485e-01j\n",
" 2 4.686934e-02+1.442490e-01j 4.686934e-02+1.442490e-01j\n",
" 3 3.108655e-02-9.567460e-02j 3.108656e-02-9.567460e-02j\n",
" 4 -3.718483e-02+2.701637e-02j -3.718484e-02+2.701636e-02j\n",
" 5 -9.999970e-04+9.855762e-10j -1.000000e-03+4.012337e-20j\n",
" 6 2.587908e-02+1.880226e-02j 2.587908e-02+1.880225e-02j\n",
" 7 -1.345747e-02-4.141783e-02j -1.345747e-02-4.141783e-02j\n",
" 8 -1.159567e-02+3.568779e-02j -1.159566e-02+3.568778e-02j\n",
" 9 1.615938e-02-1.174048e-02j 1.615938e-02-1.174048e-02j\n",
" 10 1.000003e-03-1.096195e-09j 1.000000e-03-5.552491e-20j\n",
" 11 -1.440952e-02-1.046912e-02j -1.440952e-02-1.046913e-02j\n",
" 12 7.887543e-03+2.427536e-02j 7.887542e-03+2.427536e-02j\n",
" 13 7.096590e-03-2.184102e-02j 7.096579e-03-2.184102e-02j\n",
" 14 -1.015033e-02+7.374651e-03j -1.015033e-02+7.374646e-03j\n",
" 15 -9.999993e-04-1.489762e-11j -1.000000e-03-1.916335e-20j\n",
" 16 1.010687e-02+7.343075e-03j 1.010687e-02+7.343074e-03j\n",
" 17 -5.593138e-03-1.721391e-02j -5.593137e-03-1.721390e-02j\n",
" 18 -5.096129e-03+1.568428e-02j -5.096130e-03+1.568428e-02j\n",
" 19 7.302622e-03-5.305668e-03j 7.302625e-03-5.305668e-03j\n"
]
}
],
"source": [
"c = dft_coeff(bx) # slow DFT\n",
"fc = dft_fast_coeff(bx) # fast DFT\n",
"print(t.size, c.size, fc.size)\n",
"for n in range(t.size//50):\n",
" print(\"{:6d} {:15.6e} {:15.6e}\".format(n, c[n], fc[n]))"
]
},
{
"cell_type": "markdown",
"id": "bc4a9050-a171-479e-90a5-df7b924143a9",
"metadata": {},
"source": [
"Also compare the slow and fast versions of Fourier synthesis by calling the provided functions. Print values at times around the discontinuities."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "a8abee97-2424-445d-81c3-98353d5ac270",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1000 1000\n",
" 290 -1.053945e-07 -6.280549e-17\n",
" 291 -8.330658e-08 1.449411e-17\n",
" 292 -5.615582e-08 -1.440881e-16\n",
" 293 -5.668051e-08 -1.612420e-16\n",
" 294 -7.121410e-08 3.892420e-18\n",
" 295 -8.654331e-08 -5.549463e-17\n",
" 296 -1.116903e-07 4.558707e-18\n",
" 297 -9.428106e-08 1.432295e-16\n",
" 298 -1.238188e-07 3.231428e-17\n",
" 299 -1.090360e-07 1.056715e-16\n",
" 300 9.999999e-01 1.000000e+00\n",
" 301 9.999999e-01 1.000000e+00\n",
" 302 9.999999e-01 1.000000e+00\n",
" 303 9.999999e-01 1.000000e+00\n",
" 304 9.999999e-01 1.000000e+00\n",
" 305 9.999999e-01 1.000000e+00\n",
" 306 9.999999e-01 1.000000e+00\n",
" 307 9.999999e-01 1.000000e+00\n",
" 308 9.999999e-01 1.000000e+00\n",
" 309 9.999999e-01 1.000000e+00\n",
" 310 9.999999e-01 1.000000e+00\n"
]
}
],
"source": [
"xr = dft_synthesis(c)\n",
"if t.size%2 == 0:\n",
" xrf = dft_fast_synthesis(fc, outnum='even')\n",
"else:\n",
" xrf = dft_fast_synthesis(fc, outnum='odd')\n",
"print(xr.size, xrf.size)\n",
"for k in range(290,311):\n",
" print(\"{:6d} {:15.6e} {:15.6e}\".format(k, xr[k], xrf[k]))"
]
},
{
"cell_type": "markdown",
"id": "1204f35f-7933-490e-94a7-af77edaca83c",
"metadata": {},
"source": [
"## Task 2: Interpolation by appending zeros to the Fourier coefficients"
]
},
{
"cell_type": "markdown",
"id": "accff2d0-77ce-4048-bad0-8396c62ef675",
"metadata": {},
"source": [
"A band limited time series can be interpolated using a simple trick: First, the Fourier coefficients are computed up to the Nyquist frequency, $f_{Ny} = \\frac{N}{2}\\Delta f = \\frac{N}{2T}$. Then, $L$ zero coefficents are appended to increase the Nyquist frequency to $f'_{Ny} = (\\frac{N}{2}+L)\\Delta f$ and to decrease the sampling interval to $\\Delta t' = \\frac{1}{2f'_{Ny}}$. Subsequent Fourier synthesis produces an interpolated version of the original time series. These relations hold for even and odd number of samples.\n",
"When doing the Fourier synthesis, the routine should be called with outnum='odd' for odd N and with outnum='even' for even N, respectively.\n",
"\n",
"First set up a Gaussian using the provided function. Then compute the Fourier coefficients. Print out the number of samples, the Nyquist frequency and the number of Fourier coefficients. For example, choose dt=5, period=100, tmax=50, hw=20. \n",
"\n",
"Second, append some zeros (20) to the array of coefficients and compute the new Nyquist frequency and the new sampling interval. Do the Fourier synthesis and print the new number of samples, the new Nyquist frequency and the new sampling interval.\n",
"\n",
"Third, plot the new and old time series into one graph."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "7c5663c1-5605-45df-8342-2dcefb2ec4b0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"N = 20 Nyquist frequency = 0.1 Ncoeff = 11\n"
]
}
],
"source": [
"dt = 5; period = 100; tmax = 50; hw = 20; nz = 20\n",
"t, g = gaussian(dt, period, tmax, hw)\n",
"c = dft_fast_coeff(g)\n",
"print(\"N = \", t.size,\" Nyquist frequency = \", t.size/(2*period),\" Ncoeff = \", c.size)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "2ad7ff56-8713-4386-9b4a-9228fb63fde0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"N = 60 Nyquist frequency = 0.3\n"
]
}
],
"source": [
"cz = np.append(c, np.zeros(nz))\n",
"fny = (t.size/2+nz)/period\n",
"dtnew = 1./(2*fny)\n",
"if t.size%2 != 0:\n",
" xr = dft_fast_synthesis(cz, outnum='odd')\n",
"else:\n",
" xr = dft_fast_synthesis(cz, outnum='even')\n",
"tz = dtnew*np.arange(0, xr.size)\n",
"print(\"N = \", tz.size,\" Nyquist frequency = \", fny)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "2755badf-b7d1-47f0-ab3b-1a057faaf921",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[<matplotlib.lines.Line2D at 0x107e34350>]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA1wAAAHiCAYAAAD8us3oAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/TGe4hAAAACXBIWXMAAA9hAAAPYQGoP6dpAAChQUlEQVR4nOzdeXhU1eH/8ffMhH0JKCoIERQMiyiKBFQERCEgtloR942qdWsVrdVWWr7WHxa7aKt2cRdxXxB3JAHZQUgQFJFd2QkqsgSEJDN3zu+PS4aELExuMrn3Tj6v58mTm1nPmfvJmTlzzj03YIwxiIiIiIiISI0Lul0AERERERGRZKUOl4iIiIiISIKowyUiIiIiIpIg6nCJiIiIiIgkiDpcIiIiIiIiCaIOl4iIiIiISIKowyUiIiIiIpIg6nCJiIiIiIgkiDpcIiIiIiIiCaIOl4hIDevQoQOBQIAXX3zR7aL4UvHrt379ereLUi0jR46sVg527tzJP/7xDwYPHsyxxx5LgwYNaNKkCR06dODCCy/kiSeeYNu2bTVbaI9Zv349gUCADh06uF0UERHH1OESEfG4mTNnEggEOOecc9wuii8FAgECgYDbxaiSV199lfbt23Pfffcxa9Ys2rdvz0UXXcQFF1xAu3btyM7OZtSoUXTo0IG33nrL7eKKiEglUtwugIiIiBz05JNPcvvttxMIBLjvvvv4wx/+QMuWLUvdZv/+/bz++us8/PDDfPvtty6VNPHatm3LihUrqFevnttFERFxTB0uERERj1ixYgWjRo0C4PHHH+eOO+4o93aNGjXihhtu4Morr2Tt2rW1WcRaVa9ePbp06eJ2MUREqkVTCkVEasmf//xnAoEAf/7zn/nhhx/49a9/TVpaGvXr1yctLY077riDXbt2lbrPOeecw8CBAwGYNWtWbHpcRce1fPrppwwfPpw2bdpQv359jj76aC6++GI+++yzcstUcrrd+PHjOfPMM0lNTS11DNU555xDIBBg5syZzJo1i8zMTI444ggaN25M7969efnllyuscyQS4amnnuKss84iNTWVhg0bcuKJJ3LnnXeyZcuWKr1+GzZs4G9/+xvnnnsuxx13HA0aNKBFixacffbZPP3000Sj0VK3L369D61r8c+hx4itXr2aW265hY4dO9KwYUNSU1Pp378/r7zySoVl2rFjB3fddRft27enQYMGHHfccfzmN79hx44dVapbsb/97W+Ew2F69uxZYWerpEaNGnHyySeXuXzSpEncdNNNdO/enZYtW9KwYUOOP/54brjhBlatWlXuY5Xcz+Upmd+SotEozzzzDH379qVFixbUq1ePo48+mh49enDHHXeUeZ3z8vIYNWoU6enpNGzYkMaNG5OWlsZ5553HI488Uuq2lR3DlZOTw3333Ufv3r1p3bo19evX55hjjuHnP/8506ZNK7cOL774IoFAgJEjR/LTTz9x//3306lTJxo0aEDr1q25/vrrq5xLEZHD0QiXiEgt27RpEz179iQcDtO3b18KCgqYN28e//nPf1i4cCHz5s2LTaEaOnQoDRs2JCsri2OOOYahQ4fGHqdVq1alHvd3v/sdjz76KMFgkF69etGvXz82btzI+++/z4cffsizzz7LL3/5y3LLdMcdd/C///2Ps846iwsuuIBvv/22zHFP7777Lv/5z3/o0qULQ4YMYevWrcydO5frrruOL774gkcffbTU7QsLC/nZz37GtGnTaNiwIQMHDqR58+bMnz+ff//737z++utkZWXRs2fPuF63l19+mTFjxnD88ceTnp5O3759ycvL47PPPmPevHlkZ2czceLEWLlPPfVUrr/+eiZMmADA9ddfX+rxmjZtGtt+++23ue666ygoKKBLly4MGzaM3bt3s3DhQq699lqmT5/OCy+8UOr+3333Hf369WPNmjW0bNmSn/3sZ0SjUV599VWmTJnCSSedFFe9ihlj+PDDDwG45pprqnTfQ1122WU0aNCAbt26ce655xKJRFi2bBnjx4/nrbfeIjs7m7POOqtaz1HspptuYvz48TRs2JCzzz6bo446ih07dvDtt9/yn//8h/POOy/WYdq2bRu9evVi69atHHfccbF8b926lS+++ILPP/+c3/3ud3E97+jRo5kxYwYnnXQSp59+Ok2aNOGbb77ho48+4qOPPuKxxx6LjRYeavfu3Zx11lls3LiRfv360b17dz777DNeeuklZ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WLFnC1q1b6dixI6effnq5tyleUn7+/PnlXj9ixIjDPs91111X7uXXX389jz76KHPnzsWyLEKhEIsWLWLv3r0cccQR/PznPy9zn8aNG3PFFVfw+OOPM2PGDK666qrDPj/Y0xSzs7PJzc3l+++/p7CwEGMMe/bsAajw+KSKRKNRPvnkE8DupJSnV69eNG3alCVLllBQUEDDhg2ZPXs20WiU008/nW7dupW5z6mnnsopp5zC0qVLq1Sew/nwww/5/PPPS13Wvn37Msc+Aaxdu5YpU6awdu1a9uzZg2VZwMFOz6pVq8ote1V06dKFZs2aMXnyZP7yl79w1VVXcfzxx1d4+969e/O///2PP/zhDxhjyMzMLDW1s6p+/PFHPv74Y5YtW8bOnTsJh8MArFmzBqg4D7169Sq3s+b0f1BEpDLqcImI1LKS3/iXVPxte0FBQZUer3hZ8G+++eaw55v64Ycfyr08nhPLVvRBuvjy/fv38+OPP3L00UfHPrBW9uG7Y8eOQPwfbhcsWMDll1/Oxo0bK7xNfn5+XI9V7Mcff4zdJy0tLa7bt23bls2bNwOV1+/444+vUoer5DF5P/zwA507dy5zm0WLFsW2586dS79+/crcxrIsfvOb3/D0009jjKnw+ar6WpWnWbNmjB8/nl/+8pf86U9/4k9/+hNt2rThjDPOYOjQoVx11VWlOlTXXnstU6dO5dVXX+WSSy4hFArRrVs3zj77bEaMGMG5554b93M/++yz3H333fz0008V3qaiOtb0/6CISGXU4RIRqWXBYM3O5i5ena9169YMGTKk0tseutBGsUaNGtVIWSr7gF8d+/bt4xe/+AXfffcdv/zlL7ntttvo1KkTzZs3JxQKsXr1ajp37lzl5y+5suGhi2qUp+Q00JrWoUMHjjjiCHbs2MGiRYs4++yzHT3O448/zlNPPUXr1q355z//yVlnncUxxxxDw4YNAbjqqqt4/fXXq/ValXTJJZcwaNAgPvjgA+bMmcO8efN49913effdd/m///s/pk6dGltJMRgM8sorrzB69Gg+/vhj5s2bx7x583jyySd58skn+fnPf867775LKBSqtCyff/45t9xyC6FQiL/97W/8/Oc/57jjjqNx48YEAgGeeeYZbrnllgrrWNP/gyIilVGHS0TE54pHZo488khefPHFhD3PunXryr28eNnvhg0bcuSRRwL2CWsruw8cHJkrvm1lZs+ezXfffUfPnj3LrBYIB6eQVVWrVq1o1KgR+/fv55FHHqmwQ3qo4jIfuuR5SZVdV55gMMgFF1zAyy+/zCuvvMJdd91VpfsXe+uttwB4+umnufDCC8tcX9FrVb9+fYDY9MxDbdiwocLnTE1N5dprr+Xaa68F7GPc7rjjDt5//31+85vfMGvWrFK379atG926dePee+/FGMP06dO56qqr+PDDD3nppZcqXE2z2Ntvv40xhjvuuIP77rsv7jqKiLhBX/GIiHhc8QfhSCRS7vUZGRm0atWK5cuX8/XXXyesHBWdj+qll14C4OyzzyYlxf4er/i4px07dvDBBx+Uuc/+/ft54403AGLnGatM8XmtKpoKVtm5soqX2C/v9QuFQgwePBg42FGJR//Line truncated
"text/plain": [
"<Figure size 1000x500 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fig1, ax1 = SetupFigure(10, 5, \"Time [s]\", \"xr(t)\", \"Interpolated Gaussian\", 14)\n",
"ax1.plot(tz, xr, color='blue', ls='-', lw=0.25, marker='d', markersize=4, zorder=10)\n",
"ax1.plot(t, g, color='red', ls='', marker='o', markersize=6, zorder=5)"
]
},
{
"cell_type": "markdown",
"id": "fc9fd425-811c-49d1-8372-1fb2963a8310",
"metadata": {},
"source": [
"## Task 3: Aliasing and the sampling theorem"
]
},
{
"cell_type": "markdown",
"id": "a1cf5112-c521-46d9-b642-4c3fbe3fa0ee",
"metadata": {},
"source": [
"First, calculate values for a Gaussian with dt=0.25, period=100, tmax=50 and hwidth=1. Plot the Gaussian.\n",
"\n",
"Second, in a loop, calculate sme Gaussian with dt = 0.5, 1.0, 1.5 and 2.0. Compute the fast Fourier coefficients and the frequencies associated with them. Plot the absolute value of the coefficients into one graph. Set the upper frequency axis limit to 1.0 and use different colors for the curves. Compare the spectra, what do you observe?"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "9e8596d7-1eb5-4248-85c5-e90ed8beaeb8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[<matplotlib.lines.Line2D at 0x107ea6c90>]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA2gAAAFICAYAAAAlJd0iAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/TGe4hAAAACXBIWXMAAA9hAAAPYQGoP6dpAABp70lEQVR4nO3deXhTVf4G8Pcm6b5RNoECZRUYAdmKsoNCcYCfuDGAohQRl1FnHBkV3EBHUBR1HNRRRFl0FFDcqVJkK1CBKpsIaNkLlE3o3qa5uef3R0ggJC056ZLc3vfzPH043NzcnGNeG745596rCCEEiIiIiIiIKOBMge4AERERERERObBAIyIiIiIiChIs0IiIiIiIiIIECzQiIiIiIqIgwQKNiIiIiIgoSLBAIyIiIiIiChIs0IiIiIiIiIIECzQiIiIiIqIgwQKNiIiIiIgoSLBAIyIiD1lZWXjooYfwpz/9CVFRUQgPD0fTpk2RlJSEhx56CMuWLfN4TosWLaAoCg4dOlTzHQ6Q48ePY/r06ejXrx8aNWqE0NBQxMTEoG3btrjtttswb9485ObmBrqb1Wrt2rVQFAUDBw4MdFeIiGoFRQghAt0JIiIKHp9//jluv/12WK1W1KtXD926dUODBg1w7tw5bN++HTk5OahXrx7OnDnj9rwWLVrg8OHDOHjwIFq0aBGYzteg2bNn4+mnn4bVakVERASSkpLQuHFjqKqKI0eOYOvWrbDb7YiNjcXy5cvRt2/fQHe5WqxduxaDBg3CgAEDsHbt2kB3h4hI9yyB7gAREQWPkydPYvz48bBarZg8eTJeeOEFhIeHu+3z888/47PPPgtQD4PDE088gZdffhmhoaF4+eWX8dBDDyEiIsJtn7y8PMyfPx+zZs3C0aNHA9TT6tezZ0/s2bMHkZGRge4KEVGtwBk0IiJyef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ocdzVq9ejdTUVI/zwIQQ+PbbbwG4F1Fvv/2214uynDhxwjUeb0XXpZxjnDt3LsrKylzb8/LyMH78eOTl5V32GERERsSLhBARkYvNZsOiRYuwaNEiNGjQAF27dkX9+vWRm5uL3bt3u5YEjhs3DhMnTqz063Xt2hUjR47EV199ha5du2LgwIGoW7cutm/fjt9++w1PPvkkZsyYUenX+fvf/46xY8fitddeQ9u2bbF//35s3rwZADB79myvN7euyOzZs1G/fn1MmzYNkydPxjPPPIOkpCQ0btwYiqIgJycHP/30EwoLCxETE4NRo0a5PX/atGm47bbb8Mwzz2Dp0qW46qqrcOrUKaxfvx79+vVDkyZNvBbJDz74IN577z1s3boV7dq1Q69evVBUVITMzEw0b94cN910k8e5gTt37sQ//vEPxMbGolu3bmjSpAlKSkqwdetWHD58GHFxcXj++edd+8+dOxcPPvggWrZsiY4dOyI2NhanT5/G+vXrUVJSguuuu851HmJFHnnkESxatAipqalo1aoVrr32WthsNqxbtw6RkZG4++678cEHH0j9dyciMgLOoBERkcvEiRPx5Zdf4uGHH0bLli2xe/dufPrpp1izZg3MZjPGjh2L7777Dh9++GGVXcTh008/xSuvvIJ27dphw4YNSEtLQ/PmzbFLine truncated
"text/plain": [
"<Figure size 1000x300 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"t, g = gaussian(0.25, 100, 50, 1)\n",
"fig3, ax3 = SetupFigure(10, 3, \"Time [s]\", \"g(t)\", \"Sharp Gaussian\", 14)\n",
"ax3.plot(t, g, color='blue', ls='-', lw=1.0)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "83511cfe-45e9-4216-96c7-f932d6199034",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.legend.Legend at 0x107f681d0>"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA4QAAAHiCAYAAACwfAJVAAAAOXRFWHRTb2Z0d2FyZQBNYX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Z14IIYSoGGV5MH2+K1euKK+88opSp04dxdnZWXF3d1fat2+vTJ06VcnLyytyH5PJpHz55ZdK48aNFWdnZ8XHx0d58sknlUOHDt33OYR/Xl9cH4p7jlxkZKTyzDPPKEFBQYqTk5Pi5eWlNG7cWBk+fLiyaNEiJTMz07Jt/vPsateuXeqfR3H9XbRokdK7d2+latWqipOTk+Lv76906tRJ+fzzzws8pzHfokWLlK5duypeXl6Kk5OTUrNmTWXs2LHKmTNniv0co9GoLFq0SOnfv7/i5+enODk5KVWrVlWaNm2qjBs3Tlm1alWZn7vHAzyHMF9cXJzy+uuvKw0bNlRcXV2VKlWqKA0aNFDGjx+v7Nmzp0ztKO/zVtznGY1GZcqUKUqTJk0UV1dXS/+3b9+u5OXlKdOmTVNGjBihNGrUSPH09FTc3NyUunXrKoMHD1a2bdtW7OcJIcSjRqMoilIRA1EhhBBCCCGLine truncated
"text/plain": [
"<Figure size 1000x500 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"fig2, ax2 = SetupFigure(10, 5, \"Frequency [s]\", \"G(f)\", \"Fourier coefficients\", 14)\n",
"ax2.set_xlim(0, 1.0)\n",
"col = ['grey', 'orange', 'red', 'magenta', 'blue']\n",
"lw = [4, 1, 1, 1, 1]\n",
"for i, dt in enumerate([0.25, 0.5, 1.0, 1.5, 2.0]):\n",
" t, g = gaussian(dt, 100, 50, 1)\n",
" c = dft_fast_coeff(g)\n",
" f = 1./period*np.arange(0, c.size)\n",
" ax2.plot(f, np.absolute(c), color=col[i], ls='-', lw=lw[i], label=\"DT = {:5.2f}\".format(dt))\n",
"ax2.legend() "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b8b80cd5-d9b7-40e3-858c-ed2086167c37",
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}
+34
View File
@@ -0,0 +1,34 @@
# -------------------------------------------------------------
# Functions for acausal filtering
# -------------------------------------------------------------
import numpy as np
def boxcar_lowpass_filter(fc, df, nf):
"""
Calculates samples of the boxcar lowpass filter transfer function (positive frequencies only)
:param fc: corner frequency in Hz
:param df: frequency stepping
:param nf: number of frequencies
"""
return np.where(df*np.arange(0, nf) > fc, 0, 1)
def boxcar_highpass_filter(fc, df, nf):
"""
Calculates samples of the boxcar highpass filter transfer function (positive frequencies only)
:param fc: corner frequency in Hz
:param df: frequency stepping
:param nf: number of frequencies
"""
return np.where(df*np.arange(0, nf) < fc, 0, 1)
def hann_lowpass_filter(fc, df, nf):
"""
Calculates samples of the Hann filter transfer function (positive frequencies only)
:param fc: corner frequency in Hz
:param df: frequency stepping
:param nf: number of frequencies
"""
f = df*np.arange(0, nf)
return np.where(f < fc, np.cos(np.pi*f/fc)**2, 0)
+466
View File
@@ -0,0 +1,466 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "0ff7abb1-c698-480b-a894-bdbb5d151fe6",
"metadata": {},
"source": [
"## Assignment 5\n",
"In this assignment, we look at spectral estimation and study the effects of acausal filters."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "156c8712-11be-4721-9d39-ff2c47a66bcb",
"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": "f98951f5-96dc-4ef5-b365-85bffa21f700",
"metadata": {},
"source": [
"### Task 1: Functions for Hann window and filter transfer functions\n",
"Write a function that calculates samples of a Hann window for given sampling interval and window length."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c36eb15e-26e7-4e6e-bef9-8d566dc07411",
"metadata": {},
"outputs": [],
"source": [
"def hann_window(dt, tlen):\n",
" \"\"\"\n",
" Calculate samples of the Hann window for given DT and TLEN.\n",
" Number of samples is assumed to be int(tlen/dt)\n",
" :param dt: sampling interval\n",
" :param tlen: length of window\n",
" \"\"\"\n",
" ns = int(tlen/dt)\n",
" return 2*np.sin(np.pi*dt*np.arange(0, ns)/tlen)**2"
]
},
{
"cell_type": "markdown",
"id": "60dffc7b-17a1-40aa-8a6f-918504e06af2",
"metadata": {},
"source": [
"Write a function that calculates samples of the boxcar lowpass filter transfer function for positive frequencies only."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "be77830b-6b2f-4371-9711-e83a1598c363",
"metadata": {},
"outputs": [],
"source": [
"def boxcar_lowpass_filter(fc, df, nf):\n",
" \"\"\"\n",
" Calculates samples of the boxcar lowpass 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",
" \"\"\"\n",
" return np.where(df*np.arange(0, nf) > fc, 0, 1)"
]
},
{
"cell_type": "markdown",
"id": "33eb7b1d-4abd-42a2-99ec-cdd1765fbfde",
"metadata": {},
"source": [
"Write a function that calculates samples of the boxcar highpass filter transfer function for positive frequencies only."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29c56269-025a-43e9-8748-c0514a72f70d",
"metadata": {},
"outputs": [],
"source": [
"def boxcar_highpass_filter(fc, df, nf):\n",
" \"\"\"\n",
" Calculates samples of the boxcar highpass 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",
" \"\"\"\n",
" return np.where(df*np.arange(0, nf) < fc, 0, 1)"
]
},
{
"cell_type": "markdown",
"id": "937b6b14-4d8f-40ed-bc71-86888d6e726c",
"metadata": {},
"source": [
"Write a function that calculates samples of the Hann filter transfer function for positive frequencies only."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0101ff10-d14d-4b96-83f8-b26f96e5ec8d",
"metadata": {},
"outputs": [],
"source": [
"def hann_lowpass_filter(fc, df, nf):\n",
" \"\"\"\n",
" Calculates samples of the Hann 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",
" \"\"\"\n",
" f = df*np.arange(0, nf)\n",
" return np.where(f < fc, np.cos(np.pi*f/fc)**2, 0) "
]
},
{
"cell_type": "markdown",
"id": "d641182d-5526-45a9-a69a-0a6965fd942b",
"metadata": {},
"source": [
"### Task 2: Spectral estimation and windowing\n",
"Here, we first setup a time series that consists of a superposition of different exponentially damped sine functions simulating a series of free oscillations:\n",
"\\begin{align}\n",
"s(t) = \\sum_{k=0}^N A_k\\sin(2\\pi f_k t)\\,\\exp(-\\pi f_k t/Q_k) \\,.\n",
"\\end{align}\n",
"The aim is to find the frequencies by Fourier analysis."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eba91f73-1651-4fd7-ad90-70980b1c5d4d",
"metadata": {},
"outputs": [],
"source": [
"feig = np.array([0.3, 0.48, 0.64, 0.68, 0.82, 0.85, 0.93, 0.94])*1.e-3 # eigenfreqeuncies in Hz\n",
"amp = np.array([1.0, 0.30, 2.00, 0.61, 4.00, 0.53, 3.00, 0.24]) # amplitudes\n",
"phi = np.array([ 11, 46, 271, 123, 337, 173, 65, 221])*np.pi/180. # phases\n",
"q = np.array([600, 200, 2000, 1000, 600, 100, 800, 900]) # quality factors"
]
},
{
"cell_type": "markdown",
"id": "e455172a-50ad-4aa4-a1b3-d29d50a52b49",
"metadata": {},
"source": [
"Write code that impements the above equation using the provided frequencies, amplitudes, phases and Q values. Choose dt=100 s and allow a varaible time series length of tens of hours. Plot the resulting time series."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c808e6c4-8e39-4e7c-a689-860230ff775a",
"metadata": {},
"outputs": [],
"source": [
"nhour = 100\n",
"dt = 100.0\n",
"tlen = 3600*nhour\n",
"ns = int(tlen/dt)\n",
"print(\"Number of samples: \", ns)\n",
"t = dt*np.arange(0, ns)\n",
"seis = np.zeros(ns)\n",
"for j, f in enumerate(feig):\n",
" seis = seis + amp[j]*np.sin(2*np.pi*f*t+phi[j])*np.exp(-np.pi*f*t/q[j])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3d0dbe36-34e8-486d-bc63-f5be4aff2d0c",
"metadata": {},
"outputs": [],
"source": [
"fig1, ax1 = SetupFigure(12, 5, \"Time [h|\", \"s(t)\", \"Ground motion\", 14)\n",
"ax1.plot(t/3600, seis, color='blue', ls='-')"
]
},
{
"cell_type": "markdown",
"id": "7ae65e6e-0f73-42fc-b251-878f0ef7f36c",
"metadata": {},
"source": [
"Produce a second time series by multiplying the original one with a Hann window and also plot it."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b62c1a43-f57e-4397-a8f9-7ec813db1e9d",
"metadata": {},
"outputs": [],
"source": [
"win = hann_window(dt, tlen)\n",
"seisw = seis*win"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41193b3d-110b-42a4-b2ee-9ee0dbd759af",
"metadata": {},
"outputs": [],
"source": [
"fig2, ax2 = SetupFigure(12, 5, \"Time [h|\", \"swin(t)\", \"Windowed ground motion\", 14)\n",
"ax2.plot(t/3600, seisw, color='blue', ls='-')"
]
},
{
"cell_type": "markdown",
"id": "26feab1b-41a6-464f-b578-95814286b915",
"metadata": {},
"source": [
"Perform a fast Fourier analysis, calculate the associated frequencies and plot the resulting amplitude spectra of both the original and the windowed time series. Compare the results."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "164cd4c2-72ea-44e8-a236-95475118aad1",
"metadata": {},
"outputs": [],
"source": [
"spec = dft_fast_coeff(seis)\n",
"f = 1./tlen*np.arange(0, spec.size)\n",
"print(\"Number of frequencies: \", spec.size, \" fmax = \", f[-1]*1000, \"mHz\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c88a619-8b9c-44ed-bc5f-f6ecce938af3",
"metadata": {},
"outputs": [],
"source": [
"specw = dft_fast_coeff(seisw)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "73b8fae2-e712-47d7-99a5-ebe5375084cf",
"metadata": {},
"outputs": [],
"source": [
"fig3, ax3 = SetupFigure(12, 5, \"Frequency [mHz]\", \"S(f)\", \"Amplitude spectrum of ground motion\", 14)\n",
"ax3.set_xlim(0,1.2)\n",
"ax3.plot(f*1000, np.absolute(spec), color='blue', ls='-', label='boxcar')\n",
"ax3.plot(f*1000, np.absolute(specw), color='red', ls='-', label='hann')\n",
"ax3.legend()"
]
},
{
"cell_type": "markdown",
"id": "689b3af4-1146-4041-9f4e-baed098d2a23",
"metadata": {},
"source": [
"### Task 2: Read a seismogram from file and compute spectrum\n",
"Here, we first read a recorded seismogram form a file, do a Fourier analysis and later apply diverse acausal filters. We make use of the class \"SeismicTrace\" that defines a data structure for a seismic trace and also some useful functions related to traces. You may want to look at the provided code in \"seismicTrace.py\"."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "912ae1f7-8cbc-4446-9ef0-b446b57d789d",
"metadata": {},
"outputs": [],
"source": [
"seisfile = \"CH.PANIX.HHZ\"\n",
"with open(seisfile, 'r') as fd:\n",
" seistrace = SeismicTrace.readFromAscii(fd)\n",
"t = seistrace.tanf+seistrace.dt*np.arange(0, seistrace.nsamp)\n",
"seistrace.printInfo()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eaee9262-f577-49a1-acee-9b9c27f996be",
"metadata": {},
"outputs": [],
"source": [
"fig4, ax4 = SetupFigure(12, 5, \"Time [s]\", \"s(t)\", \"Unfiltered seismic data\", 14)\n",
"ax4.plot(t, seistrace.vals, color='blue', ls='-')"
]
},
{
"cell_type": "markdown",
"id": "934a4fa1-7f8d-4977-bf86-bfe094abc5e5",
"metadata": {},
"source": [
"Perform a fast Fourier analysis, calculate the associated frequencies and plot the resulting amplitude spectrum."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c4a0c8c9-b51a-4116-9988-73d75b2f6704",
"metadata": {},
"outputs": [],
"source": [
"spec = dft_fast_coeff(seistrace.vals)\n",
"f = 1./seistrace.tlen*np.arange(0, spec.size)\n",
"print(\"Number of frequencies: \", spec.size, \" fmax = \", f[-1], \"Hz\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "82fb96d5-39ac-4c6a-a360-3544167ec26b",
"metadata": {},
"outputs": [],
"source": [
"fig5, ax5 = SetupFigure(12, 5, \"Frequency [Hz]\", \"S(f)\", \"Amplitude spectrum of seismogram\", 14)\n",
"ax5.set_xlim(0, 1)\n",
"ax5.plot(f, np.absolute(spec), color='blue', ls='-', label='boxcar')"
]
},
{
"cell_type": "markdown",
"id": "8d36d0c3-6faf-4e0b-b934-8822100b2b2f",
"metadata": {},
"source": [
"### Task 3: Acausal low pass filtering"
]
},
{
"cell_type": "markdown",
"id": "36b37ff1-3080-4651-a344-9cb4ede9109d",
"metadata": {},
"source": [
"Apply the boxcar lowpass filter to the data and plot the result."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a1e59a76-d251-4d66-b6ba-7c29a0626b3e",
"metadata": {},
"outputs": [],
"source": [
"fc = 0.1\n",
"df = 1./seistrace.tlen\n",
"specfil = spec*boxcar_lowpass_filter(fc, df, spec.size)\n",
"seisfil = dft_fast_synthesis(specfil)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "532ad374-2728-435d-8dca-afd7fcf9c475",
"metadata": {},
"outputs": [],
"source": [
"fig6, ax6 = SetupFigure(12, 5, \"Time [s]\", \"sf(t)\", \"Boxcar lowpass filtered seismic data, FC = {:5.2f} Hz\".format(fc), 14)\n",
"#ax6.set_xlim(64000, 64500)\n",
"ax6.plot(t, seisfil, color='blue', ls='-')"
]
},
{
"cell_type": "markdown",
"id": "e1097e6d-c459-489d-aecd-75911419a319",
"metadata": {},
"source": [
"Apply the Hann low pass filter to the data and plot the result."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "be2e1d6d-0c4e-4bfa-8612-ce626393bdff",
"metadata": {},
"outputs": [],
"source": [
"specfil2 = spec*hann_lowpass_filter(fc, df, spec.size)\n",
"seisfil2 = dft_fast_synthesis(specfil2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b5d4bf72-9bc2-4679-9178-4f7bc650e4ee",
"metadata": {},
"outputs": [],
"source": [
"fig7, ax7 = SetupFigure(12, 5, \"Time [s]\", \"sf(t)\", \"Hann lowpass filtered seismic data, FC = {:5.2f}\".format(fc), 14)\n",
"#ax7.set_xlim(64000, 64500)\n",
"ax7.plot(t, seisfil2, color='blue', ls='-')"
]
},
{
"cell_type": "markdown",
"id": "3bbec473-2bf7-41d0-828d-9413dff147e8",
"metadata": {},
"source": [
"Apply the boxcar highpass filter to the data and plot the result."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c9ec91f3-cc1c-48e5-ac53-882002fc74f6",
"metadata": {},
"outputs": [],
"source": [
"specfil3 = spec*boxcar_highpass_filter(fc, df, spec.size)\n",
"seisfil3 = dft_fast_synthesis(specfil3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7a24950e-8438-4e3b-903e-75f2f4408105",
"metadata": {},
"outputs": [],
"source": [
"fig8, ax8 = SetupFigure(12, 5, \"Time [s]\", \"sf(t)\", \"Boxcar highpass filtered seismic data, FC = {:5.2f}\".format(fc), 14)\n",
"#ax7.set_xlim(64000, 64500)\n",
"ax8.plot(t, seisfil3, color='blue', ls='-')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "92e0ebc5-9f7d-4302-8ad1-9aed6c7f2d6a",
"metadata": {},
"outputs": [],
"source": []
}
],
"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
}