Electronics-Related.com
Forums

LT Spice FFT scaling

Started by John Larkin July 9, 2018
On 10/07/2018 22:53, bitrex wrote:
> On 07/10/2018 03:03 AM, Martin Brown wrote: >> On 09/07/2018 20:00, John Larkin wrote: >>> >>> >>> Suppose I create a current source that's a sine wave, amplitude 1, >>> frequency 1, and then run a transient response. Then plot current and >>> FFT. On a linear scale, it has some harmonics (no surprise) but the >>> amplitude of the fundamental is 690 mA. >>> >>> Why 690? Is that a bad approximation of 707? >> >> Very probably. >> >> If you want to get a better approximation relatively cheaply then use >> three simulations to compute it for 2dt, dt, dt/2 and use Richardson >> extrapolation to remove the leading error term. >> >> https://en.wikipedia.org/wiki/Richardson_extrapolation >>> >>> Update: if I set the time step to 10 us, it runs slow but the FFT >>> amplitude is 706.9 mA. So the FFT reports RMS. >> >> <sin^2(x)> = 1/2 >> >> Hence amplitude = 1/sqrt(2) >> >> It is common in numerical FFTs for the scaling to be done only in one >> direction as a shortcut rather than symmetrically as in symbolic maths. >> >> > > My copy of LTSpice gets it almost exactly right at 707 mA if I let a 1 > Hz sine wave sim for 1 cycle, with the default settings. > > I definitely couldn't tell ya what information there is to be gained > from letting a FFT sim of a sine wave run any longer than that.
Amplitude drift for one thing. You have to be very careful to preserve full accuracy if you are generating sin cos pair using the recurrence relations if you want to make sure ((-1,0)^(1/N))^N = -1 almost exactly.
> Everything you need to know about the spectrum of an > infinitely-repeating waveform defined by a closed-form mathematical > expression is contained in one cycle, like why would one think DTFT/FFT > transform of a signal like that is going to get _more_ accurate over ten > cycles, or a hundred? It's not calculating some cycle-by-cycle average. > The DTFT/FFT doesn't "know" anything at all about cycles, in fact.
The simulation will drift in amplitude due to the limitations of the general DE methods used to conserve energy. Could go up or down. How bad the drift is can be tested by simulating for 2,4,8,16,32,64 cycles. It doesn't take eps to be very big for (1+eps)^N to grow. -- Regards, Martin Brown
On Wed, 11 Jul 2018 09:19:04 +0300, upsidedown@downunder.com wrote:

>On Tue, 10 Jul 2018 14:37:59 -0700, John Larkin ><jjlarkin@highland_snip_technology.com> wrote: > >>On Tue, 10 Jul 2018 17:10:54 -0400, bitrex <user@example.net> wrote: >> >>>On 07/10/2018 01:33 PM, John Larkin wrote: >>>> On Mon, 9 Jul 2018 23:16:12 -0400, bitrex <user@example.net> wrote: >>>> >>>>> On 07/09/2018 11:09 PM, bitrex wrote: >>>>>> On 07/09/2018 08:00 PM, John Larkin wrote: >>>>>>> On Mon, 09 Jul 2018 16:47:06 -0700, Jim Thompson >>>>>>> <To-Email-Use-The-Envelope-Icon@On-My-Web-Site.com> wrote: >>>>>>> >>>>>>>> On Mon, 09 Jul 2018 16:03:54 -0700, John Larkin >>>>>>>> <jjlarkin@highland_snip_technology.com> wrote: >>>>>>>> >>>>>>>>> On Mon, 09 Jul 2018 13:06:15 -0700, boB <boB@K7IQ.com> wrote: >>>>>>>>> >>>>>>>>>> On Mon, 09 Jul 2018 12:00:24 -0700, John Larkin >>>>>>>>>> <jjlarkin@highland_snip_technology.com> wrote: >>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>> >>>>>>>>>>> Suppose I create a current source that's a sine wave, amplitude 1, >>>>>>>>>>> frequency 1, and then run a transient response. Then plot current and >>>>>>>>>>> FFT. On a linear scale, it has some harmonics (no surprise) but the >>>>>>>>>>> amplitude of the fundamental is 690 mA. >>>>>>>>>>> >>>>>>>>>>> Why 690? Is that a bad approximation of 707? >>>>>>>>>>> >>>>>>>>>>> Update: if I set the time step to 10 us, it runs slow but the FFT >>>>>>>>>>> amplitude is 706.9 mA. So the FFT reports RMS. >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> Try it also with the alternate solver ?&#4294967295; Will probably be the same as >>>>>>>>>> decreasing the step size though I imagine.&#4294967295; Not a lot to go wrong... >>>>>>>>>> go wrong.... go wrong... >>>>>>>>> >>>>>>>>> It's just a sinewave current source. There's really nothing to solve! >>>>>>>>> I think LT Spice just picks an ambitiously small delta-T by default. >>>>>>>> >>>>>>>> Nope.&#4294967295; It's a cockpit set-up error >:-} >>>>>>>> >>>>>>>> &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295; ...Jim Thompson >>>>>>> >>>>>>> There's no operator error at all. LT Spice gives one a choice of >>>>>>> solver, a choice of dt, and several other params to tweak to trade off >>>>>>> speed against accuracy. What's wrong with that? >>>>>>> >>>>>>> I needed to know what units the FFT uses when it displays current. The >>>>>>> answer is RMS amps. Sinewave sources, voltage or current, use peak >>>>>>> amps. >>>>>>> >>>>>> >>>>>> I think I see the problem. How many cycles at 1 Hz did you let the >>>>>> simulation run for? If you set the transient sim to 1 second at 1 Hz it >>>>>> hits 707 mA on the linear FFT just about on the money. 10 seconds, it's >>>>>> worse. 100 seconds a lot worse. >>>>>> >>>>>> Floating point roundoff errors are accumulating cycle by cycle and a >>>>>> small step size makes it worse. >>>> >>>> Except, of course, that setting a smaller step size make the FFT >>>> better. >>>> >>>> >>>> The sine is starting at 0 amplitude at 0 >>>>>> seconds, but after "exactly" say 100 cycles it's not ending there. And >>>>>> the end-point error is throwing off the RMS calculation. >>>>>> >>>>> >>>>> That is to say if you are seeing higher harmonics at all then by >>>>> Parseval's theorem/energy conservation the fundamental definitely cannot >>>>> have its mathematically ideal pure sinewave maximum amplitude. >>>> >>>> The physical universe has to conserve energy. A computer simulation >>>> doesn't. >>>> >>>> >>> >>>That total signal energy is conserved when taking the Fourier transform >>>is implied by the mathematical fact that the Fourier transform is a >>>unitary transform. It has to behave that way in a computer simulation as >>>well or it's not the Fourier transform! >> >>The it's not. The LT Spice FFT clearly has spectral lines coming from >>a pure sine wave current source. > >What kind of window function did you use ?
LT Spice has a huge list of hyphenated-name window functions. I tried a few at random and they didn't change the spectrum much. What does clean up the spectrum (of a pure sine wave source!) is running more cycles and setting smaller dT. Try it. -- John Larkin Highland Technology, Inc lunatic fringe electronics
> > What does clean up the spectrum (of a pure sine wave source!) is > running more cycles and setting smaller dT. > >
so the issue may be the transient at the start and stop. unless the analysis is run over EXACTLY one cycle, there will be small errors. running more cycles may reduce the percentage of that error mark
On Wed, 11 Jul 2018 07:55:28 -0700 (PDT), makolber@yahoo.com wrote:

> >> >> What does clean up the spectrum (of a pure sine wave source!) is >> running more cycles and setting smaller dT. >> >> > >so the issue may be the transient at the start and stop. > >unless the analysis is run over EXACTLY one cycle, there will be small errors. > >running more cycles may reduce the percentage of that error > >mark
I was running exactly 100 cycles, my original goal being to determine the absolute scaling of the FFT, which turned out to be RMS amps in my case. Generators are scaled in peak volts or amps. Just checking. -- John Larkin Highland Technology, Inc lunatic fringe electronics
On 07/10/18 17:27, bitrex wrote:
> On 07/10/2018 08:41 AM, Phil Hobbs wrote: >> On 07/09/18 15:00, John Larkin wrote: >>> >>> >>> Suppose I create a current source that's a sine wave, amplitude 1, >>> frequency 1, and then run a transient response. Then plot current and >>> FFT. On a linear scale, it has some harmonics (no surprise) but the >>> amplitude of the fundamental is 690 mA. >>> >>> Why 690? Is that a bad approximation of 707? >>> >>> Update: if I set the time step to 10 us, it runs slow but the FFT >>> amplitude is 706.9 mA. So the FFT reports RMS. >> >> LTspice, like most simulators apart from FDTD EM simulators, uses a >> variable time step.&nbsp; That means that it has to interpolate to get >> regularly-spaced samples for the FFT.&nbsp; That interpolation introduces >> significant amounts of truncation error, which is probably what's >> responsible for both the harmonics and the reduced fundamental. >> >> Cheers >> >> Phil Hobbs >> >> > > the fact that the fundamental is 690 instead of 707 is not a "bad > approximation" it's a perfectly valid and correct Fourier transform of > some other sequence of samples generated through a given interpolation > algorithm,
So what? I didn't complain that it was a "bad approximation", just that it depended on numerical details that were irrelevant to the problem at hand. Correctly coding a poor algorithm doesn't make your program "perfectly valid and correct" unless you're scrambling to keep your job.
> that's not a mathematically abstract pure sine wave. SPICE > doesn't "know" anything about sine waves or that they should be treated > any different than any other random sequence of samples.
So what? We're not talking about Volkswagen's engine management software here. The use of interpolation to force variable-step results onto the FFT's uniform grid introduces truncation error, so the resulting frequency-domain plots can very easily exhibit artifacts.
> > The implication by the OP seems to be that the FFT in LTSpice is somehow > a wrong or "sloppy" implementation, maybe so but I don't think that > follows from the observed symptoms at all.
The FFT isn't the issue. As you say, it's an algorithm to turn discrete time-domain samples into samples of the Fourier transform of the input sequence. But when the assumptions underlying the theorems aren't met, the algos will produce wrong answers to the question the user is asking. Unit tests can ensure that the code is doing what it's supposed to, but they're powerless if the design is wrong. Cheers Phil Hobbs -- Dr Philip C D Hobbs Principal Consultant ElectroOptical Innovations LLC / Hobbs ElectroOptics Optics, Electro-optics, Photonics, Analog Electronics Briarcliff Manor NY 10510 http://electrooptical.net http://hobbs-eo.com
On 07/09/2018 03:00 PM, John Larkin wrote:
> > > Suppose I create a current source that's a sine wave, amplitude 1, > frequency 1, and then run a transient response. Then plot current and > FFT. On a linear scale, it has some harmonics (no surprise) but the > amplitude of the fundamental is 690 mA. > > Why 690? Is that a bad approximation of 707? > > Update: if I set the time step to 10 us, it runs slow but the FFT > amplitude is 706.9 mA. So the FFT reports RMS. > > >
After some research here is a quick guide on how to get very low spurious harmonic energy FFT plots in LTSpice: 1. Turn off simulation data compression using the directive on the schematic ".options plotwinsize=0" 2. Make the maximum timestep under "Edit Simulation Command" an appropriate reciprocal power of two e.g. 1/65536. 3. Simulate for an integer number of cycles at least 8x reciprocal of Nyquist frequency for given signal. 4. In the FFT setup make "Number of data point samples in time" the same power of two as above e.g. 65536. 5. Turn off "quadratic interpolate uncompressed data." 6. Select windowing function "Blackman." Using that setup here's what I get for a linear-scale FFT of 100 cycles of 1 Hz 2 amp p2p sine wave: <https://imgur.com/a/natdJhq>
On 10/07/2018 18:26, John Larkin wrote:
> On Tue, 10 Jul 2018 09:28:02 -0400, Phil Hobbs > <pcdhSpamMeSenseless@electrooptical.net> wrote: > >> On 07/09/18 22:53, Jim Thompson wrote: >>> On Mon, 09 Jul 2018 17:00:27 -0700, John Larkin >>> <jjlarkin@highland_snip_technology.com> wrote: >>> >>>> On Mon, 09 Jul 2018 16:47:06 -0700, Jim Thompson >>>> <To-Email-Use-The-Envelope-Icon@On-My-Web-Site.com> wrote: >>>> >>>>> On Mon, 09 Jul 2018 16:03:54 -0700, John Larkin >>>>> <jjlarkin@highland_snip_technology.com> wrote: >>>>> >>>>>> On Mon, 09 Jul 2018 13:06:15 -0700, boB <boB@K7IQ.com> wrote: >>>>>> >>>>>>> On Mon, 09 Jul 2018 12:00:24 -0700, John Larkin >>>>>>> <jjlarkin@highland_snip_technology.com> wrote: >>>>>>>> >>>>>>>> Suppose I create a current source that's a sine wave, amplitude 1, >>>>>>>> frequency 1, and then run a transient response. Then plot current and >>>>>>>> FFT. On a linear scale, it has some harmonics (no surprise) but the >>>>>>>> amplitude of the fundamental is 690 mA. >>>>>>>> >>>>>>>> Why 690? Is that a bad approximation of 707? >>>>>>>> >>>>>>>> Update: if I set the time step to 10 us, it runs slow but the FFT >>>>>>>> amplitude is 706.9 mA. So the FFT reports RMS. >>>>>>> >>>>>>> >>>>>>> Try it also with the alternate solver ? Will probably be the same as >>>>>>> decreasing the step size though I imagine. Not a lot to go wrong... >>>>>>> go wrong.... go wrong... >>>>>> >>>>>> It's just a sinewave current source. There's really nothing to solve! >>>>>> I think LT Spice just picks an ambitiously small delta-T by default. >>>>> >>>>> Nope. It's a cockpit set-up error >:-} >>>>> >>>>> ...Jim Thompson >>>> >>>> There's no operator error at all. LT Spice gives one a choice of >>>> solver, a choice of dt, and several other params to tweak to trade off >>>> speed against accuracy. What's wrong with that? >>>> >>>> I needed to know what units the FFT uses when it displays current. The >>>> answer is RMS amps. Sinewave sources, voltage or current, use peak >>>> amps. >>> >>> Except that the waveform simulated is NOT sin(wt), it's >>> u_1(t)*sin(wt). >>> >>> From which the DC offset arises. >> >> The DFT/FFT only applies to periodic sequences. For a sequence periodic >> in the FFT interval and having compact support in the frequency domain, >> the FFT produces correct samples of the underlying continuous-time >> transform. >> >> Real data, or in this case interpolated simulation data, doesn't fit >> those conditions exactly, which leads to errors. It's not very >> difficult to compute an error bound in any given case.
There is a way to make an FFT give a very good approximation to a DFT in the case of unequally spaced data provided that the data is reasonably compact on the new uniform grid. You convolve the raw data with an interpolation function on compact support (Jodrell used to use truncated Gaussian, VLA used a variant prolate spheroidal bessel). This means that your final result is then multiplied by a scale factor that has to be corrected out but by keeping track of how much contribution is put into each cell and the cumulative value you can get a pretty good FFT out of data that are not uniformly spaced with minimal artefacts. See https://github.com/mrbell/gfft/wiki/Gridding The paper you want is Schwab 1984 from there http://adsabs.harvard.edu/abs/1984iimp.conf..333S Probably no help to the OP but I think Phil will enjoy it!
> > I tried a couple of windowing functions (there is a huge list > available) but they didn't change the FFT much. Smaller time steps > really do.
Windowing functions just alter the sidebands caused by the discontinuity between the start and end of your time series data. If your signal is exactly periodic on the FFT length it should be irrelevant (and will only make things worse). You might find that choosing a timescale that is an exact power of two will give less side effects depending on exactly how LT spice does it internally. Modern FFT packages can do most sizes efficiently. I suspect you are suffering amplitude drift. We used to optimise FFT recurrence relations to the extent of tabulating the discrete roots of rN = (-1)(1/2^N) that gave least error when rN^(2^N) was computed by the recurrence algorithm. It was never quite the same as theory. -- Regards, Martin Brown
On 10/07/2018 22:37, John Larkin wrote:
> On Tue, 10 Jul 2018 17:10:54 -0400, bitrex <user@example.net> wrote: > >> On 07/10/2018 01:33 PM, John Larkin wrote: >>> On Mon, 9 Jul 2018 23:16:12 -0400, bitrex <user@example.net> wrote: >>> >>>> On 07/09/2018 11:09 PM, bitrex wrote: >>>>> On 07/09/2018 08:00 PM, John Larkin wrote: >>>>>> On Mon, 09 Jul 2018 16:47:06 -0700, Jim Thompson >>>>>> <To-Email-Use-The-Envelope-Icon@On-My-Web-Site.com> wrote: >>>>>> >>>>>>> On Mon, 09 Jul 2018 16:03:54 -0700, John Larkin >>>>>>> <jjlarkin@highland_snip_technology.com> wrote: >>>>>>> >>>>>>>> On Mon, 09 Jul 2018 13:06:15 -0700, boB <boB@K7IQ.com> wrote: >>>>>>>> >>>>>>>>> On Mon, 09 Jul 2018 12:00:24 -0700, John Larkin >>>>>>>>> <jjlarkin@highland_snip_technology.com> wrote: >>>>>>>>> >>>>>>>>>> >>>>>>>>>> >>>>>>>>>> Suppose I create a current source that's a sine wave, amplitude 1, >>>>>>>>>> frequency 1, and then run a transient response. Then plot current and >>>>>>>>>> FFT. On a linear scale, it has some harmonics (no surprise) but the >>>>>>>>>> amplitude of the fundamental is 690 mA. >>>>>>>>>> >>>>>>>>>> Why 690? Is that a bad approximation of 707? >>>>>>>>>> >>>>>>>>>> Update: if I set the time step to 10 us, it runs slow but the FFT >>>>>>>>>> amplitude is 706.9 mA. So the FFT reports RMS. >>>>>>>>> >>>>>>>>> >>>>>>>>> Try it also with the alternate solver ?&nbsp; Will probably be the same as >>>>>>>>> decreasing the step size though I imagine.&nbsp; Not a lot to go wrong... >>>>>>>>> go wrong.... go wrong... >>>>>>>> >>>>>>>> It's just a sinewave current source. There's really nothing to solve! >>>>>>>> I think LT Spice just picks an ambitiously small delta-T by default. >>>>>>> >>>>>>> Nope.&nbsp; It's a cockpit set-up error >:-} >>>>>>> >>>>>>> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...Jim Thompson >>>>>> >>>>>> There's no operator error at all. LT Spice gives one a choice of >>>>>> solver, a choice of dt, and several other params to tweak to trade off >>>>>> speed against accuracy. What's wrong with that? >>>>>> >>>>>> I needed to know what units the FFT uses when it displays current. The >>>>>> answer is RMS amps. Sinewave sources, voltage or current, use peak >>>>>> amps. >>>>>> >>>>> >>>>> I think I see the problem. How many cycles at 1 Hz did you let the >>>>> simulation run for? If you set the transient sim to 1 second at 1 Hz it >>>>> hits 707 mA on the linear FFT just about on the money. 10 seconds, it's >>>>> worse. 100 seconds a lot worse. >>>>> >>>>> Floating point roundoff errors are accumulating cycle by cycle and a >>>>> small step size makes it worse. >>> >>> Except, of course, that setting a smaller step size make the FFT >>> better. >>> >>> >>> The sine is starting at 0 amplitude at 0 >>>>> seconds, but after "exactly" say 100 cycles it's not ending there. And >>>>> the end-point error is throwing off the RMS calculation. >>>>> >>>> >>>> That is to say if you are seeing higher harmonics at all then by >>>> Parseval's theorem/energy conservation the fundamental definitely cannot >>>> have its mathematically ideal pure sinewave maximum amplitude. >>> >>> The physical universe has to conserve energy. A computer simulation >>> doesn't. >>> >>> >> >> That total signal energy is conserved when taking the Fourier transform >> is implied by the mathematical fact that the Fourier transform is a >> unitary transform. It has to behave that way in a computer simulation as >> well or it's not the Fourier transform! > > The it's not. The LT Spice FFT clearly has spectral lines coming from > a pure sine wave current source.
A picture is worth a thousand words here. Odd or even harmonics? Try doing 128 cycles or 64 cycles and does the harmonic content vanish? -- Regards, Martin Brown
On 11/07/2018 15:08, John Larkin wrote:
> On Wed, 11 Jul 2018 09:19:04 +0300, upsidedown@downunder.com wrote: > >> On Tue, 10 Jul 2018 14:37:59 -0700, John Larkin >> <jjlarkin@highland_snip_technology.com> wrote: >> >>> On Tue, 10 Jul 2018 17:10:54 -0400, bitrex <user@example.net> wrote: >>> >>>> That total signal energy is conserved when taking the Fourier transform >>>> is implied by the mathematical fact that the Fourier transform is a >>>> unitary transform. It has to behave that way in a computer simulation as >>>> well or it's not the Fourier transform! >>> >>> The it's not. The LT Spice FFT clearly has spectral lines coming from >>> a pure sine wave current source. >> >> What kind of window function did you use ? > > LT Spice has a huge list of hyphenated-name window functions. I tried > a few at random and they didn't change the spectrum much. > > What does clean up the spectrum (of a pure sine wave source!) is > running more cycles and setting smaller dT. > > Try it.
Try a power of two number of cycles and I think some of your artefacts will spontaneously vanish. It will probably keep on improving until dT is O(2*sqrt(eps)) but it will get very very slow and tedious. Checking the actual harmonic content by simulating a couple of the stronger harmonics cos & sin and computing their products explicitly in spice would show if it is FFT artefact ot numerical. -- Regards, Martin Brown
On 07/11/18 22:24, bitrex wrote:
> On 07/09/2018 03:00 PM, John Larkin wrote: >> >> >> Suppose I create a current source that's a sine wave, amplitude 1, >> frequency 1, and then run a transient response. Then plot current and >> FFT. On a linear scale, it has some harmonics (no surprise) but the >> amplitude of the fundamental is 690 mA. >> >> Why 690? Is that a bad approximation of 707? >> >> Update: if I set the time step to 10 us, it runs slow but the FFT >> amplitude is 706.9 mA. So the FFT reports RMS. >> >> >> > > > After some research here is a quick guide on how to get very low > spurious harmonic energy FFT plots in LTSpice: > > 1. Turn off simulation data compression using the directive on the > schematic ".options plotwinsize=0" > > 2. Make the maximum timestep under "Edit Simulation Command" an > appropriate reciprocal power of two e.g. 1/65536. > > 3. Simulate for an integer number of cycles at least 8x reciprocal of > Nyquist frequency for given signal. > > 4. In the FFT setup make "Number of data point samples in time" the same > power of two as above e.g. 65536. > > 5. Turn off "quadratic interpolate uncompressed data." > > 6. Select windowing function "Blackman." > > Using that setup here's what I get for a linear-scale FFT of 100 cycles > of 1 Hz 2 amp p2p sine wave: > > <https://imgur.com/a/natdJhq>
The trouble is that you don't know to do that when you're simulating something more complicated, so you can get snookered. How important was each item? I can well believe that turning off compression helped a lot, but I doubt that the window did anything except smear out the spectrum by a few samples. Cheers Phil Hobbs -- Dr Philip C D Hobbs Principal Consultant ElectroOptical Innovations LLC / Hobbs ElectroOptics Optics, Electro-optics, Photonics, Analog Electronics Briarcliff Manor NY 10510 http://electrooptical.net http://hobbs-eo.com