FFT Kicad 8

I have just started with Kicad 8 and tried FFT on an RC circuit. The result look strange and I do not understand what is wrong. Below see circuit and results:




Spice Netlist:
.title KiCad schematic
.save all
.probe alli
.probe p(V2)
.probe p(C1)
.probe p(R1)
linearize V(in) V(out)
fft V(in) V(out)
V2 in 0 DC 0 SIN( 0 1 1k 0 0 0 ) AC 1
C1 out 0 10u
R1 in out 1k
.end
your help will be much appreciated

The fft code in Eeschema is still a little buggy (see the output text at the bottom).

To improve on the fft curve, you should raise the number of cycles simulated. For a 1kHz sine the TSTOP should be no less than 100ms.

This is how it will look like after some additional code development (ngspice-44):

Still missing in Eeschema: selection of lin or log in both axes, netlist bug removal (see above).

The fundamental issue is you have not windowed the data and thus you have what is known as spectral leakage. This isn’t a Kicad/ngspice limitaton, this is fundamental to fft’s… How to mitigate it though is downto the tool

FFT only really works for complete cycles, and that is complete cycles of all frequencies of interest.

For a simple simulation like this you can set the simulation time to the EXACT multiple of the period.
Since it is a simple circuit (one that does not have harmonics) you could perform it over one cycle.

HOWEVER… The maximum frequency is half the sampling frequency and the minimum frequency is the simulation time.

Now for simulations with harmonics where ensuring you have completed cycles is “impossible” you perform what is windowing ( my favourite is blackman-harris but will manually perform a rectangle if I was EXACT results).
Whether ngspice supports a basic Hamming/Hanning/* window… I do not know and thus the alternative is to

  1. Export as CSV and process with python
  2. Run for a longer period and thus more cycles to mitigate aspects of the spectral leakage at the frequency of interest (at the expense of leakage at higher frequency )
1 Like

ngspice offers: none, rectangular, bartlet, hanning (default), blackman, blackmanharris, hamming, gaussian, and flattop.

It is interesting to see how the circuit influences the response:


Red is the sine input, green a resistor voltage divide by 2, violett a high-pass RC, orange the low-pass RC.
The fft here is provided by the fftw3 library (KiCad8.99).
fft-rc.7z (6.0 KB)
With existing KiCad 8.99 (no fftw3) the result is much noisier.

thank you all for your response,
I realize that I was on the wrong path (I did FFT many years ago in school).
What I really was looking for was the spectrum, that means the frequences that I get after filtering. Is there a way to get that simulated?

Sounds like you want to use the AC analysis.

Your input is a single frequency sinusoidal signal.

The RC filter is a linear filter.

The spectrum thus is just the single frequency entered into the filter, damped and phase shifted. There is no other frequency generated.

I think @KiCADsmktec is referring to finding the filter transfer function, a la AC analysis at the OUT node:

1 Like

I showed some windowing examples (not ng-spice, just fft examples) in this post:

https://forum.kicad.info/t/how-to-improve-fft-waveform-in-kicad-8/48710/4