-
Notifications
You must be signed in to change notification settings - Fork 323
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
NimaSarajpoor
wants to merge
5
commits into
TDAmeritrade:main
Choose a base branch
from
NimaSarajpoor:dev_fft
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
aa82e2e
add notebook for njit fft and sliding dot product
NimaSarajpoor 3e16ce8
Add notebook for sliding_dot_product
NimaSarajpoor c2a9b43
add OTFFT notebook
NimaSarajpoor 5d6fc83
add docstrings and put each function in one cell
NimaSarajpoor 2c5dd4d
Add function _fft0
NimaSarajpoor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,289 @@ | ||
{ | ||
"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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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