openreader/compute-worker/tests/compute/algorithms/whisper-spectral.test.ts
Richard R 6e69b48103 chore(worker): restructure inference modules and centralize config
- Remove legacy inference and platform files from `src/inference/` and migrate responsibilities to new modular locations under `src/infrastructure/`, `src/inference/pdf/`, and `src/inference/whisper/`
- Consolidate environment/config logic into `src/infrastructure/config.ts`
- Move docstore and ffmpeg platform utilities to `src/infrastructure/platform.ts`
- Refactor PDF and Whisper inference code to use new document layout, layout model, and timestamp utilities
- Update API contracts to define and export types and constants previously scattered in inference/types
- Adjust all imports in jobs, storage, and tests to reference new module structure and type locations
- Inline parser version and encoding logic into API contracts
- Remove obsolete files and update test fixtures for new type locations

This update improves codebase modularity, maintainability, and separation of concerns. No changes to inference or orchestration functionality.

BREAKING CHANGE: inference module structure, type imports, and config utilities have changed; downstream code must update imports and integration points.
2026-06-12 14:16:13 -06:00

34 lines
1.2 KiB
TypeScript

import { describe, expect, test } from 'vitest';
import { buildGoertzelCoefficients, goertzelPower } from '../../../src/inference/whisper/align';
function dftPower(samples: Float32Array, k: number): number {
const n = samples.length;
let re = 0;
let im = 0;
for (let i = 0; i < n; i += 1) {
const angle = (-2 * Math.PI * k * i) / n;
re += samples[i] * Math.cos(angle);
im += samples[i] * Math.sin(angle);
}
return (re * re) + (im * im);
}
describe('whisper spectral helpers', () => {
test('goertzel power matches direct DFT for non-power-of-two frame size', () => {
const frameSize = 400;
const bins = 201;
const coeffs = buildGoertzelCoefficients(bins, frameSize);
const samples = new Float32Array(frameSize);
for (let i = 0; i < frameSize; i += 1) {
samples[i] = Math.sin((2 * Math.PI * 37 * i) / frameSize) + (0.2 * Math.cos((2 * Math.PI * 91 * i) / frameSize));
}
const testBins = [0, 7, 37, 91, 150, 200];
for (const k of testBins) {
const expected = dftPower(samples, k);
const actual = goertzelPower(samples, coeffs[k]);
const rel = Math.abs(actual - expected) / Math.max(1, Math.abs(expected));
expect(rel).toBeLessThan(1e-5);
}
});
});