1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

How to use FFT in LTspice

Discussion in 'Circuit Simulation & PCB Design' started by carbonzit, Jul 29, 2011.

  1. carbonzit

    carbonzit Active Member

    Joined:
    Mar 19, 2011
    Messages:
    1,958
    Likes:
    13
    OK, so over in this other thread they're picking my poor li'l 3-transistor headphone amplifier apart, doing all kinds of FFT stuff with it and all. So I'd like to be able to do the same, just to be able to keep up there.

    Now, I know about the FFT viewer, but I really don't know how to use it. And, as usual, the online help is of absolutely no help. What they say is this:

    Followed by a screenshot of the FFT dialog. WTF?????

    So I know how to select the trace to perform the FFT analysis on. That's easy.

    I know I can select the "number of data point samples in time", although I really don't know what this does, but I assume that more is better (but may also take more time to simulate).

    "Binomial Smoothing done before FFT and windowing/Number of points"? Right over my head. (Default is 3.)

    Now we come to the worst part: the Windowing Function. Not only do I have no frigging idea what this is, but I now have a dizzying array of choices:

    • Bartlett
    • Bartlett-Hann
    • Blackman
    • Blackmann-Harris
    • Blackman-Nuttall
    • Bohman
    • Cosine
    • etc., etc., etc.

    Are any of these guys available on-line to ask them how to use their algorithm? (Just kidding.)

    So I would very much appreciate it if anyone could point me to a set of choices here that works and will allow me to see the distribution of harmonics in my little amp's output. (Or to a decent explanation of how to use this function.)

    I guess LTspice is like a lot of similar software packages: extremely powerful, widely used, and very poorly documented.
     
  2. Sceadwian

    Sceadwian Banned

    Joined:
    Oct 27, 2006
    Messages:
    14,047
    Likes:
    141
    Location:
    Rochester, US
    Actually in a way they are carbonzit, this link should answer all your questions about the window function.

    For what you want I don't think there's much difference but I've never looked at the real differences between the various functions available, I've always visually been pleased with the Blackmann-Harris. The number of sample points is hyper critical to the resolution you get, the bigger the number the more resolution you'll get but the longer it takes to render RF simulations can take a LONG time to run due to the high frequency, your audio circuit should be fine even with high sampling relative to the frequencies you're interested in. Binomal smoothing is in the case of the default takes three samples and applies an algorithm to smooth their values out a bit, helps avoid complex noise issues that can arise from math errors in simulations, try before and after to see if it helps with a rendering that makes more sense.

    In the case of the FFT function in LTSpice being poorly documented, it's not really, anyone that knows what FFT functions are and how they work will understand all the options that are presented. I mean the LTSpice doesn't have help information on the function of a diode, or the details of transistor operation, nor are things like voltage resistance and current explained for obvious reasons, the help file would basically be every Wikipedia entry on electronics =)

    I found a good site a week ago that had a good explanation of how the FFT algoryhtm works and how the sample/window size effects time/frequency resolution but unfortunately I've lost it.
     
  3. crutschow

    crutschow Well-Known Member Most Helpful Member

    Joined:
    Mar 14, 2008
    Messages:
    10,591
    Likes:
    477
    Location:
    L.A., USA Zulu -8
    As Sceadwian noted, the LTspice documentation is designed to help you use the functions available, not to give a tutorial about the functions. FFT's are a big subject all by themselves and if you don't know "WTF" the options are, then some appropriate searching and reading about the subject is in order. Don't expect LTspice to do that for you, especially since it's a free program. It's a poor carpenter who blames his tools.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. carbonzit

    carbonzit Active Member

    Joined:
    Mar 19, 2011
    Messages:
    1,958
    Likes:
    13

    OK, so let's say a guy has gotten a FFT plot, like this one:

    [​IMG]

    (per Sceadwian's preference, I chose Blackman-Harris) So now what do I do with it?

    I can clearly see the harmonic spikes, but what I'm after, in this case, is a figure of merit (it would be nice if I could compute THD, or something close to it). Are there any tools that let me do this? Something that will concentrate on the frequencies of interest (1KHz and its multiples) and compute their relative magnitudes?

    Alternatively, I guess I could use the window's cursors to look at the first n harmonics and hand-compute THD (or something close to it).
     
  6. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,174
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
  7. carbonzit

    carbonzit Active Member

    Joined:
    Mar 19, 2011
    Messages:
    1,958
    Likes:
    13
    Thanks (although I'm not sure how helpful someone else's Google search is: I'm perfectly capable of typing "analyzing fft plots" into a search field myself, you know). I think I know enough about FFT analysis to proceed. It's not lack of knowledge of FFT that's holding me back here: it's the specific question I asked (which you didn't answer), which is how do I derive a value for THD from the plot shown above? Which at this point is really more of a LTspice question than a FFT theory question. I know what the plot represents; it's basically a histogram showing the frequency distribution of the output signal of the amplfiier. I need to somehow convert that to a single figure of merit (total harmonic distortion).

    Now, I'm not necessarily expecting there to be some ready-made, click-and-you're-done solution for this in LTspice (though it would be nice to know if one exists). But since I'm ignorant of so much of its operation, I ask here in hopes that someone, like yourself, who's much more knowledgable about this, might guide me. It may be that one simply has to sit down with paper, pencil and calculator and go through all those harmonic peaks to compute THD.

    In any case, I'd like to know how this is done.
     
    Last edited: Jul 30, 2011
  8. audioguru

    audioguru Well-Known Member Most Helpful Member

    Joined:
    Mar 16, 2004
    Messages:
    32,430
    Likes:
    933
    Location:
    Canada, of course!
    The fundamental should have a level of 0dB but yours is 30dB lower.
    The 2nd harmonic is a little more than 40dB (1% distortion) less than the fundamental so is about 3% distortion and the 3rd harmonic is maybe at 6% distortion.
    I would guess the total distortion is 15% which is horrible.
     
  9. carbonzit

    carbonzit Active Member

    Joined:
    Mar 19, 2011
    Messages:
    1,958
    Likes:
    13
    I don't want your "guess". I want the actual number.
     
  10. audioguru

    audioguru Well-Known Member Most Helpful Member

    Joined:
    Mar 16, 2004
    Messages:
    32,430
    Likes:
    933
    Location:
    Canada, of course!
    The actual distortion percentage is the amplitude of all the harmonics added together divided by the amplitude of the fundamental.
    The number doesn't matter since 1% distortion sounds awful to most people and most hi-fi's have distortion less than 0.05%.
     
  11. carbonzit

    carbonzit Active Member

    Joined:
    Mar 19, 2011
    Messages:
    1,958
    Likes:
    13
    Thanks for explaining something I already know (the meaning of THD).

    Now, if you could tell me how I could derive this number from that LTspice plot shown above, that would be helpful.

    ===========================================================

    I went ahead and searched the web a bit, and what do you know? I found a helpful tool for THD calculation. It's an Excel spreadsheet (attached below) from this page of Fred Nachbaur's tube site.

    So here's what I did:

    1. Measured the magnitudes of all the harmonic peaks (in dB) in the FFT plot above (listed below).
    2. Normalized them to the fundamental by subtracting the magnitude of the fundamental from each.
    3. Plugged the numbers into Fred's spreadsheet. (2nd attached spreadsheet is the one with the results.)

    Result? According to his sheet, my THD is a little over 6%.

    Maybe someone can check my work here (and his spreadsheet, which is pretty simple.)

    Harmonic dB normalized
    ---------------------------------
    1 -28.79 0
    2 -65.61 -36.82
    3 -53.3 -24.51
    4 -91.66 -62.87
    5 -74.36 -45.57
    6 -101.75 -72.96
    7 -95.97 -67.18
    8 -97.52 -68.73
    9 -92.26 -63.47
    10 -97.99 -69.2
    11 -97.74 -68.96
    12 -101.93 -73.14
    13 -97.42 -68.63
    14 -119.38 -90.59
    15 -92.35 -63.56


    Hmm, looks like I can't attach .xls files. Anyhow, you can get the spreadsheet from the site I linked to above if you want it. You can just plug in the numbers above (the right-hand normalized ones) to see my result.
     
    Last edited: Jul 30, 2011
  12. Sceadwian

    Sceadwian Banned

    Joined:
    Oct 27, 2006
    Messages:
    14,047
    Likes:
    141
    Location:
    Rochester, US
    I can't comment on your methods, but they appear solid without analyzing the math, I'll leave that up to people actually qualified. If those are your results though that's pretty sad. THD of 6% at 1khz is.. beyond wretched. I'm almost thinking the first attempts at reproducing audio on wax were better.
     
  13. audioguru

    audioguru Well-Known Member Most Helpful Member

    Joined:
    Mar 16, 2004
    Messages:
    32,430
    Likes:
    933
    Location:
    Canada, of course!
    Yes, it is a terrible extremely simple amplifier with LOTS of distortion.
     
  14. carbonzit

    carbonzit Active Member

    Joined:
    Mar 19, 2011
    Messages:
    1,958
    Likes:
    13
    Typical of you, AG: you comment on some side issue. What I'm trying to resolve here is how to computer THD, not whether some particular amplifier sucks or not. (The whole point of the exercise being to attempt to make this amplifier suck less.)

    So to address Sceadwian's comment, do you think this method of computation is valid? at least close? If you look at that spreadsheet, you can see the (simple) math involved. Here are the formulas used:

    1. He computes "power" from each harmonic's dB value: 10 ^ (dB/10)
    2. He adds the powers to get their sum (THD by power)
    3. He computes THD% ("by voltage") as 100 * sqrt (sum of powers)

    Regarding the amplifier: whaddya expect from a a lousy little 3-transistor amplifier? Actually not too bad, considering.

    Would any of us consider buying any amplifier with an advertised THD of 6%? Of course not! That's not the point.

    Regarding the distortion level of old acoustic recordings (wax and shellac), my guess, having listened to a number of them, is that distortion in them was way higher than 10 or 20%. Lots of clipping, ringing, etc. Yet they were considered a major miracle when they were first heard.
     
    Last edited: Jul 31, 2011
  15. audioguru

    audioguru Well-Known Member Most Helpful Member

    Joined:
    Mar 16, 2004
    Messages:
    32,430
    Likes:
    933
    Location:
    Canada, of course!
    I have never made an extremely simple amplifier that has lots of distortion (all of mine had very low distortion) but I have heard many that other people have bought or made.
    Excellent and inexpensive IC amplifiers have been available for about 35 years so why not use one?
     
  16. alec_t

    alec_t Well-Known Member Most Helpful Member

    Joined:
    Jul 10, 2011
    Messages:
    9,242
    Likes:
    1,218
    Location:
    Cardiff, Wales
    But I bet many amps with 6% distortion are sold (without, of course, having that feature advertised)!
     
  17. carbonzit

    carbonzit Active Member

    Joined:
    Mar 19, 2011
    Messages:
    1,958
    Likes:
    13
    Dang, think about those horrible distortion-generators known as "cell phones". The other day I heard someone carrying on a conversation where I could hear both sides quite loudly, if not clearly. The phone sounded like nothing so much as what we used to call "squawk boxes". And people put up with that crap!

    (Of course, that was some fancy-schmancy class D amplifier, not a simple, humble "analog" one made from antique 3-legged devices ...)
     
    Last edited: Aug 1, 2011
  18. alec_t

    alec_t Well-Known Member Most Helpful Member

    Joined:
    Jul 10, 2011
    Messages:
    9,242
    Likes:
    1,218
    Location:
    Cardiff, Wales
    I draw your attention to
    http://en.wikipedia.org/wiki/Total_harmonic_distortion
    It looks as though manufacturers of audio equipment can cherry-pick their method of measuring THD to give the most favourable (lowest) figure!
    Does anyone out there know if the 'dB' in LTSpice's FFT plot is voltage dB or power dB? It makes a significant difference to your THD calculation!
     
    Last edited: Aug 1, 2011
  19. audioguru

    audioguru Well-Known Member Most Helpful Member

    Joined:
    Mar 16, 2004
    Messages:
    32,430
    Likes:
    933
    Location:
    Canada, of course!
    LTspice shows output voltage, not output power. To calculate the total distortion you must add the levels of each harmonic.
    If there is no distortion then there are no harmonics.

    Speakers and amplifiers have their power ratings exaggerated by stating impossible Peak Music Power or Maximum power.
    Some amplifiers have their output power rating calculated by the age of the designer's grandmother squared.
     
  20. alec_t

    alec_t Well-Known Member Most Helpful Member

    Joined:
    Jul 10, 2011
    Messages:
    9,242
    Likes:
    1,218
    Location:
    Cardiff, Wales
    Since the number of harmonics tends towards infinity (albeit in theory and they will be of pretty low amplitude by the time you get to 100 or so), is it practical to include all harmonics in a THD calculation? I bet the commercial pragmatic approach is 'ignore anything beyond the nth harmonic', where n is an arbitrary low number!
     
  21. alec_t

    alec_t Well-Known Member Most Helpful Member

    Joined:
    Jul 10, 2011
    Messages:
    9,242
    Likes:
    1,218
    Location:
    Cardiff, Wales
    @carbonzit
    I've just found another thread discussing the .four directive, which lets LTSpice calculate THD for you!
    Check out thread 'FFT in LTSpice' posted 4 june 2011, as well as the Spice Help page about his directive.
     

Share This Page