Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed#938: Speed up njit-decorated function for sliding dot product [WIP] #939

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
289 changes: 289 additions & 0 deletions docs/OTFFT.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
{
Copy link
Collaborator Author

@NimaSarajpoor NimaSarajpoor Jan 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line #20.        x[:] = scipy.fft.fft(x)  # we will implement our fft shortly!

We will later work on implementing this fft function using 6-step / 8-step algorithm. We then change this line and the caller function accordingly.


Reply via ReviewNB

"cells": [
{
"cell_type": "markdown",
"id": "5b88fa48",
"metadata": {},
"source": [
"This notebook implements the 6-step / 8-step FFT (fft) algorithm as provided in [OTFFT](http://wwwa.pikara.ne.jp/okojisan/otfft-en/stockham2.html). Accordingly, the inverse FFT (ifft) algorithm will be implemented as well. When the input of FFT, `T` with length `n`, consists of real-valued elements only, we can take advantage of real FFT (rfft) as explained in [2.6.2](https://www.researchgate.net/profile/Christos-Bechlioulis/publication/341270520_FFT_algorithms_are_not_mine_However_I_am_going_to_convince_you_soon_regarding_the_visit_of_RMS_to_our_university_Believe_it_or_not_this_is_me_This_is_us_Univeristy_of_Patras_you_have_chosen_a_quite_wr/links/5fa53ce7299bf10f7328c33b/FFT-algorithms-are-not-mine-However-I-am-going-to-convince-you-soon-regarding-the-visit-of-RMS-to-our-university-Believe-it-or-not-this-is-me-This-is-us-Univeristy-of-Patras-you-have-chosen-a-quite.pdf). "
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c9a886c2",
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"import time\n",
"\n",
"import numba\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import scipy\n",
"\n",
"from numba import njit, prange\n",
"import numpy.testing as npt\n",
"\n",
"from stumpy import core"
]
},
{
"cell_type": "markdown",
"id": "80765bca",
"metadata": {},
"source": [
"Let's start with `rfft`. First, we write the test!"
]
},
{
"cell_type": "markdown",
"id": "8fc07997",
"metadata": {},
"source": [
"## rfft"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7bb4242c",
"metadata": {},
"outputs": [],
"source": [
"def test_rfft(n_powers_list):\n",
" seed = 0\n",
" np.random.seed(seed)\n",
" for p in n_powers_list:\n",
" n = 2 ** p\n",
" T = np.random.rand(n)\n",
" \n",
" ref = scipy.fft.rfft(T)\n",
" comp = rfft(T)\n",
" \n",
" npt.assert_almost_equal(ref, comp)"
]
},
{
"cell_type": "markdown",
"id": "5a1c553e",
"metadata": {},
"source": [
"We now implement `rfft` function according to the steps provided in [2.6.2](https://www.researchgate.net/profile/Christos-Bechlioulis/publication/341270520_FFT_algorithms_are_not_mine_However_I_am_going_to_convince_you_soon_regarding_the_visit_of_RMS_to_our_university_Believe_it_or_not_this_is_me_This_is_us_Univeristy_of_Patras_you_have_chosen_a_quite_wr/links/5fa53ce7299bf10f7328c33b/FFT-algorithms-are-not-mine-However-I-am-going-to-convince-you-soon-regarding-the-visit-of-RMS-to-our-university-Believe-it-or-not-this-is-me-This-is-us-Univeristy-of-Patras-you-have-chosen-a-quite.pdf)."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d0033890",
"metadata": {},
"outputs": [],
"source": [
"def _rfft(T):\n",
" \"\"\"\n",
" For the input `T` with length `n=len(T)`, this function returns its\n",
" real fast fourier transform (rfft) with length of `(n // 2) + 1`.\n",
" \n",
" Parameters\n",
" ----------\n",
" T : numpy.ndarray\n",
" A time series of interest, with real-valued numbers\n",
" \n",
" Returns\n",
" -------\n",
" out : numpy.ndarray\n",
" the real fast fourier transform (rfft) of input `T`\n",
" \"\"\"\n",
" n = len(T)\n",
" half_n = int(n // 2)\n",
" \n",
" x = T[::2] + 1j * T[1::2]\n",
" x[:] = scipy.fft.fft(x) # we will implement our fft shortly!\n",
" \n",
" out = np.empty(half_n + 1, dtype=np.complex_)\n",
" out[0] = x[0].real + x[0].imag\n",
" out[half_n] = x[0].real - x[0].imag\n",
" out[n // 4] = x[n // 4].conjugate()\n",
" \n",
" theta0 = 2 * math.pi / n\n",
" for k in range(1, n // 4):\n",
" theta = theta0 * k\n",
" a = x[half_n - k].conjugate()\n",
" b = 0.5 * (x[k] - a) * (1.0 + complex(math.sin(theta), math.cos(theta)))\n",
" out[k] = x[k] - b\n",
" out[half_n - k] = (a + b).conjugate()\n",
" \n",
" return out"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d5db0eb9",
"metadata": {},
"outputs": [],
"source": [
"def rfft(T):\n",
" \"\"\"\n",
" For the input `T` with length `n=len(T)`, this function returns its\n",
" real fast fourier transform (rfft) with length of `(n // 2) + 1`.\n",
" \n",
" Parameters\n",
" ----------\n",
" T : numpy.ndarray\n",
" A time series of interest, with real-valued numbers\n",
" \n",
" Returns\n",
" -------\n",
" out : numpy.ndarray\n",
" the real fast fourier transform (rfft) of input `T`\n",
" \"\"\"\n",
" return _rfft(T)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "ded21f89",
"metadata": {},
"outputs": [],
"source": [
"n_powers_list = np.arange(2, 11)\n",
"test_rfft(n_powers_list)"
]
},
{
"cell_type": "markdown",
"id": "6398a98a",
"metadata": {},
"source": [
"We now work on implementing 6-step / 8-step FFT algorithm. We then revisit the two functions above. We will replace `scipy.fft.fft(x)` with our FFT function, and then change them accordingly."
]
},
{
"cell_type": "code",
"execution_count": 133,
"id": "09e2fb6d",
"metadata": {},
"outputs": [],
"source": [
"@njit(fastmath=True)\n",
"def _fft0(n, s, eo, x, y):\n",
" \"\"\"\n",
" A recurive function that is being called by six-step / eight-step FFT algorithm, \n",
" and update `x` in place.\n",
" \n",
" Parameters\n",
" ----------\n",
" n : int\n",
" Length of sequence\n",
" \n",
" s : int\n",
" size of striding window\n",
" \n",
" eo : bool\n",
" If False, `x` is output. If True, `y` is the output.\n",
" \n",
" x : numpy.ndarray\n",
" A 1D numpy.ndarray with `np.complex_` dtype\n",
" \n",
" y : nummpy.ndarray\n",
" A 1D numpy.ndarray with same size as `x` and `np.complex_` dtype. \n",
" \n",
" Returns\n",
" -------\n",
" \n",
" Notes\n",
" -----\n",
" <http://wwwa.pikara.ne.jp/okojisan/otfft-en/sixstepfft.html>`__\n",
"\n",
" See function `fft0` provided in \"List-11: Six-Step FFT\"\n",
" \"\"\"\n",
" if n == 2:\n",
" if eo:\n",
" z = y\n",
" else:\n",
" z = x\n",
" \n",
" for i in range(s):\n",
" j = i + s\n",
" a = x[i]\n",
" b = x[j]\n",
" z[i] = a + b\n",
" z[j] = a - b\n",
" \n",
" elif n >= 4:\n",
" m = n // 2\n",
" sm = s * m\n",
" \n",
" theta = 2 * math.pi / n\n",
" c = complex(math.cos(theta), -math.sin(theta))\n",
" \n",
" twiddle_factor = 1.0\n",
" for p in range(m):\n",
" sp = s * p\n",
" two_sp = 2 * sp\n",
" for q in range(s):\n",
" i = sp + q\n",
" j = i + sm\n",
" \n",
" k = two_sp + q\n",
" y[k] = x[i] + x[j]\n",
" y[k + s] = (x[i] - x[j]) * twiddle_factor\n",
" \n",
" twiddle_factor = twiddle_factor * c\n",
" \n",
" _fft0(m, 2 * s, not eo, y, x)\n",
" \n",
" else:\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9813fb90",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "a57bf1ac",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "88312d26",
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading