Square wave harmonic content - Page 1
Forum section: electronics / projects

author: taydin
author_rank: Frequent Contributor
timestamp: September 28, 2019, 01:17:20 pm
content: Somebody asked me a question about harmanic content of a square wave, and even though I have been dealing with FFT and such for quite some time, I was kinda baffled by it. The question basically is this:For some signal sources, it makes quite sense that there is multiple harmonic content, because that is how the signal is produced. For example, if you pick a guitar string, that string vibrates at multiple sinusoidal frequencies, so it is quite normal that we see that harmonic content when we do an FFT on the sound.But let's say we are switching current through a resistor on and off using a MOSFET. In this case, we obtain a squrewave, but there is nothing that ACTUALLY produces sinewaves. So where does the harmonic content of this squarewave that we see in the FFT come from?

author: taydin
author_rank: Frequent Contributor
timestamp: September 28, 2019, 01:22:07 pm
content: I thought about this for a while and the only explanation I can come up with is this:In the resistor switching case, the switching speed, or the RISE TIME of the current through the resistor is the important factor here. In order for the circuit to allow that rise time to happen, it needs to be designed such that it can also pass through a sine wave with the same rise time. So, we basically take the FFT to find out what frequency sinewave the circuit has to pass through without attenuating it too much. And this frequency allows us to determine the rise time that the same circuit will allow. So a high rise time requires that the circuit passes a lot of the higher order harmonics of the square wave.So in this case, even though the FFT spectrum tells us that there is the 3rd harmonic, 5th harmonic etc, those are just mathematical representations, just like we use complex numbers to model RLC circuit behavior. The FFT basically looks at the various dV/dt points (or rate of instantaneous change, which is the derivative at that point) in the waveform and tells us what frequency they correspond to.I wanted to have this discussion here to get the opinions of the experts. Tried to do a search, but for some reason, only one page of search result is returned, and I didn't see this discussed in that one page.

author: drussell
author_rank: Super Contributor
timestamp: September 28, 2019, 01:24:31 pm
content: Just like an infinitely fast single pulse, a perfect (theoretical) square wave would have an infinite series of harmonics ateveryfrequency.It is impossible to create a perfect pulse or square wave, though.

author: RoGeorge
author_rank: Super Contributor
timestamp: September 28, 2019, 01:28:23 pm
content: Quote from: taydin on September 28, 2019, 01:17:20 pmThere is abidirectionalmathematical equivalence between a time domain representation and a spectral representation of the same signal. Search about Fourier for more details.You can either say/think about the square wave is a square wave, or you can say/think as well it's a sum of sinusoidal waves of certain frequencies and amplitudes. It doesn't make any difference at all.The resistor have nothing to do with that, and those harmonics do really exist there, same as 2 apples and 3 apples also exist in a basket with 5 apples.There is nothing virtual there, Fourier it's for real.Many, many moons later (2021 edit):Changed my mind. Now I think those sinusoids are not physically there.

author: drussell
author_rank: Super Contributor
timestamp: September 28, 2019, 01:32:43 pm
content: Quote from: RoGeorge on September 28, 2019, 01:28:23 pmWell, except that the sum of the sinusoids is only ever a closeapproximationof the square wave.Perfectly usable for real-world applications, though, of course!

author: Yansi
author_rank: Super Contributor
timestamp: September 28, 2019, 01:33:05 pm
content: Quote from: drussell on September 28, 2019, 01:24:31 pm... at everyoddharmonic frequency.

author: taydin
author_rank: Frequent Contributor
timestamp: September 28, 2019, 01:37:12 pm
content: Quote from: RoGeorge on September 28, 2019, 01:28:23 pmThat is the theory and I understand that part. But I want to understand the actual physics behind it.Here is one thought experiment. Let's say I designed the resistor, MOSFET circuit so that only the third and fifth harmonic are present and everything else is attenuated. I can easily do that by chosing an appropriate switching frequency. In this case, the square wave must look like this:But I won't. The signal will have a slow rise time, and the top of the square wave will be more or less flat. But in the above graph, we see the sinusoidal artifacts.

author: taydin
author_rank: Frequent Contributor
timestamp: September 28, 2019, 01:43:36 pm
content: And here is another thought experiment. A DC signal has a spectrum where EVERY frequency is present at a fixed energy.So if I take my 1.5V AA battery, can I use it to get 100 MHz, if I put a band pass filter after it?Removed, wrong information.

author: RoGeorge
author_rank: Super Contributor
timestamp: September 28, 2019, 01:52:12 pm
content: Quote from: taydin on September 28, 2019, 01:37:12 pmNo, that's no theory, that's reality, that's how the real world physics works. We prefer to think about signals in time domain, but that is just a human bias. There is absolutely no way, either imaginary or experimentally, to separate the time domain representation from the spectral representation. No way whatsoever. They ARE the same. You will need some time to let this idea sink, but if you want to consider it, it will be very rewarding.About softening the edges of a square wave, that is because the resistor together with the gate parasitic capacitance form an RC low pass filter. That will lower the amplitude of high frequency spectral components while it will not alter much the low spectral components. In time domain representation, that ratio change between lower frequency components vs higher frequency components is seen as a less "sharp" edges and some ripple in the square wave.Many, many moons later (2021 edit):Changed my mind. Now I think those sinusoids are not physically there.

author: taydin
author_rank: Frequent Contributor
timestamp: September 28, 2019, 01:56:45 pm
content: Quote from: RoGeorge on September 28, 2019, 01:52:12 pmThe time domain vs frequency domain duality works for practical calculation, and allows us to make progress in our designs. I'm not questioning that. Same as complex numbers make RLC circuit analysis so much easier. But just like there is no physical thing that can describe the square root of -1, maybe the FFT in the resistor/MOSFET case is just a mathematical method of getting the results we want.How do you image the sine waves are coming from when you think about the resistor/MOSFET switching?

author: RoGeorge
author_rank: Super Contributor
timestamp: September 28, 2019, 02:23:59 pm
content: Exactly how you imagine a square wave is coming, but instead of thinking about a square shape, you can think about it as a sum of sinusoidal voltages.As a side note, FFT is an algorithm, a method to roughly calculate the Fourier Transform (FT) as fast as you can.- FFT is a computer algorithm that takes a limited number of samples and produces some "buckets" of spectrum, buckets that may, or may not, correspond exactly to the analog signal's spectrum.- FT is an infinite series of sinusoidal functions, a fundamental concept in both the mathematical and the physical world, and a perfect representationMany, many moons later (2021 edit):Changed my mind. Now I think those sinusoids are not physically there.

author: SiliconWizard
author_rank: Super Contributor
timestamp: September 28, 2019, 02:38:54 pm
content: This is kinda basic maths. Learn about Fourier analysis to understand this.FFT is an algorithm for fast discrete Fourier transforms. You need to understand what Fourier transforms do and what they are used for. Knowing how to use an FFT will not teach you what it's useful for.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: September 28, 2019, 02:56:03 pm
content: You're perhaps confused about what a transform is:Quote from: taydin on September 28, 2019, 01:17:20 pm"Produces" implies a causal relationship, which in turn implies some sequence of events through time. This is inapplicable -- the Fourier transform applies toall time.It is better to say, if we suppose this signal is repeating for all time, then we canequivalently represent itas a sum of sinusoids that repeat for all time. That's the Fourier series.Or if it's a nonperiodic signal, we can take the transform of its energy -- because a signal that's zero for almost all time has zero average power, so it has no Fourier series, but it can still have a transform.That's probably confusing, too -- let me address that. You get a series -- harmonics -- from transforming a repetitive (periodic) signal. A signal you can describe with a single finite window, that repeats endlessly backwards and forwards in time. A non-periodic signal, like a one-time ringdown waveform for example, is zero for all negative time and practically zero for practically all positive time; it's not periodic so it has no harmonics and it has no power averaged over infinite time. When we transform such a signal, we look at its energy as a function of frequency. Power is the rate of energy over time; we only get average power if there's energy being delivered constantly. If not, we are looking at its energy instead.There are other transforms that take both time and frequency into account -- they use a kernel* that has finite time and frequency extents, and so it becomes meaningful to speak of them in causal terms. These are much more complicated than a beginner needs to work with, though -- understandable as you're working with two variables at once!*A kernel is a function which represents the simplest form of a transform; for the Fourier transform, it's the sine and cosine functions. For a wavelet transform, it might be a Gaussian (exp(-x^2) function), or that times sine and cosine (which makes a tone-burst waveform). The transform is usually calculated by a sum or integral of the given signal times the kernel, i.e., a convolution of them.Convolution is perhaps not so hard to understand, as it's the time-domain equivalent to multiplying frequency responses together -- that is, applying a filter to a signal. The response you see on the oscilloscope, for a given signal and filter, is the convolution of the signal with the filter's response. The Fourier transform is essentially the waveform you get by filtering the signal through infinite channels of infinitesimally narrow filters (note that a sine wave has zero bandwidth!). If you think in terms of a finite number of filters with finite bandwidths, then you get a wavelet transform. Waterfall spectrograms, in audio and radio, are a common application of this.Tim

author: taydin
author_rank: Frequent Contributor
timestamp: September 28, 2019, 04:47:13 pm
content: The question isn't "what is Fourier transform and its mathematical foundation". The question is, if I switch a resistor with a MOSFET, how do the sinusoidals form? At the electron level, at the level of the magnetic fields or electric fields.If I have three signal generators that generate 1 MHz, 2 MHz, and 3 MHz, and then I add these signals together, and then feed the results into a spectrum analyzer, I fully expect to see the three peaks in the spectrum, because those sinusoidals are there.But if I switch a resistor using a MOSFET, there are NO SINUSOIDALS anywhere. So my points is, they aren't really there. What you are looking at is the effect of the square wave rise time. If the rise time is high, you will see much more harmonics spanning to higher frequencies. If the rise time is low, you will only see a limited number of harmonics.

author: taydin
author_rank: Frequent Contributor
timestamp: September 28, 2019, 04:58:45 pm
content: Or if I put it in another way, the rising edge, or the falling edge (or in fact, any dV/dt change that occurs in a signal) has energy change at a rate that can only be caused by a specific sine wave frequency. The fourier transform lets you know what those frequencies are. It's a mathematical tool.The fact that you can represent a square wave by an infinite number of sine waves isn't proof that those sine waves are there. If they are there, there must be a physical mechanism (or quantum mechanism) causing them.

author: tggzzz
author_rank: Super Contributor
timestamp: September 28, 2019, 05:08:21 pm
content: Quote from: taydin on September 28, 2019, 04:47:13 pmYes, they are there, and (to use your terminolgy) have already been added together.Band limit that square wave and put it into a spectrum analyser (just like you did for your added sinewaves), and you will see the three peaks (and more if you don't band limit the signal).QuoteYour language makes it appear you think the risetimecausessine waves or harmonics. It doesn't.Square waves can be decomposed into sets of sinewaves.Sets of sine waves can be composed into squarewaves.The time domain and frequency domain are equivalent, as defined by the Fourier transform.

author: RoGeorge
author_rank: Super Contributor
timestamp: September 28, 2019, 05:24:51 pm
content: Quote from: taydin on September 28, 2019, 04:47:13 pmFirst, if you want to resemble something like a square wave, then you can have only odd harmonics, syncronized in a certain way and with a certain amplitude relative to each other, but let's say your generators are 1, 3 and 5 MHz, all with just the right amplitude and phase. Once you add them together, and look at the resulting waveform, you won't see any sinusoid, you'll see almost a square wave.So where are your original 1, 3 and 5 sinusoidal waveforms? Did they just disappeared? No, they are all there, in the (almost) square waveform of all 3 added together. You don't believe your original 1, 3 and 5 MHz are still there? Go ahead, make 3 filters at 1, 3 and 5 MHz respectively, and you will see your original signals.Quote from: taydin on September 28, 2019, 04:47:13 pmYes, there are A LOT of sinusoidals in the square wave. You CREATED them, all at once, when you turned your switch on through your resistor. They ARE really there, they are real. You can look at each of those components with an adjustable bandpass filter.If you don't want to consider the composing parts of a more complicated object as real, then what is REAL?Your body is made out of a bunch of various body organs. When you look in a mirror you can "see" only the whole human body (analogy with the square wave), you are not seeing internal organs, but if you look with a tomography machine, you'll observe a bunch of internal organs (the 1, 3, 5 MHz sinus) so when you look in a mirror, are those internal organs REAL, or not?Same with your square waveform. It depends of what type of instruments you use to "look" at your signal. With an oscilloscope, you'll see a square wave, with a spectrum analyzer/VNA you'll see 3 spectral components at 1, 3 and 5 MHz.Why the difference between the 2 instruments? Which instrument is lying to you, the oscilloscope or the spectrum analyzer?The answer is that both instruments are correct.What we call "reality" is just a preferred subset of what is really out there, a subset we get after we applied some sort offilterwhile we wereobserving(or measuring) the real world around us.After all, what do you mean by "REAL", and by "are they (the sinusoids) really there"?Many, many moons later (2021 edit):Changed my mind. Now I think those sinusoids are not physically there.

author: 2N3055
author_rank: Super Contributor
timestamp: September 28, 2019, 05:33:42 pm
content: Quote from: taydin on September 28, 2019, 04:58:45 pmI cannot depict whether you are making fun of us or you are serious...Lets take the picture and put it upside down..If you take squarewave (as perfect as you can make it) at say 1 kHz. Make it very nice, clean and symmetric, both in duty cycle (50%) and amplitude wise.And feed that into a nice analog filter, that will be made to filter out 3 kHz, 5 kHz, 7 kHz. A nice sharp filter with cutoff at 1 kHz.. Make filter at least 24dB/oct. And feed output of that into scope.What you will see? A nice clean 1 kHz sine wave. Where that one came from. It obviously was in there.... We didn't add it mathematically. We subtracted other sine waves from original signal, one by one, and what was left wasfundamentaltone of 1kHz. That thing WAS in there all the time, you just didn't see it from other crap.And as Tim nicely said, actually there is no squarewave or triangle wave or any other wave that is 1 kHz. Only 1 kHz waveform would be insanely clean 1 kHz sine wave. Add phase noise, change waveform, you get a handful of frequencies (ideal sinewaves) combined into a waveform shape that repeats at 1 kcycles per second.

author: fourfathom
author_rank: Super Contributor
timestamp: September 28, 2019, 05:35:39 pm
content: The sine waves are there. Try this: Generate a square wave, say at 1MHz, or 100 KHz. Connect this to a single-sideband radio receiver and tune across the spectrum. You will hear a pure beat-note tone at all the odd harmonics of that square wave. Those are your sine waves.

author: taydin
author_rank: Frequent Contributor
timestamp: September 28, 2019, 05:42:54 pm
content: Quote from: RoGeorge on September 28, 2019, 05:24:51 pmYou misunderstood. What I'm saying is, I have 3 sine wave sources, added together (NOT to form a squarewave, as you seem to think). Then I feed the sum to a SA, and I fully expect to see 3 peaks, because those sinewaves were created by the source.Quote from: RoGeorge on September 28, 2019, 05:24:51 pmSo can we say that the rising edge and the falling edge of the square wave implement a change of energy which resembles one half of multiple sine waves (the first 90 degrees). The bandpass filter will just act on these partial sine waves and produce the necessary output?

author: taydin
author_rank: Frequent Contributor
timestamp: September 28, 2019, 05:46:00 pm
content: Quote from: 2N3055 on September 28, 2019, 05:33:42 pmI don't see what's funny so far, but anyway, I have no desire of making fun of you.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: September 28, 2019, 05:46:38 pm
content: Yes, I see. You areasking the wrong question.There is not even any meaning to the question "whatcausesa sine wave" -- a sine wave is eternal, without beginning nor end!You can no more ask "what causes a sine wave" than you can ask of God, "what created You?"Indeed, since all physical signals are by definition finite, on a sufficiently pedantic level, the Fourier transform (and all other infinite-time transforms) are somewhere between useless (they can never truly represent a real signal) and nonsensical (how can you ever integrate into even the finite future, let alone infinite?).But we nonetheless find them a useful representation, as others have illustrated through example.So what's so wrong with that? Perhaps your mind is too accustomed to causation, to procedure -- procedural computer programming for instance is exclusively causative; it is also nonreciprocal, or irreversible: a transfer of data from one function to another, does not cause a necessary change in the source.It is simply that, other systems have more complicated rules, rules like superposition (a signal can be represented as a sum of any infinite number of possible components, so long as the total ends up the same), reciprocity (you cannot deliver voltage to a load, without also drawing current from the power supply) and so on. Open your mind to these possibilities, and most of all -- work some problems dealing with this very subject. In time you will gain understanding.Tim

author: tggzzz
author_rank: Super Contributor
timestamp: September 28, 2019, 05:50:54 pm
content: Quote from: taydin on September 28, 2019, 05:42:54 pmNo, you can't say that.

author: rstofer
author_rank: Super Contributor
timestamp: September 28, 2019, 05:55:07 pm
content: For giggles, I created two FFT displays with my Analog Discovery 2. Both are 1 kHz, 50% duty cycle with 1V amplitude (2V P-P) but one has a DC offset of 1V and in the FFT there is a DC component. The one without the offset (square wave is symmetric with respect to 0V), there is no DC spike.I rescaled so the two plots don't share a common vertical axis. Mostly because of the magnitude of the DC spike.The important thing to note is the fact that the square wave contains odd harmonics only and they extend from DC to Daylight with diminishing amplitudes proportional to 1/<harmonic number>. As we get higher in harmonics, the amplitude doesn't change as much from one harmonic to the next. Think about the 101st and 103rd harmonic. The amplitude factors are 1/101 and 1/103 0.0099 and 0.0097 - not much change.The 'squarish' waveform in the MOSFET circuit contains a bandwidth limited waveform. It will have odd harmonics out to some point where the low pass filter just attenuates them so much that the waveform loses its square appearance. Think in terms of pushing a 100 MHz squarewave through a 100 MHz scope. All you get is a sine wave with, perhaps, a wee bit of distortion. The 3rd harmonic is well beyond the capability of the scope and the 5th, 7th, and so on, are all essentially zeroed out.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: September 28, 2019, 06:12:01 pm
content: Quote from: taydin on September 28, 2019, 05:42:54 pmIf you insist on operating exclusively with edges, we can still perform the analysis; you're just forcing yourself into a corner which is more difficult to work in -- but which, trust me, is no less mathematically rigorous.To wit: we start with the Heaviside step function u(t). This is defined as zero for t < 0, one for t > 1, and conventionally 0.5 at exactly t = 0, but that doesn't really matter.The Fourier transform of this function is \$\frac{- j}{2 \pi}\frac{1}{\omega}\$. Don't mind the imaginary unit j -- that just sets the phase, and shows that the phase for \$\omega < 0\$ is positive (+90°), and negative (-90°) else. This is a standard consequence of real-valued signals, actually: that the transform is Hermitian, i.e., the imaginary sign flips when the argument (omega) flips. (I think that's right? I always forget which, or if it's that it's an odd function.)Note, by the way: this function has considerable power, indeed infinite energy around zero frequency (again, the rate of energy is power; such a function does indeed exhibit power, though only at DC. Which is to be expected, since, well, it's the purest case of "pulsed DC"!) It has very little energy at high frequencies, and does not contain discrete frequencies, but rather a continuum.Suppose we add another edge, another unit step, time shifted and opposite in phase -- so we get a single rectangular pulse instead. What then?We apply the time-shift identity, which multiplies the transformed function by \$e^{j \omega t}\$ for a time-shift oft.I will leave it as an exercise to the reader to prove, but the result is sinc(omega) = sin(omega) / omega. Give or take phase, since the rect(t) function gives sinc(omega) but rect(t) is centered around zero by convention. But anyway, this is just one of many handy complementary functions we learn about.What if there's an infinite series of steps, thus making a square wave?The oscillation of the sinc function continues to be reinforced by interference; in fact, at infinity, the interference is so strong that the only peaks left are infinitesimal in width (usually written \$\delta(\omega)\$). And there's an infinite series of them, and their amplitudes happen to go as 1/N.We might simplify this and instead write the original function as a sum of sines or cosines, with their amplitudes and phases given by this solution. Then we can dispose of that nasty delta impulse function, and get the Fourier series representation.So, after all this process --In the iterative sense, we have "caused" sine waves to exist, by using an infinite series of equally spaced, complementary edges. Although, say you wrote a program that adds up infinite edges and finally calculates its transform -- it can never finish. So again, it isn't very meaningful to "cause" this to be, if it can never be computed directly, say.What caused the universe to exist, or God (if you are of a religious sort -- take most world religions* for example on this one)? It isn't a meaningful question; they simply exist.*Interestingly, several major ones teach a (finite) beginning to the universe, or to their god(s), putting a more human (finite) touch on things than the western Abrahamic ones do. Perhaps a theological analogy isn't the best after all.Tim

author: ebclr
author_rank: Super Contributor
timestamp: September 28, 2019, 07:10:22 pm
content: This toy helps you play with some conceptshttp://www.lohninger.com/comimg/ex_foursynt_large.gif

author: SiliconWizard
author_rank: Super Contributor
timestamp: September 28, 2019, 07:27:27 pm
content: A "square" wave is a theoretical object, like infinite. When you're switching a transistor, it just generates a series of waves that... "mostly" add up in phase. If the signal travels in some medium other than good conductors, it will be less pretty, and "interferences" can happen. It won't look square then. (And yes, you can just filter a square wave in various ways to convince yourself it's effectively composed of a series of sine waves.)If you're considering "edges", even any pure sine wave has rising and falling "edges". The rise and fall times will just depend on frequency.The typical "rise time" of a sine wave of frequency F is ~ 0.295 / F (note that it's a bit less than the usual ~ 0.35 / BW, for the max rise time for a given bandwidth, due to the fact bandwidths are defined at -3dB ...)(To calculate this, just take a sine wave S(t) = A.sin(2.pi.F.t), take the 10% and 90% points of the P-P amplitude, and infer the time difference from there.)

author: rstofer
author_rank: Super Contributor
timestamp: September 28, 2019, 08:41:59 pm
content: Quote from: T3sl4co1l on September 28, 2019, 06:12:01 pmI think it is 1 for t > 0, not t > 1 -- likely just a typo because you clearly have a handle on it.Maybe the universe is a result of the Heaviside Step Function. For t < 0, nothing existed. For t > 0 we're all here. And I don't want to know what happened at t = 0.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: September 28, 2019, 09:40:47 pm
content: Thanks, yes just a typoTim