Electronics-Related.com
Forums

LT Spice FFT scaling

Started by John Larkin July 9, 2018
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 ? 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. >
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. 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.
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. 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.
On Mon, 09 Jul 2018 19:53:01 -0700, Jim Thompson
<To-Email-Use-The-Envelope-Icon@On-My-Web-Site.com> 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. > > ...Jim Thompson
I simulated 100 seconds of 1 Hz of 1 amp sine wave. Exactly 100 cycles. The average was -3.9 nA. Well, nobody's perfect. -- John Larkin Highland Technology, Inc lunatic fringe electronics
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. -- Regards, Martin Brown
On 09/07/2018 20:54, Jim Thompson 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. > > As I've harped almost continuously... with simulators, particularly of > the toyLTspice variety... "fast" and "accuracy" are inversely > proportional.
In all numerical methods that is always true. More accurate methods tend to be slower unless they are highly optimised for a particular sort of problem. There are some incredibly cute tricks for motion under the influence of an inverse square law for instance.
> > Stop whining and learn to live with it. > > ...Jim Thompson
Or if you really need to push the accuracy envelop for some reason look up Richardson extrapolation and Shank's transform which will deliver more accurate answers from slightly dodgy raw data on a good day. The latter will also determine an "answer" even when the series it is being asked to give a judgement on is divergent. eg. feeding partial sums 1, 3, 7 into Shank's transform gives: ((1*7)-(3*3))/(1+7-2*3) = -2/2 = -1 (the same result as in twos complement arithmetic) Pure mathematicians will be running away crossing themselves and shouting heresy at this point but physicists still use these tricks. They are a bit out of fashion now but were more popular long before digital computers and numerical methods came of age. The human computors found it easier to transform the algebra into something that converged more easily rather than hammer it out at a finer step size. -- Regards, Martin Brown
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. That means that it has to interpolate to get regularly-spaced samples for the FFT. 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 -- 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/10/18 03:13, Martin Brown wrote:
> On 09/07/2018 20:54, Jim Thompson 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. >> >> As I've harped almost continuously... with simulators, particularly of >> the toyLTspice variety... "fast" and "accuracy" are inversely >> proportional. > > In all numerical methods that is always true. More accurate methods tend > to be slower unless they are highly optimised for a particular sort of > problem. There are some incredibly cute tricks for motion under the > influence of an inverse square law for instance. >> >> Stop whining and learn to live with it. >> >> &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;&nbsp; ...Jim Thompson > > Or if you really need to push the accuracy envelop for some reason look > up Richardson extrapolation and Shank's transform which will deliver > more accurate answers from slightly dodgy raw data on a good day. > > The latter will also determine an "answer" even when the series it is > being asked to give a judgement on is divergent. > > eg. feeding partial sums 1, 3, 7 into Shank's transform gives: > > ((1*7)-(3*3))/(1+7-2*3) = -2/2 = -1 > > (the same result as in twos complement arithmetic) > > Pure mathematicians will be running away crossing themselves and > shouting heresy at this point but physicists still use these tricks. > They are a bit out of fashion now but were more popular long before > digital computers and numerical methods came of age. The human computors > found it easier to transform the algebra into something that converged > more easily rather than hammer it out at a finer step size. >
Right, because human-powered differential equation solvers almost always use a fixed step size, which works well with extrapolation methods. The assumption underlying Richardson is that the truncation error goes as a lowish-order polynomial in the step size h. SPICE in general won't provide evenly-spaced data, so that it'll have to interpolate to a regular grid to apply the FFT. Each run's truncation error (both SPICE and interpolation) will depend on the details of the adaptive time step algorithm, so the extrapolation is liable to make it worse rather than better. 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/18 18:14, Jim Thompson 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... > > As I've also harped... selecting "Alternate Solver" forces LTspice to > behave in conventional Berkeley Spice fashion... no short-cuts, none > of Mikey's "behavior" models that take gross liberties so that FAST > (or is it HALF-FAST?) becomes the marketing line?
Harped is the word, for sure (or perhaps bloviated). ;) I'm not invested in the quality of LTspice's 'normal' solver, and I don't know its internal details, but it's not like numerical algorithms have stood still for the last 50 years. "Conventional Berkeley Spice fashion" was a good 1968 answer for a CDC 6400, which was pretty swoopy for its day, but less powerful than a 21st century elevator controller. It had 200k words of memory (about 1.5 MB) and a 10-MHz clock. You generally get more performance improvement by using a better algorithm than you do by buying a faster computer. Cheers Phil Hobbs (who has written a lot of simulation code) -- 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/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. > > ...Jim Thompson >
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. 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 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. >> >> ...Jim Thompson >> > >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. > >Cheers > >Phil Hobbs
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. -- John Larkin Highland Technology, Inc picosecond timing precision measurement jlarkin att highlandtechnology dott com http://www.highlandtechnology.com