Complex Number Utilities
Helper functions for complex number arithmetic.
Complex Type
ts
type Complex = [number, number]; // [real, imaginary]1
Arithmetic Operations
complexAdd()
ts
function complexAdd(a: Complex, b: Complex): Complex;1
complexSub()
ts
function complexSub(a: Complex, b: Complex): Complex;1
complexMul()
ts
function complexMul(a: Complex, b: Complex): Complex;1
complexScale()
ts
function complexScale(a: Complex, scale: number): Complex;1
complexConj()
ts
function complexConj(a: Complex): Complex;1
Returns the complex conjugate.
complexMagnitude()
ts
function complexMagnitude(a: Complex): number;1
Returns the magnitude (absolute value).
Twiddle Factors
twiddleFactor()
ts
function twiddleFactor(k: number, n: number): Complex;1
Computes e^(-2πi·k/n).
twiddleFactorInverse()
ts
function twiddleFactorInverse(k: number, n: number): Complex;1
Computes e^(2πi·k/n).
Data Conversion
interleavedToComplex()
ts
function interleavedToComplex(data: Float32Array): Complex[];1
Converts [Re, Im, Re, Im, ...] to [[Re, Im], ...].
complexToInterleaved()
ts
function complexToInterleaved(data: Complex[]): Float32Array;1
Converts [[Re, Im], ...] to [Re, Im, Re, Im, ...].
Utilities
complexApproxEqual()
ts
function complexApproxEqual(
a: Complex,
b: Complex,
tolerance?: number
): boolean;1
2
3
4
5
2
3
4
5
Compares two complex numbers with tolerance.
naiveDFT()
ts
function naiveDFT(data: Float32Array): Float32Array;1
Computes DFT using the naive O(N²) algorithm. Useful for verification.
naiveIDFT()
ts
function naiveIDFT(data: Float32Array): Float32Array;1
Computes inverse DFT using the naive algorithm.
Example
ts
import {
complexMul,
complexMagnitude,
twiddleFactor,
} from 'webgpu-fft';
const a: Complex = [3, 4];
const b: Complex = [1, 2];
const product = complexMul(a, b); // [-5, 10]
const mag = complexMagnitude(a); // 5
const twiddle = twiddleFactor(1, 8); // [0.707, -0.707]1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13