If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

Finding Fourier coefficients for square wave

Finding Fourier coefficients for a square wave. Created by Sal Khan.

Want to join the conversation?

  • female robot grace style avatar for user Anna
    Wouldn't a square wave not be a function since every T/2 (where T is the period) it is perfectly vertical which violates the VLT or in other words you have all y's from the top to the bottom of the wave for those x values?
    (5 votes)
    Default Khan Academy avatar avatar for user
    • spunky sam orange style avatar for user Willy McAllister
      This is a really good question. It is a good example of our quest for beautiful tidy mathematical rigor bumping up against something that is super useful in real life, in this case, the highly non-linear square wave. We need to exercise some flexibility in order to resolve the conflict.

      Wolfram lists three ways to analytically represent a square wave using functions. (http://mathworld.wolfram.com/SquareWave.html). Another way to compose a square wave is as an infinite sum of sine waves (as in this Fourier Series video sequence). Each individual sine wave component is a proper function that passes the Vertical Line Test. Until the limit gets all the way to infinity, there is always a slope to the function as it passes through its transition.

      Another similar place this puzzle arrises in signal processing where we have the concept of the Dirac Delta Function (https://en.wikipedia.org/wiki/Dirac_delta_function). Again, there are very sudden changes in value that you have to hold in your head. The way I accept these ideas is to understand them as (and believe them to be) the limits of more reasonable functions.

      Also check out the value of the sinc function, sin (x) / x, when x = 0. Another mind bender.
      (5 votes)
  • aqualine ultimate style avatar for user Kira Haller
    At , Sal said there will be no b sub 4 term. Why is that?
    (3 votes)
    Default Khan Academy avatar avatar for user
  • duskpin tree style avatar for user Claudia Chen
    hold on, in the middle of the video, Khan replaces f(t) in the integral with 3. Isn't that not possible to do in calculus? I think you have to do integration by parts
    (3 votes)
    Default Khan Academy avatar avatar for user
    • mr pink green style avatar for user Sean
      Look at the function f(t) in the graph between 0 and pi. What is the function doing there? It's a straight line at y = f(t) = 3. So therefore f(t) = 3 between 0 and pi. So f(t) can be replaced by 3 in that integral because the function is 3.
      Hope I helped
      (4 votes)
  • blobby green style avatar for user Aakash Chawda
    When he introduces the n/n trick to make the integration of cos(nt) and sin(nt) easier, it threw me off. Is there a lesson on here where he explains that some more?
    (1 vote)
    Default Khan Academy avatar avatar for user
    • spunky sam orange style avatar for user Willy McAllister
      Just before Sal is getting ready to evaluate an integral of sin(nt). The "n" inside the sine makes this a little tricky. Sal often thinks about an integral as an anti-derivative. If he knows how to take a derivative of something, then its easy to run the process in the opposite direction to get the integral. He's better at remembering his derivatives. In this example he considers the derivative of a cosine function because he knows it always ends up as some sort of sine function. The derivative of cosine is -sine. Then he slips an "n" into the cosine...

      At Sal thinks aloud, "We know the derivative of cos(nt) = -n sin(nt)

      This gives him an expression that includes sin(nt), the term inside his integral. But his antiderivative equation said aloud at also has an "n" term that's not currently part of the integral he wants to solve. He can run this equation in the opposite direction (take the integral of sin(nt) IF he can fiddle around and somehow include the leading "n" term as well.

      So he just sticks in an "-n" term into the integral because he feels like it, and then includes a -1/n on the outside of the integral to repair the "damage".

      Once he has -n sin(nt) inside the integral he runs the antiderivative equation backwards at to solve the integral, resulting in cos(nt).

      I don't think this is a standard calculus method for doing integrals, but it clearly in Sal's bag of tricks. It's an interesting perspective on how to attack integrals.
      (4 votes)
  • hopper cool style avatar for user Ethan La Chapelle
    does the curly S he draws at mean interval?
    (2 votes)
    Default Khan Academy avatar avatar for user
  • starky sapling style avatar for user Yakov Garfinkel
    if my f(x) = pi+x , and its between (-pi,pi) how does that change things?
    (2 votes)
    Default Khan Academy avatar avatar for user
  • blobby green style avatar for user Ery Ferdian Sanusi
    At , Sal said our function is equal to three. Why is that?
    (1 vote)
    Default Khan Academy avatar avatar for user
    • spunky sam orange style avatar for user Willy McAllister
      3V was an arbitrary choice Sal made for the amplitude of the square wave used in this example. The answer (the fourier series of a square wave) includes a term based on the amplitude of the given square wave. Sal gave the amplitude a concrete value, so you can see how it travels through the computation.
      (2 votes)
  • purple pi teal style avatar for user Anshuman Nayak
    Why there isn't a b sub zero term?
    (1 vote)
    Default Khan Academy avatar avatar for user
  • blobby green style avatar for user 22njiang
    If I'm applying Fourier analysis and Fourier transform to comparing the sound waves of a piano and a cello, how would i go about this?
    (1 vote)
    Default Khan Academy avatar avatar for user
    • old spice man green style avatar for user Willy McAllister
      Step 1: Make a recording of each instrument in digital form. For example, record a single note (A440 or middle-C for example) for 1 second with a sample rate of 20,000 samples/second.

      Step 2: Perform Fourier transforms on each tone file on a computer to extract the frequency content of each tone. The computer algorithm for Fourier transforms is called an FFT (Fast Fourier Transform). Programs like Matlab or Octave (free) have an FFT module.

      Step 3: Overlay the two results in different color. The highest peak in both will be at the primary tone, A440 or middle-C. You will see smaller peaks (overtones) at octaves above the fundamental tone. And you will also see even smaller peaks at other frequencies due to non-linearities in the two instruments.

      The overtones and non-linear sounds are how your ear can tell the same note on a piano apart from a cello.
      (2 votes)
  • blobby green style avatar for user Brian Morrow
    I'm applying the Fourier Transform to a wire mesh with a separation between wires at a different interval than the actual width of the wire. How would I be able to write this in terms of the Fourier series with the maximum and minimums at different intervals from each other? Or can we make the assumption that they are the same.

    The exact difference is the wires have difference of ~92 microns, the separation between wires is ~120 microns.

    I also have another question about the video specifically, around the time, you pulled 'n' out from the integral and put in the denominator. Why?
    (1 vote)
    Default Khan Academy avatar avatar for user
    • spunky sam orange style avatar for user Willy McAllister
      If your wire mesh was a time-based signal, it would look like a pulse train where the pulse height was less than half the period. Look for guidance on the web: the Fourier transform of a pulse train where the "duty cycle" is not 50%.

      Just before Sal pulls a trick where he introduces a factor of n/n, the top n is inside the integral and the 1/n is in front of the integral. The reason for this is is to get an easier evaluation of the antiderivative of the term n cos(nt) compared to evaluating the antiderivative of cos(nt).
      (2 votes)

Video transcript

- [Voiceover] So this could very well be an exciting video because we start with this idea of a Fourier series that we could take a periodic function and represent it as an infinite sum of weighted cosines and sines and we use that idea to say, "Well can we find formulas "for those coefficients?" And we were able to do that using the powers of calculus. And now we can actually apply it for this particular square wave. And I picked a square wave that has a period of two pi and that's where, actually, a lot of these two pis came out from, and that's also why we started here at cosine t and sine of t. They both have a frequency of one over two pi, which is the frequency of our original square wave. And then the other terms have frequencies that are multiples of that. If we had a different period then all of that would change. But I picked this period to just make the math a little bit simpler and we will generalize in the future. But now, let's actually evaluate a-sub-zero, a-sub-n, and b-sub-n for this particular square wave. So, they key to realize is that our square wave between zero and pi, 'cause we're gonna keep taking the definite integral from zero to two pi, from zero to two pi, from zero to two pi. The key realization here is from zero to pi our function is equal to three, and from pi to two pi, our function is equal to zero. So all of these definite integrals. So this definite integral, for example, is going to be the same thing as, and I'll do it once and then we'll see that trend. This is the same thing as one over two pi times the definite integral from zero to pi of f-of-t dt plus the integral from pi to two pi of f-of-t dt. Now f-of-t, between zero and pi, we just said it's equal to three. And f-of-t between pi and two pi, well it's going to be equal to zero. And that's actually the case for these other situations. That f-of-t's gonna be zero from pi to two pi and zero times anything is gonna be zero, so the integrals, the definite integrals over this, the second part of the interval, are always going to be zero. And so it boils down to this. And so what's the definite integral from zero to pi of three dt? Well this is going to be equal to one over two pi times, so if you evaluate this, the anti-derivative of three, it'd be three t, evaluated from zero to pi, and so it'd be three pi minus three times zero which is just three pi. So it's equal to three pi over the two pi that we had already, over the two pi, and so this is going to be equal to three halves. Three halves. And that makes a lot of sense because a-sub-zero we said you could view that as the average value of the function over that interval. And the average value of that function is indeed, if it's three half the time, and zero the other half of the time, well then the average is going to be one and a half, or three halves. So that is our a-sub-zero. So now let's figure out the general a-sub-n where n is not equal zero. So, a-sub-n, well we are just gonna take, it's gonna be one over pi, one over pi, the definite integral, I could go from zero to two pi, but instead I'm just gonna cut to the chase. I'm gonna go just from zero to pi 'cause the integral from pi to two pi is just gonna be zero 'cause the function's equal to zero. It's gonna be zero times cosine nt; it's just gonna be zero. So for this particular square wave, I can just worry about from zero to pi. From zero to pi, f-of-t is three. So it's gonna be three cosine-nt. Three, let me do that color, three cosine-of-nt, cosine-nt dt. Now what is this going to be equal to? Well we can do a few things. We could take our three and bring it out front. So let me do that. So we could take that three and bring it out front. So, just putting the three out here. So take that three, put it out front. And we know that the derivative of sine-of-nt is n cosine-of-nt. So let's multiple the inside and the out, or let's multiple the inside by n, and then we could also divide by n but we could put that outside. They would, it'd be, n divided by n, we haven't changed the value. And so this is going to be equal to three over n pi, that's just what we had out here, times, well the anti-derivative of this business is going to be sine-of-nt, sine-of-nt. And we're gonna evaluate from zero to pi. So this is going to be equal to three over n pi times sine-of-n pi, well that's gonna be zero for any n, minus sine-of-zero times n. Well that's gonna be zero for any n. So all of this is just going to be zero. So a-sub-n is going to be equal to zero. The coefficient on any, for any, of a-sub-ns for any n not equal zero, it's going to be zero. So actually we're not gonna have any of these cosines show up in the Fourier expansion. Let's think about our b-sub-n. When you look at the shape of the square wave, it actually makes a lot of sense. But let's now tackle our b-sub-ns. So our b-sub-ns, get a little space here. So b-sub-n. Same thing, we could just worry about the interval from zero to pi because from pi to two pi our f-of-t is going to be equal to zero. So this is going to be equal to one over pi times the definite integral, once again I'm only gonna worry about from zero to pi, zero to pi dt. Now the value of the function from zero to pi is three, we've seen that before, I could put it here, but just to get a little bit simpler, let's just stick it right over here. Just gonna be three, actually I don't wanna skip too many steps. It's gonna be three times sine-of-nt. Three sine-of-nt. Now let's see. How can I reconstruct this so it's easy to take the anti-derivative? Well this is going to be, we take that three out front, it's gonna be three over pi. And then you have your definite integral from zero to pi of sine-of, we'll do that same color, sine-of-nt. Sine-of-nt dt. And we know the derivative of cosine-nt is negative n sine-of-nt, so let's throw a negative n in here. So negative n. Let's also divide by negative n. Just like that. If you have negative n divided by negative n, we haven't changed the value. That's just one. And so this is going to be equal to negative three over n pi times, we're going to take the anti-derivative here, so this is gonna be cosine-of-nt evaluated from zero zero to pi. And so, what is this? This is going to be equal to, this is equal to negative three over n pi. Cosine-of-zero n, that's gonna be one for any n, minus cosine-of Oh sorry. Why did I start at zero? It's gonna be cosine-of-n pi, cosine-of-n pi minus cosine-of-n times zero. So minus cosine-of-zero, cosine-of-zero is just gonna be one for any n. And so there you have it. We have a general And you might say, "Well, cosine-of-n pi, "is that positive one, or is that negative one?" Well, it depends. If n is, if n is even, this is going to be positive one. If n is odd, this is going to be negative one. So it depends. And actually let's just write that out. So b-sub-n, b-sub-n is equal to, let's write those two cases. So if n is if n is even, and, another one, if n is odd. So if n is even, you're gonna have negative three over n pi times Actually let me just do it over here so we have the space. So what would this thing evaluate to if n is even? So this n, if it's even it'd be like cosine-of-two pi, cosine-of-four pi, cosine-of-six pi. Well in that situation, this is going to evaluate to one. You're gonna have one minus one, which is zero, so the whole thing is gonna evaluate to zero. And if n is odd, cosine-of-pi, cosine-of-three pi, cosine-of-five pi, well those are gonna evaluate to negative one. Then it's gonna be negative one if n is odd. Negative one minus one is negative two, and so this is all gonna be negative two. Negative two times negative three over n pi is going to be six. Six over n pi. So there you have it. We have been able to figure out our Fourier expansion. It is going to be, our square wave, and we definitely deserve a drumroll, this is many videos in the making, f-of-t is going to be equal to a-sub-zero, we figured out in this video is equal to three halves. So it's gonna be three halves. Let me write this. Well I'm just write it all in yellow. So three halves. Now we don't have any a-sub-ns. We figured that out. All of the a-sub-ns are going to be zero. And then the b, the only b-sub-ns we have is when n is odd. When n is odd. So, we're gonna have b-sub-one. So that's gonna be six over one pi. Six over one pi. Sine-of-one times t. So sine-of-t. Plus, now we're not gonna have a b-sub-two, we're gonna have a b-sub-three. So six over three pi times sine-of-three t, and of course this is the same thing as, this is the same thing as two over pi times sine of three t, plus, now we're not gonna have a b-sub-four, we're gonna have a b-sub-five. So that's going to be six over five pi. Actually I liked writing, it's nicer to actually not simplify here because you can see the pattern. So it's gonna be plus six over five pi times sine-of-five t. And we're just gonna go on and on and on. And there you have it. We have our Fourier expansion. In the next video, we're actually going to visualize this.