Skip to content
Merged

fft2 #344

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
99 changes: 99 additions & 0 deletions benchmark/reimFFT.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/* eslint-disable no-console */
import { reimFFT } from '../src/reim/reimFFT.ts';
import { reimArrayFFT } from '../src/reimArray/reimArrayFFT.ts';

const size = 2 ** 16;
const count = 10; // number of spectra in the array benchmark

// Build input data
const spectra = Array.from({ length: count }, () => {
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();
}
return { re, im };
});

// Warmup
for (const s of spectra) reimFFT(s);
for (const s of spectra) reimFFT(s, { inPlace: true });
reimArrayFFT(spectra);
reimArrayFFT(spectra, { inPlace: true });

const targetMs = 5000;

// --- reimFFT (loop over each spectrum individually) ---
{
let iterations = 0;
const start = performance.now();
console.time('reimFFT (loop)');
while (performance.now() - start < targetMs) {
for (const s of spectra) reimFFT(s);
iterations++;
}
const elapsed = performance.now() - start;
console.timeEnd('reimFFT (loop)');
console.log(
` ${iterations * count} total FFTs, ${count} spectra × ${iterations} rounds`,
);
console.log(` ${(elapsed / (iterations * count)).toFixed(3)} ms per FFT`);
}

console.log('');

// --- reimFFT inPlace (loop over each spectrum individually) ---
{
let iterations = 0;
const start = performance.now();
console.time('reimFFT inPlace (loop)');
while (performance.now() - start < targetMs) {
for (const s of spectra) reimFFT(s, { inPlace: true });
iterations++;
}
const elapsed = performance.now() - start;
console.timeEnd('reimFFT inPlace (loop)');
console.log(
` ${iterations * count} total FFTs, ${count} spectra × ${iterations} rounds`,
);
console.log(` ${(elapsed / (iterations * count)).toFixed(3)} ms per FFT`);
}

console.log('');

// --- reimArrayFFT (single call for the whole array) ---
{
let iterations = 0;
const start = performance.now();
console.time('reimArrayFFT');
while (performance.now() - start < targetMs) {
reimArrayFFT(spectra);
iterations++;
}
const elapsed = performance.now() - start;
console.timeEnd('reimArrayFFT');
console.log(
` ${iterations * count} total FFTs, ${count} spectra × ${iterations} rounds`,
);
console.log(` ${(elapsed / (iterations * count)).toFixed(3)} ms per FFT`);
}

console.log('');

// --- reimArrayFFT inPlace (single call for the whole array) ---
{
let iterations = 0;
const start = performance.now();
console.time('reimArrayFFT inPlace');
while (performance.now() - start < targetMs) {
reimArrayFFT(spectra, { inPlace: true });
iterations++;
}
const elapsed = performance.now() - start;
console.timeEnd('reimArrayFFT inPlace');
console.log(
` ${iterations * count} total FFTs, ${count} spectra × ${iterations} rounds`,
);
console.log(` ${(elapsed / (iterations * count)).toFixed(3)} ms per FFT`);
}
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@
"ml-xsadd": "^3.0.1"
},
"devDependencies": {
"@types/node": "^25.0.3",
"@vitest/coverage-v8": "^4.0.16",
"@types/node": "^25.3.0",
"@vitest/coverage-v8": "^4.0.18",
"@zakodium/tsconfig": "^1.0.2",
"cheminfo-build": "^1.3.2",
"eslint": "^9.39.2",
"eslint-config-cheminfo-typescript": "^21.0.1",
"eslint-config-cheminfo-typescript": "^21.1.0",
"jest-matcher-deep-close-to": "^3.0.2",
"ml-spectra-fitting": "^5.0.1",
"prettier": "^3.7.4",
"rimraf": "^6.1.2",
"prettier": "^3.8.1",
"rimraf": "^6.1.3",
"spectrum-generator": "^8.1.1",
"typescript": "^5.9.3",
"vitest": "^4.0.16"
"vitest": "^4.0.18"
},
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ exports[`existence of exported functions 1`] = `
"reimFFT",
"reimPhaseCorrection",
"reimZeroFilling",
"reimArrayFFT",
"getOutputArray",
"xAbsolute",
"xAbsoluteMedian",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './reim/index.ts';
export * from './reimArray/index.ts';

export * from './x/index.ts';

Expand Down
26 changes: 26 additions & 0 deletions src/reim/__tests__/reimFFT.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,29 @@ test('reimFFT', () => {

expect(inverse.re).toStrictEqual(re);
});

test('reimFFT inPlace: returns same object and mutates input arrays', () => {
const re = Float64Array.from([0, 3, 6, 5]);
const im = Float64Array.from([0, 4, 8, 3]);
const data = { re, im };

const result = reimFFT(data, { inPlace: true });

expect(result).toBe(data);
expect(result.re).toBe(re);
expect(result.im).toBe(im);
});

test('reimFFT inPlace: round-trip restores original values', () => {
const re = Float64Array.from([0, 3, 6, 5]);
const im = Float64Array.from([0, 4, 8, 3]);
const reOrig = Float64Array.from(re);
const imOrig = Float64Array.from(im);
const data = { re, im };

reimFFT(data, { inPlace: true, applyZeroShift: true });
reimFFT(data, { inPlace: true, inverse: true, applyZeroShift: true });

expect(data.re).toStrictEqual(reOrig);
expect(data.im).toStrictEqual(imOrig);
});
28 changes: 16 additions & 12 deletions src/reim/reimFFT.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import FFT from 'fft.js';

import type { DataReIm } from '../types/index.ts';
import { xRotate } from '../x/index.ts';

import { zeroShift } from './zeroShift.ts';

export interface ReimFFTOptions {
inverse?: boolean;
applyZeroShift?: boolean;
/**
* Write the result back into the input arrays instead of allocating new ones.
* @default false
*/
inPlace?: boolean;
}

/**
Expand All @@ -18,7 +24,7 @@ export function reimFFT(
data: DataReIm,
options: ReimFFTOptions = {},
): DataReIm<Float64Array> {
const { inverse = false, applyZeroShift = false } = options;
const { inverse = false, applyZeroShift = false, inPlace = false } = options;

const { re, im } = data;
const size = re.length;
Expand All @@ -40,6 +46,14 @@ export function reimFFT(
if (applyZeroShift) output = zeroShift(output);
}

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>;
}

const newRe = new Float64Array(size);
const newIm = new Float64Array(size);
for (let i = 0; i < csize; i += 2) {
Expand All @@ -49,13 +63,3 @@ export function reimFFT(

return { re: newRe, im: newIm };
}

function zeroShift(
data: Float64Array,
inverse?: boolean,
): Float64Array<ArrayBuffer> {
const middle = inverse
? Math.ceil(data.length / 2)
: Math.floor(data.length / 2);
return xRotate(data, middle);
}
12 changes: 12 additions & 0 deletions src/reim/zeroShift.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { xRotate } from '../x/index.ts';

export function zeroShift(
data: Float64Array,
inverse?: boolean,
): Float64Array<ArrayBuffer> {
const middle = inverse
? Math.ceil(data.length / 2)
: Math.floor(data.length / 2);

return xRotate(data, middle);
}
Loading
Loading