Task 1 of 5

One Bin, One Thread

A transform sounds grand. One bin of it is not: pick a frequency, build a test wave at that frequency, multiply it against the signal sample by sample, and add the products up. One number falls out, and it answers exactly one question — how much of this frequency is in here?

That is a dot product, and it is the same instinct Template Matching used on images: slide a pattern over the data and let the sum of the products score the match. Here the pattern is a cosine and the data is a row of samples.

Start with one bin and one thread. output: [1] gives you a single thread that walks the whole signal — the conceptual atom. Task 3 hands every bin a thread of its own, which is where this stops being a loop and starts being a GPU workload.

a transform is a pile of dot products — this is one of them
Goal: correlate signal against a cosine at bin k — sum signal[i] × cos(2π·k·i/n) over all 256 samples — and report bins 8, 20 and 13.

Requirements

Hint 1 — what the angle has to do

Over the whole signal, the test wave for bin k must complete exactly k turns. Term i is therefore a fraction i / n of the way through k turns, and one turn is radians:

const angle = 2 * Math.PI * k * i / this.constants.n;
Hint 2 — the accumulator

Same shape as any other loop-and-total kernel: declare let acc = 0; before the loop, add one product per iteration, and return acc; after it.

acc += signal[i] * Math.cos(angle);
Hint 3 — asking for a bin

k is an ordinary kernel argument, so one kernel answers every bin — call it again with a different number:

console.log('bin 8:', bin(signal, 8)[0]);
console.log('bin 20:', bin(signal, 20)[0]);

Same idea elsewhere

Correlating against a basis function is the move behind far more than audio: it is what a matched filter does in radar, what a lock-in amplifier does in a lab, and what a single row of a matrix–vector product does in linear algebra. On any platform the kernel is the same — CUDA, WGSL and Metal all give you a thread, a loop and a fused multiply-add, and that is the entire ingredient list.

All tasks in The DFT, Honestly

  1. One Bin, One Thread
  2. One Number Is Not Enough
  3. Every Bin, One Thread Each
  4. Magnitude, Phase and the Mirror
  5. There and Back Again

This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.