Installing and using LTspice IV (now including LTXVII), From beginner to advanced

Administrator
Joined 2007
Paid Member
OK :)

There are other more advanced (but easier) ways to do this. The method I gave is the longhand calculation showing how it is worked out but we can also let LT do all the work for us.

Try this simulation, run it and look at the FFT.

You can change the frequency by changing the line .param freq 12k. The simulation recalculates the required parameters.

The following is a copy of notes I have made over the years:

Code:
THD measurement
The .four statement runs a fourier analysis on the node of interest and computes the THD. To view the results of this analysis, look at the Spice error log through the "view" menu.

Syntax: .four <frequency> [Nharmonics] [Nperiods] <data trace1> [<data trace2> ...]

To get an accurate result from the .four command, it is important to have compression turned off and the time step set properly, and for simulation to stop on an exact integer number of cycles. I also find that I get more consistent results by doing the FFT over multiple periods. 

You can include spice directives on your schematic to automatically set the simulation time and timestep, and also the frequency of the input stimulus based on a single parameter. This way you only have to edit one line in the spice directives to change the input frequency, simulation time, and timestep.

For example:
;turn off compression and use double precision
.options plotwinsize=0
.options numdgt=8

;set frequency and timestep
.param freq 1k
.param num_fft_pts 4096
.param timestep {1/{ {freq} * {num_fft_pts}} }

;perform FFT out to 9th harmonic over last 8 cycles
.four {freq} 9 8 V(ampOut)

For the simulation command, enter the following (modify as desired):
Stop time: {20/freq} 
Maximum Timestep: {timestep}

For the voltage source driving the input, enter {freq} for the frequency.

So whatever frequency you choose, the simulation will run for 20 cycles with the appropriate timestep and input frequency.

If the THD result changes depending on how long you run the simulation before the .four command is executed, that typically means there is DC drift in the circuit.
 

Attachments

  • F1.png
    F1.png
    53.3 KB · Views: 163
  • F2.png
    F2.png
    52.1 KB · Views: 168
  • Draft1.asc
    499 bytes · Views: 79
Great, thanks. I'll try to get to grips with that.
I've copied all the parameters and directives into my One Transistor Amp and still can't see 'total harmonic distortion' in the spice error log.

Also, I'm a bit confused as to when to use the .four command. Is there something suspicious about the very fast speed at which it runs the FFT - a second or two?
 

Attachments

  • Distortion.png
    Distortion.png
    128.9 KB · Views: 161
I've copied all the parameters and directives into my One Transistor Amp and still can't see 'total harmonic distortion' in the spice error log.

I think this is what you intended. It's not easy to see where you went wrong without the .asc file, but it worked for me as shown.
 

Attachments

  • goddlediddles.PNG
    goddlediddles.PNG
    137.9 KB · Views: 164
  • goddlediddles.asc
    1.7 KB · Views: 67
I think this is what you intended. It's not easy to see where you went wrong without the .asc file, but it worked for me as shown.
Great, thanks for your help. It's working now. I'm delighted I got the same result for THD as you. I'm still a little puzzled as to when one should use the .four command. Is it only at the stage of looking at the Spice error log?
 
Administrator
Joined 2007
Paid Member
Looks like you are having fun :)

I'll be honest and say I've never delved into trying to understand the shorthand way of setting the simulations up... so maybe someone has an answer as to why only certain frequencies work correctly doing it this way.

For example if I run the sim at 12k there are glitches. Change to 10k and all is good. Why does this happen because it wouldn't using the longhand method.

So here is the sim at 10k not 12k.

A couple of useful tips.

1/ Run the sim for a good while and only take data from the last part of the run. For example here I am running for 200 cycles at 10k and saving data for just the last 100 cycles.

2/ Things like coupling caps and bootstrap caps etc add a time constant and that adds a slope to the FFT trace. We can make the caps bigger... much bigger... and that takes them out of the equation.

Setting them at 1 gives a value of 1F (note, if you enter 1F as a value it is actually 1 pico farad, one of the oddities of LT and yet 1H is 1 Henry as far as I recall).

So running the sim like this gives us a super detailed display. Last shot shows the glitches at 12k. Why ?
 

Attachments

  • Glitches.png
    Glitches.png
    113.3 KB · Views: 82
  • Circuit.png
    Circuit.png
    113.7 KB · Views: 77
  • FFT.png
    FFT.png
    137.1 KB · Views: 86
Administrator
Joined 2007
Paid Member
How to set the sim up to only look at the last part (100 cycles in this case).

You can also change this parameter on the FFT which alters the scale for want of a better word.
 

Attachments

  • Untitled3.png
    Untitled3.png
    143.4 KB · Views: 120
  • Untitled4.png
    Untitled4.png
    135.3 KB · Views: 124
  • Untitled2.png
    Untitled2.png
    117.6 KB · Views: 120
Looks like you are having fun :)

I'll be honest and say I've never delved into trying to understand the shorthand way of setting the simulations up... so maybe someone has an answer as to why only certain frequencies work correctly doing it this way.

For example if I run the sim at 12k there are glitches. Change to 10k and all is good. Why does this happen because it wouldn't using the longhand method.

So here is the sim at 10k not 12k.

A couple of useful tips.

1/ Run the sim for a good while and only take data from the last part of the run. For example here I am running for 200 cycles at 10k and saving data for just the last 100 cycles.

2/ Things like coupling caps and bootstrap caps etc add a time constant and that adds a slope to the FFT trace. We can make the caps bigger... much bigger... and that takes them out of the equation.

Setting them at 1 gives a value of 1F (note, if you enter 1F as a value it is actually 1 pico farad, one of the oddities of LT and yet 1H is 1 Henry as far as I recall).

So running the sim like this gives us a super detailed display. Last shot shows the glitches at 12k. Why ?
Great, thanks. I've been at it since 5am (no kidding), so too much screen-time means I've experienced a flame-out, and have to make an emergency landing. Back tomorrow for more hours in the left-hand seat.
 
Fully automatic version

I'll be honest and say I've never delved into trying to understand the shorthand way of setting the simulations up.

...

Last shot shows the glitches at 12k. Why ?

Here's a fully "automatic" version. All one has to do is change the Frequency parameter. Plotting, fft-ing, fourier-ing are all taken care of with no other changes and no glitches at any frequency.

Enjoy.
 

Attachments

  • goddlediddles.asc
    1.8 KB · Views: 74
Mooly

In LTSpice component values the units are ignored so an inductor with a value of 1H is the same as one with a value of 1. However in the case of capacitors "F" means "femto" so 1F is 1E-15 Farads (not 1E-9). Another gotcha that trips up a lot of people is that "m" and "M" both mean milli, for 1E6 you use "Meg".

As far as your Fourier issues are concerned unfortunately I don't have time to run the sim today but my *guess* is that you're not in steady state. Fourier analysis doesn't make any sense if the simulation is changing state. That's why I always run a .tran analysis first to understand the time behavior before I do an ac analysis. Then I set the simulation to not collect data until steady state is reached.

tommost


Looks like you are having fun :)

I'll be honest and say I've never delved into trying to understand the shorthand way of setting the simulations up... so maybe someone has an answer as to why only certain frequencies work correctly doing it this way.

For example if I run the sim at 12k there are glitches. Change to 10k and all is good. Why does this happen because it wouldn't using the longhand method.

So here is the sim at 10k not 12k.

A couple of useful tips.

1/ Run the sim for a good while and only take data from the last part of the run. For example here I am running for 200 cycles at 10k and saving data for just the last 100 cycles.

2/ Things like coupling caps and bootstrap caps etc add a time constant and that adds a slope to the FFT trace. We can make the caps bigger... much bigger... and that takes them out of the equation.

Setting them at 1 gives a value of 1F (note, if you enter 1F as a value it is actually 1 pico farad, one of the oddities of LT and yet 1H is 1 Henry as far as I recall).

So running the sim like this gives us a super detailed display. Last shot shows the glitches at 12k. Why ?
 
Here's a fully "automatic" version. All one has to do is change the Frequency parameter. Plotting, fft-ing, fourier-ing are all taken care of with no other changes and no glitches at any frequency.

Enjoy.
I've used your settings and got a very good result. But I set the input and output caps at 8000μF, and it ran very smoothly with a very similar figure for the THD as before and a great amount of detail in the trace..
 

Attachments

  • 8000μF FFT.png
    8000μF FFT.png
    79.4 KB · Views: 113