6

I have a 10 seconds of sampled data. Sampling rate is 1kHz. I uploaded the text file which is a two column time vs voltage data; it can be downloaded here.

I performed FFT in MATLAB, Python and LTspice. MATLAB and Python agrees when I plot but I get different result in LTspice.

Here below is the code I use and the plot with MATLAB:

ts = 0.001; %sampling interval (sec)
Fs = 1./ts;    %sampling frequency (Hz)
L = length(y); %number of samples
complex = fft(y)/L; % complex signals
f = 0 : Fs/L : Fs/2; % frequency bins
amplitude = 2*abs(complex(1:L/2+1)); % amplitudes
pow = (amplitude).^2/2*(L/Fs); % power
semilogx(f,20*log10(amplitude),'-b');
grid on;
xlabel('Frequency [Hz]')
ylabel('dB')

enter image description here


Below is the code I use and the plot with Python:

plt.figure()
y = v_in
T = 1/sampling_rate
N = len(y)
yf = scipy.fftpack.fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
amplitude = 2.0/N * np.abs(yf[:N//2])
pow = (N/sampling_rate)*amplitude*amplitude/2
plt.semilogx(xf, 20*np.log10(amplitude),'b')
plt.grid()
plt.xlabel('Frequency [Hz]')
plt.ylabel('dB')

enter image description here


And finally I fed the signal from the text file to the signal generator in LTspice(by using PWL file option) and performed FFT and I get the following plot:

enter image description here

MATLAB and Python both show the max db point as -46.2dB but Ltspice shows this point as -49.3dB. This is not a very small difference.

What could be the reason for this difference? Am I doing something wrong in MATLAB and Python when evaluating FFT or LTspice is wrong?

cm64
  • 2,059
  • 1
  • 16
  • 40
  • 1
    The difference is suspiciously close to 3dB. Is there some different assumption about whether the power is single or two-sided? I *always* mistrust the scaling on any FFT I do, and run a calibration with synthetic data just to check what assumptions the FFT is making. – Neil_UK Apr 16 '18 at 16:58
  • @Neil_UK I added a comment below that addresses this issue. – a concerned citizen Apr 16 '18 at 16:59
  • What window function did you use when doing the LTSpice FFT? – The Photon Apr 16 '18 at 17:18
  • Window function set to "none" – cm64 Apr 16 '18 at 17:19
  • Since I am after dB(power ratio) should I convert amplitude to rms always in a 20*log10(amplitude) formula? If you couple 1V amplitude sine wave to a scope, would the max FFT point of the scope show 1V or Vrms=0.7V? – cm64 Apr 16 '18 at 17:41

2 Answers2

7

Despite the fixed timings provided by the PWL file, LTspice is a SPICE engine, analog simulator, which means the simulation will not have even steps. In Matlab and Python, you can correctly account for the exact samples that are described in the definition. LTspice needs to simulate it, so, like all analog SPICE engines, it has an uneven time.

You could increase the timestep and/or add .opt plotwinsize=0, numdgt=15, which will improve the result. See if that helps.

[edit] However, no matter what you do, LTspice will show RMS values in the FFT window (unlike with the .four command).[/edit]


Here's my attempt by using the above (timestep 25u):

fft

Seems quite close to the Python version. In LTspice FFT I used 24000 points and set "Number of points" to 1 (polynomial smoothing).

a concerned citizen
  • 21,167
  • 1
  • 20
  • 40
  • These didn't make any change on mine. I added .opt plotwinsize=0, numdgt=15 and set "Number of points" to 1 at binomial smoothing. – cm64 Apr 16 '18 at 17:07
  • If all you need to account for is the 3dB difference, then my first comment should do the trick: just multiply with `sqrt(2)`. The rest are for a better resolution, that's all. – a concerned citizen Apr 16 '18 at 17:24
  • Yes when I do amplitude=amplitude/sqrt(2) in MATLAB and Python it matches to LTspice. But I dont get why. Something related to RMS. But where is the mistake I could not figure out. – cm64 Apr 16 '18 at 17:26
  • @cm64 Well, now you know why. I don't know why Mike decided to make it like this, but that's how it is. It doesn't seem to be a big deal. After all, the `.four` command calculates the peak amplitudes. Go figure... – a concerned citizen Apr 16 '18 at 17:54
  • Please write these to your answer so I can select. Pls if you know also add what would a real scope show Vrms or Vamp. Thanks – cm64 Apr 16 '18 at 18:00
  • @cm64 It would depend on the scope, I guess. Still, I should add that you don't need to divide by `sqrt(2)` in Matlab or Python, it's in LTspice that you have to multiply by `sqrt(2)`. – a concerned citizen Apr 16 '18 at 18:09
  • Thank you for the vote, but if you found the answer useful, select it so that others may find it easy. It's not mandatory, though. – a concerned citizen Apr 16 '18 at 18:12
0

In my view, LTSpice's FFT takes RMS value to calculate the spectrum of a signal. This is the reason why you have a 3dB discrepancy

cardompal
  • 1
  • 1
  • If you'll read the answer and the comments, this has been addressed. Therefore what you wrote would be more appropriate as a comment, rather than an answer. – a concerned citizen Feb 17 '22 at 16:29