-
Notifications
You must be signed in to change notification settings - Fork 13
fft #343
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
Closed
fft #343
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
|
|
@@ -44,3 +44,5 @@ dist | |
|
|
||
| .DS_Store | ||
| .eslintcache | ||
|
|
||
| CPU.* | ||
This file contains hidden or 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,28 @@ | ||
| import { reimFFT } from '../src/reim/reimFFT.ts'; | ||
|
|
||
| const size = 2 ** 16; | ||
| const re = new Float64Array(size); | ||
| const im = new Float64Array(size); | ||
| for (let i = 0; i < size; i++) { | ||
| re[i] = Math.random(); | ||
| im[i] = Math.random(); | ||
| } | ||
| // Warmup | ||
| reimFFT({ re, im }); | ||
|
|
||
| // Benchmark: repeat until ~5s | ||
| const targetMs = 5000; | ||
| let iterations = 0; | ||
|
|
||
| console.time('reimFFT'); | ||
| const start = performance.now(); | ||
| while (performance.now() - start < targetMs) { | ||
| reimFFT({ re, im }); | ||
| iterations++; | ||
| } | ||
| console.timeEnd('reimFFT'); | ||
|
|
||
| console.log(`${iterations} iterations, ${size} points each`); | ||
| console.log( | ||
| `${((performance.now() - start) / iterations).toFixed(2)} ms per iteration`, | ||
| ); |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| import { defineConfig, globalIgnores } from 'eslint/config'; | ||
| import cheminfo from 'eslint-config-cheminfo-typescript'; | ||
|
|
||
| export default defineConfig(globalIgnores(['coverage', 'lib']), cheminfo); | ||
| export default defineConfig( | ||
| globalIgnores(['coverage', 'lib', 'benchmark']), | ||
| cheminfo, | ||
| ); |
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -3,9 +3,13 @@ import FFT from 'fft.js'; | |
| import type { DataReIm } from '../types/index.ts'; | ||
| import { xRotate } from '../x/index.ts'; | ||
|
|
||
| let output: Float64Array | undefined; | ||
| let complexArray: Float64Array | undefined; | ||
| let fft: FFT | undefined; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also cache the FFT instance and I think it is ok. With this I can win a factor 2 in my benchmark reimFFT (3000 to 6800 iterations in 5s) |
||
| export interface ReimFFTOptions { | ||
| inverse?: boolean; | ||
| applyZeroShift?: boolean; | ||
| inPlace?: boolean; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -18,20 +22,21 @@ export function reimFFT( | |
| data: DataReIm, | ||
| options: ReimFFTOptions = {}, | ||
| ): DataReIm<Float64Array> { | ||
| const { inverse = false, applyZeroShift = false } = options; | ||
|
|
||
| const { re, im } = data; | ||
| const size = re.length; | ||
| const csize = size << 1; | ||
|
|
||
| let complexArray = new Float64Array(csize); | ||
| const { inPlace = false, inverse = false, applyZeroShift = false } = options; | ||
|
|
||
| if (fft?.size !== size) fft = new FFT(size); | ||
|
|
||
| if (complexArray?.length !== csize) complexArray = new Float64Array(csize); | ||
| for (let i = 0; i < csize; i += 2) { | ||
| complexArray[i] = re[i >>> 1]; | ||
| complexArray[i + 1] = im[i >>> 1]; | ||
| } | ||
|
|
||
| const fft = new FFT(size); | ||
| let output = new Float64Array(csize); | ||
| if (output?.length !== csize) output = new Float64Array(csize); | ||
| if (inverse) { | ||
| if (applyZeroShift) complexArray = zeroShift(complexArray, true); | ||
| fft.inverseTransform(output, complexArray); | ||
|
|
@@ -40,14 +45,21 @@ export function reimFFT( | |
| if (applyZeroShift) output = zeroShift(output); | ||
| } | ||
|
|
||
| const newRe = new Float64Array(size); | ||
| const newIm = new Float64Array(size); | ||
| for (let i = 0; i < csize; i += 2) { | ||
| newRe[i >>> 1] = output[i]; | ||
| newIm[i >>> 1] = output[i + 1]; | ||
| if (inPlace) { | ||
| for (let i = 0; i < csize; i += 2) { | ||
| re[i >>> 1] = output[i]; | ||
| im[i >>> 1] = output[i + 1]; | ||
| } | ||
| return data as DataReIm<Float64Array>; | ||
| } else { | ||
| const newRe = new Float64Array(size); | ||
| const newIm = new Float64Array(size); | ||
| for (let i = 0; i < csize; i += 2) { | ||
| newRe[i >>> 1] = output[i]; | ||
| newIm[i >>> 1] = output[i + 1]; | ||
| } | ||
| return { re: newRe, im: newIm }; | ||
| } | ||
|
|
||
| return { re: newRe, im: newIm }; | ||
| } | ||
|
|
||
| function zeroShift( | ||
|
|
||
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.
@targos those arrays are recreated each time we call this method which consume some memory especially on 2D for which we call this method maybe 256 times with the same size.
Is it acceptable to define them globally the code being sync ?
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.
Not really a fan. This will keep the last used objects indefinitely in memory.
I would rather export a function that creates an object with these arrays, and accept that object as an option.