221 lines
7.3 KiB
Plaintext
221 lines
7.3 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"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": null,
|
|
"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": null,
|
|
"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": null,
|
|
"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": null,
|
|
"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": null,
|
|
"id": "f5da468c-fded-40b4-a691-6adc93fbf110",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"\"\"\"Here comes your code\"\"\""
|
|
]
|
|
},
|
|
{
|
|
"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": null,
|
|
"id": "a8abee97-2424-445d-81c3-98353d5ac270",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"\"\"\"Here comes your code\"\"\""
|
|
]
|
|
},
|
|
{
|
|
"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, hwidth=20. \n",
|
|
"\n",
|
|
"Second, append some zeros (e.g. 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": null,
|
|
"id": "7c5663c1-5605-45df-8342-2dcefb2ec4b0",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"\"\"\"Here comes your code\"\"\""
|
|
]
|
|
},
|
|
{
|
|
"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 the same 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": null,
|
|
"id": "9e8596d7-1eb5-4248-85c5-e90ed8beaeb8",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"\"\"\"Here comes your 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
|
|
}
|