Connect with us

LTspice THD+N and other questions...

Discussion in 'CAD' started by Mike Deblis, Nov 11, 2003.

Scroll to continue with content
  1. Mike Deblis

    Mike Deblis Guest

    Forgive my ignorance...

    Is there a way to get an estimation of the THD+N of an audio amp in
    LTSpice? Ideally, I'd like to plot THD+N against frequency & possibly

    next question - what is the simplist way of generating a fairly
    complex repetative sequence of pulses on a number of nets? What I'm
    trying to do is to open & close a lot of analogue gates in a fairly
    complex repative sequence. Again, ideally I'd like to manipulate this
    sequence fairly simply, rather than change a load of PULSE statements
    as I do at the moment (its rather prone to error).


  2. Another Mike. Welcome, Mike.

    The lack of a THD+N meter in SPICE is a problem that I've always wanted
    a solution to.

    SPICE guys, who are not usually audio engineers, will suggest the FFT as
    a way to examine the spectral output, but it's not as informative as
    looking at the output of a harmonic analyzer, which not only gives you
    the ratio of the sum of the amplitudes of the various generated
    harmonics (minus the fundamental, and within the bandwidth specified by
    the user) to the total amplitude of the circuit's output, but -- and
    this next is very useful to the experienced analog circuit designer user
    -- by using an oscilloscope, one can view the actual distortion of the
    device under test with the stimulus sine wave subtracted. This
    "distortion residue" output is a graphic display of how the device is
    distorting the sine wave. One trick that is often used is to sum the
    (greatly amplified) distortion residue with the input signal on an
    oscilloscopes screen so you can see how the sine wave has been changed
    as a result of the various nonlinearities in
    the device.

    These bits are all modelable in SPICE using subcircuits, but it's
    cumbersome -- to me, because I am not very clever with SPICE. To see the
    distortion residue minus the fundamental, you need to basically build a
    band-reject filter with a very narrow (less than an octave so as not to
    attenuate the 2nd harmonic), very deep (like 120dB) notch. If you want
    to change the generator frequency to examine the performance of the
    circuit at a different frequency, you need to change the notch filter's
    center frequency, and that's tedious as hell, esp. since the component
    value have to be specified to many digits to get the notch spot-on and
    narrow enough.

    Another way of getting rid of the stimulus signal so the nonlinearities
    can be made viewable is to subtract the input from the output. This does
    not require a notch filter, but the gain of the circuit needs to be
    established to a high enough degree so that the results are accurate to
    within, say, 80dB. Math again -- calculate the gain, then feed that
    number into the summer to get the residual output. This might not be
    useful in circuits that change their gain intentionally -- like
    limiters, expanders or compressors, or unintentionally, due to bias
    point shifts that can occur when a circuit element is driven into
    nonlinearity. But maybe the gain calculations could be made in real-time.

    Making a "virtual" THD meter in SPICE that is parameter-driven seems
    very do-able for some of the wizards that hang out in CAD-land.
    Generally speaking, one need to take the output of the circuit and notch
    out or subtract the fundamental sine wave. A way to specify the upper
    cutoff frequency (usually 20kHz, but should be user-adjustable). A
    little waveform math to compare the amplitude of this distortion reside
    as a percentage of the device's total output would then provide the
    usual THD number. One could easily sum the distortion residue with the
    input sine wave to get a sense of what's happening to cause high THD

    Of course, SPICE models generally provide a simplified version of real
    components' nonlinearities, so the results of our virtual THD meter
    would only be as good as our models, and would need to be verified in
    real life. But even so, this tool, which is basic to audio design, would
    be very useful during the initial design phase of a complex analog audio

    The "N" portion of THD+N is not likely to be modelable unless you add
    noise sources of the appropriate amplitude and spectrum to all your
    component models -- then, of course, the aforementioned virtual THD
    meter would simply sum the noise with the distortion reside and give you
    that "N" number, just like a real harmonic analyzer.
  3. Mike,
    Noise and distortion would come from different simulations.
    You can compute noise from .noise analysis. An example
    is supplied as ./examples/Educational/noise.asc. Noise
    density is automatically plotted as a function of frequency.
    You can integrate that in the waveform viewer to the noise
    over a bandwidth. You can also plot noise density as a
    function of temperature or anything else. See the example
    stepnoise.asc to plot noise density as a function of tail
    resistor of a diff pair. Note that many SPICE models don't
    have noise modeled. Unless you verify that each device's
    noise is modeled, you're just going to be looking at the
    Johnson noise of the resistances.

    The distortion can be reached with a .tran analysis and
    subsequent FFT's of the waveform data or a .four commands.
    Usually you have to turn off waveform compression and
    stipulate a small timestep for this to give meaning full
    results. The .four command can give THD from a specified
    number of harmonics. The simulation can be stepped over
    temperature or a parameter to step frequency. The results
    are printed in the .log file.
    The method you're using is fine. Other methods would be
    to use PWL's saved in files for .wav file input/output.
    See the appropriate sections of the help documentation.

  4. ldg

    ldg Guest

    On Tue, 11 Nov 2003 16:29:24 GMT, "Mike Rocket J. Squirrel Elliott"

    Here's something to play with. It's sort of a start on your spice
    based thd meter, but very primitive. I decided to play with the
    LTspice Laplace transforms today. Using the .params statement makes
    changing the frequency easy enough. You'll probably have a much
    better implementation since I've never seen the inside of a thd meter.
    I saw some thd meters use a 400 hz high pass filter. This is easy to
    add. So is bandlimiting with a lowpass (first order section provided)

    It would help to know the characteristics of the bandstop and a block
    diagram of such an instrument. I've done something very simple here.
    Perhaps someone can extend this. I'm out of play time right now.

    The answer comes out on meter3 in percent. The error signal is at




    * This file fakes a Distortion Analyzer. It cheats in that it uses
    * frequency of the error component to drive a synchronous detector.
    * An AC analysis will show the bandpass filter at BPout and the
    * filter at ERROR. (Uses the BPF to BSF transformation 1-H(S))
    * It still has a -65 fundamental component in the ERROR signal, so
    * be better. The notch sure looks deep enough though.

    .param tone = 1000
    .param noise = 2000

    VIN1 1 0 SIN(0 0.005 'noise' 0 0 0) AC 1
    * noise signal 5mv pk

    VIN2 2 1 SIN(0 0.5 'tone' 0 0 0)
    * input tone 0.5v pk

    XU1 2 BPout bpf1p PARAMS: HO = 1 FOP = 'tone' ZETAP = 0.2
    * Bandpass filter

    Esum ERROR 0 BPout 2 1
    * Sums output of filter with input to make BSF

    Vpul1 Pulse1 0 pulse(1 -1 0 1n 1n '0.5/noise' '1/noise')
    * ok, I faked a rectifier

    XU2 0 ERROR 0 Pulse1 RECT1 0 idmult
    * does the rectification

    Vpul2 Pulse2 0 pulse(-1 1 0 1n 1n '0.5/tone' '1/tone')
    * faked another rectifier
    XU3 0 Bpout 0 Pulse2 RECT2 0 idmult
    * does the other rectification

    XU4 Rect1 Fil1 LPF1P PARAMS: HO = 1 FOP = 20
    * LPF at 20 hz
    XU5 Rect2 Fil2 LPF1P PARAMS: HO = 1 FOP = 20
    * LPF at 20 hz

    Escale1 meter1 0 Fil1 0 'pi/2'
    * I didn't have to scale this here, but I did . . .
    * And I really faked a meter % :)
    Escale2 meter2 0 Fil2 0 '(pi/2)'
    Bscale3 meter3 0 v = 106*V(meter1)/V(meter2)
    * note the 06 fudge . . .

    *.AC DEC 200 1 1E8
    .TRAN 10u 0.055 .005

    ..SUBCKT BPF1P IN OUT PARAMS: Ho='1' Fop=1000 Zetap=0.01
    ..ENDS BPF1P

    EMUL OUT REF POLY(2) (PIN1, NIN1) (PIN2, NIN2) 0 0 0 0 1

    ..SUBCKT LPF1P IN OUT PARAMS: Ho='1' Fop='1000'
    ELP OUT 0 IN 0 LAPLACE = Ho/(1+S/(2*pi*Fop))

  5. ldg

    ldg Guest

    Use the .params? It's still a pain to set up, but easier with
    equations. Then it's really easy to change . .

  6. Generally speaking, the high-pass function on a THD meter is used to
    block powerline-related noise (ripple) so that you are measuring the
    harmonics of the input signal and not those harmonics plus a bunch of
    hum and stuff. Not needed, I don't think, in a SPICE meter, since our
    power supplies are so supremely quiet.
    Hm. For the bandstop, your hardware-based harmonic analyzers usually
    have 20kHz and 80kHz cutoffs. A fourth-order Butterworth would be fine.

    A block diagram. I could probably source the block diagram of a Sound
    Technology 1701A -- an industry workhorse. It would take a couple of
    days. Interested? E-mail me at my (munged) e-mail address.

    I've done something very simple here.
    Larry, that you even took some time to work on this is very cool.

    Oh -- for the LTspice conversant, is Larry's syntax okay? The OP asked
    about LTspice (which I use, too). He uses ^2 to mean square, doesn't
    that have to be ** in LTspice?

    Mike Rocket J Squirrel Elliott
  7. ldg

    ldg Guest

    I was using LTspice for the sims. I found the ^2 in the help file.

    Actually the filters came from something I did a long while ago in
    another simulator. I think it was called Pspice or P something at any
    rate :). (Where's JIm? I'm in trouble.)

    At any rate, I converted the syntax to LTspice as follows:
    SUBCKT BSF1P IN OUT PARAMS: Ho=1 Foz=1000 Fop=1000 Zetap=0.02
    ..ENDS BSF1P

    I sort of had this working in LTspice for a direct bandstop. It
    worked in the ac analysis, but not in the transient. At that point I
    converted it to the bandpass which is very close and did the
    transformation. At the same time I found the note in the help file
    for the ^ exponentiation,

  8. Oh right. "The Boolean XOR operator, "^" is understood to mean
    exponentiation "**" when used in a Laplace expression."

    Thanks for the update!
  9. So Mike, have you ever consisded basing a Spice engine on evolutionary
    GA, Genetic Algorithms? It might be that for really large circuits they
    might be much faster. People are using it for all sorts of stuff, like
    optimisation, but I don't recall anyone actually trying the make the
    basic engine GA based. It certainly works for our brains pretty
    effectively, well unless your name happens to end in Bush.

    Kevin Aylward
    SuperSpice, a very affordable Mixed-Mode
    Windows Simulator with Schematic Capture,
    Waveform Display, FFT's and Filter Design.

    Understanding, is itself an emotion, i.e. a feeling.
    Emotions or feelings can only be "understood" by
    consciousness. "Understanding" consciousness can
    therefore only be understood by consciousness itself,
    therefore the "hard problem" of consciousness, is
    intrinsically unsolvable.

    Physics is proven incomplete, that is, no
    understanding of the parts of a system can
    explain all aspects of the whole of such system.
  10. Mike Deblis

    Mike Deblis Guest

    Thanks for that - A typical THD analyser is essentially a 6 terminal
    device - floating input for source, floating input for output from
    amplifier, ground and output. The output is typically in % or dB of
    the DUT output. Units normaly have a 400Hz high pass filter and a
    30KHz & 80KHz low-pass (Butterworth 4 pole or similar). You can't just
    subtract the input from the output of the DUT as there may be phase &
    amplitude differences - you need to extract the fundamental of the
    input and notch that from the DUT output. I would hope that this could
    be done but I know little about the subtleties of SPICE in general...

    Whatever, such a device would be of *great* use to audio engineers. If
    the "black box" also supported intermodulation tests (notch both
    signals), all the better ;-) The "Black box" could contain the test
    signal source, thus simplifying the determination of the test
    frequenc(y|ies) and hence the notches required for the output of the
    DUT. Help from the SPICE gurus? Please?
    As suggested elsewhere, I've moved to using .params which make life a
    whole lot easier - so many features, so little time!

    Thanks for a great utility - I like the manual on the Yahoo forum -
    very useful reading when on the train ;-)

  11. Standed Spice itself (e.g. SuperSpice:)) also has direct small signal
    plots of THD and IMD against frequency. This is a ".disto" run. This can
    be a lot easier for doing initial design.

    Kevin Aylward
    SuperSpice, a very affordable Mixed-Mode
    Windows Simulator with Schematic Capture,
    Waveform Display, FFT's and Filter Design.

    Understanding, is itself an emotion, i.e. a feeling.
    Emotions or feelings can only be "understood" by
    consciousness. "Understanding" consciousness can
    therefore only be understood by consciousness itself,
    therefore the "hard problem" of consciousness, is
    intrinsically unsolvable.

    Physics is proven incomplete, that is, no
    understanding of the parts of a system can
    explain all aspects of the whole of such system.
  12. Jim Thompson

    Jim Thompson Guest


    Careful there. We're going to start drafting smart-asses who are here
    on H1B's ;-)

    ...Jim Thompson
  13. You're right -- I overlooked that.
  14. Kevin Aylward wrote:

    That's true, and I've used it in my ISspice days. I seem to recall it
    wasn't very sensitive -- could not read low values of distortion or

    Regardless, a proper THD meter needs to operate during .TRAN runs as
    viewing the distortion residue waveform is, to a designer very useful
    and more informative than just the %THD number.

    A THD v Frequency plot test is useful for production testing -- it's a
    handy QC test that doesn't require a lot of skill to interpret.
  15. Jim,
    Isn't KA a naturalized US citizen that
    retreated back to the UK when he couldn't
    make it as an IC designer in the US? He
    should be able to renounce citizenship to
    avoid the draft.

  16. Jim Thompson

    Jim Thompson Guest

    I'm not sure. I had the impression he was a Brit working for TI in
    the Dallas area.

    But, no matter, can't we just use him for target practice ?:)

    ...Jim Thompson
  17. ldg

    ldg Guest

    As Kevin pointed out, it's a small signal analysis. Since much of thd
    is associated with the nonidentities of large signal swings (crossover
    distortion, distortion near the rails, etc.) it has limited use (also
    pointed out by Kevin).

  18. Jim Thompson

    Jim Thompson Guest

    Distortion analysis in PSpice is done in a .TRAN situation and applies
    accurately to the amplitude used for test.... you must vary amplitude
    to get a full picture of distortion.

    ...Jim Thompson
  19. ldg

    ldg Guest

    In Smartspice .disto is small signal - like ac analysis. I hope we're
    talking about the same statement call.


    ..DISTO DEC 10 1KHZ 100Meghz
    ..DISTO DEC 10 1KHZ 100Meghz 0.9

    (From their very own help file :)

  20. Jim Thompson

    Jim Thompson Guest

    Differences between PSpice and Berkeley SPICE2

    The version of SPICE2 referred to is SPICE2G.6 from the University of
    California at Berkeley.

    PSpice runs any circuit that SPICE2 can run, with these exceptions:

    1. Circuits that use .DISTO (small-signal distortion) analysis. U.C.
    Berkeley SPICE supports the .DISTO analysis, but contains errors.

    Also, the special distortion output variables (e.g., HD2 and DIM3) are
    not available.

    Instead of the .DISTO analysis, we recommend running a transient
    analysis and looking at the output spectrum using the Fourier
    transform mode in Probe. This technique shows the distortion
    (spectral) products for both small-signal and large-signal distortion.


    You also get a list of percent THD and percent distortion versus
    harmonic number in the .OUT file. (You need to ask for it in the
    ..TRAN setup boxes.)

    ...Jim Thompson
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day