Denoise a signal with the DFT
Turn a noisy sine wave into frequencies with the NPU, keep only the low ones, and turn the result back into a clean signal with the same operation.
Tested on the toolkit 2.5.0 CPU emulator on 18 November 2026 with commit e0868cf of the examples repository. The test passed in 3.4 seconds.
This is the first official example for the DFT, the native operation that toolkit 2.5.0 added for NPU Gen2+. It hides a 5 Hz sine wave under heavy noise, then recovers it in three steps: transform to frequencies, delete the frequencies that are only noise, and transform back.
Objectives
- Run a DFT on the NPU with
native.dftand thebcomplex32data type. - Filter a signal in the frequency domain.
- Compute the inverse DFT with the forward operation.
Before you begin
-
Install the toolkit, version 2.5.0 or newer. The CPU emulator simulates Gen2+ devices, so you don’t need a card.
-
Get the examples repository:
git clone --recurse-submodules https://github.com/Q-ANT-GmbH/qant_native_computing_toolkit_examples.git cd qant_native_computing_toolkit_examples/python/signal_denoising -
Install the example’s dependencies:
pip install -r requirements.txtThis installs
numpy,matplotlib,ml_dtypes0.6.0 or newer, Jupyter and the test tools.
Run the example
Open signal_denoising_on_npu.ipynb in Jupyter and run all cells.
How it works
1. Make a noisy signal
The notebook samples a 5 Hz sine wave for one second, 1,024 times, and adds Gaussian noise with a standard deviation of 1.5. That is one and a half times the height of the wave, so the wave is hard to see by eye.
n_samples = 1024
sampling_rate = 1024.0
t = np.arange(n_samples) / sampling_rate
clean_signal = np.sin(2 * np.pi * 5.0 * t)
noisy_signal = clean_signal + rng.normal(0.0, 1.5, size=n_samples)
2. Transform it on the NPU
spectrum = q_native.dft(noisy_signal.astype(bcomplex32))
spectrum = spectrum.astype(np.complex64)
The signal is real, so its imaginary parts are zero. In the spectrum the sine wave stands out as two tall peaks at plus and minus 5 Hz, while the noise spreads thinly over all frequencies.
3. Keep only the low frequencies
A low-pass filter keeps every bin up to 6 Hz and sets the rest to zero. That keeps 13 of the 1,024 bins.
freqs = np.fft.fftfreq(n_samples, d=1 / sampling_rate)
filtered_spectrum = spectrum * (np.abs(freqs) <= 6.0)
4. Transform back on the NPU
The NPU has no inverse DFT, so the notebook builds one from the forward transform with two complex conjugations:
def idft_on_npu(spectrum):
n = spectrum.shape[-1]
result = q_native.dft(np.conj(spectrum).astype(bcomplex32))
return np.conj(result.astype(np.complex64)) / n
denoised_signal = idft_on_npu(filtered_spectrum).real
Expected output
On the CPU emulator the notebook printed:
MSE noisy vs. clean: 2.1306
MSE denoised vs. clean: 0.0331
| Measure | Value |
|---|---|
| Error of the noisy signal | 2.1306 |
| Error after denoising on the emulator | 0.0331 |
| Error after the same filter in NumPy, float64 | 0.0329 |
| Largest difference between the emulator and NumPy | 0.016 |
Denoising cuts the error by a factor of about 64. The bfloat16 DFT ends up within 0.0002 of the float64 result, so for this task the reduced precision barely matters.
Run the tests
The test runs the notebook and checks that the denoised error is below 0.1 and below a tenth of the noisy error:
python -m pytest -v test_signal_denoising.py
On the emulator the test passed in 3.4 seconds.
What’s next
- Learn the details of the operation in The DFT on Gen2+.
- See how the emulator simulates Gen2+ devices in Emulator and hardware.