Return to Video

W01_L01_P04 - Derivatives with the FFT

  • 0:00 - 0:05
    And then you just have to keep in mind,
    when you use that fft you'd better know
  • 0:05 - 0:09
    that the fft is doing the shifting. And
    you could ask, well, why not, why the, why
  • 0:09 - 0:14
    make it, why not shift it back? Why, why
    make me shift it back?" Cuz a lot of times
  • 0:14 - 0:19
    when you're doing computation, you're not
    gonna necessarily go look at the spectrum
  • 0:19 - 0:24
    all the time. You, the reason it stays
    shifted is because, remember, speed is
  • 0:24 - 0:30
    everything. Speed is everything in terms
    of the computing. You want, if you got, if
  • 0:30 - 0:35
    you got n log n speed, you wanna add you
    know, stuff on top of it. You can do that
  • 0:35 - 0:40
    on your own time. With this computing,
    keep it at n log n, fast. Okay? In my last
  • 0:40 - 0:46
    class, I talked in my last course, 581. I
    always tell people you may get the right
  • 0:46 - 0:52
    answer. But it's wrong if you did it the
    slow way. Okay? Part of computing now is
  • 0:52 - 0:57
    not just about oh, I can compute and I got
    this really, I got this method that can
  • 0:57 - 1:02
    compute it right. It's not about that
    anymore, it's about not only can I compute
  • 1:02 - 1:11
    it right, I can compute it faster than
    you. Okay. It's not that, it's not that I
  • 1:11 - 1:16
    guess you know, competitive. But, but
    actually, you know it kind of is. If
  • 1:16 - 1:21
    you're trying to write a paper in this
    area, that's what you're trying to say
  • 1:21 - 1:25
    actually all the time. It's like oh, I can
    compute this faster than you can. And here
  • 1:25 - 1:30
    is why. Okay? That's, that's how you write
    a paper in scientific computing. You tell
  • 1:30 - 1:34
    people how much awesomer you are. Okay?
    Alright. But, see, the nice thing with
  • 1:34 - 1:38
    your data analysis skills, you might be
    working, I don't know, in atmospheric
  • 1:38 - 1:42
    sciences or geology geologoy or something.
    With your skills you'll be able to say how
  • 1:42 - 1:47
    much better you are also. See, then I'll,
    then we'll all have won. Okay? Alright. So
  • 1:47 - 1:52
    there you go. that's the simple
    implementation. Now I'm gonna use it to do
  • 1:52 - 2:01
    a trick. Okay? Yes? So, the actual
    coefficients are positive and negative
  • 2:01 - 2:04
    alternating. This is. Yeah. This is the
    absolute value. If it's complex you know,
  • 2:05 - 2:09
    most people, by the way, when you get a
    complex spectrum, what you normally do
  • 2:09 - 2:14
    what people call the power spectrum. I
    don't know if you've heard this. Okay?
  • 2:14 - 2:19
    Well that's an aggressive word just like
    the competition. It's all, it's all like
  • 2:19 - 2:24
    smackdown for comp, computing. So the
    power spectrum is always the absolute
  • 2:24 - 2:29
    value of the spectral content, cuz that's
    what you want to know is how much
  • 2:29 - 2:34
    frequency content, not necessarily what
    phase it's in. Okay? Alright. So, there's
  • 2:34 - 2:42
    that. And now, for my next trick, I will
    differentiate a function, computation. Are
  • 2:42 - 2:46
    you ready? I, I don't know if you guys are
    ready. Let's, can I have the lights on
  • 2:46 - 2:53
    real quick, upfront? There's this very
    nice relationship, that's actually worked
  • 2:53 - 3:08
    out in the notes, which says the
    following. It says the following, the hat
  • 3:08 - 3:14
    represent Fourier transform by the way,
    and this represents the nth derivative. If
  • 3:14 - 3:20
    I take the nth derivative of a function
    and I wanna find what that is, the Fourier
  • 3:20 - 3:24
    transform of the nth derivative is the
    same as taking the function itself, and
  • 3:24 - 3:30
    Fourier transforming it, and multiplying
    by ik to the nth power. Okay? So if I want
  • 3:30 - 3:34
    to compute the third derivative, all I
    gotta do is take the function, Fourier
  • 3:34 - 3:40
    transform it, multiply this by k^3 inverse
    Fourier transform it. That's it. By the
  • 3:40 - 3:44
    way, this is one of the most, if, if your
    boundary conditions work for you, like
  • 3:44 - 3:48
    here the boundaries go to zero, right? And
    there's supposed to be a periodic
  • 3:48 - 3:51
    function, but here the boundaries are
    pretty much going to zero. This is
  • 3:51 - 3:55
    probably one of the most accurate ways you
    can compute a derivative, spectrally
  • 3:55 - 3:59
    accurate. Okay? So let's compute a
    derivative, and here's the thing I'm gonna
  • 3:59 - 4:04
    compute. oh actually, we can go back with
    the lights down. Okay. So I'm gonna take
  • 4:04 - 4:11
    this function u here. and I'm gonna do the
    dollowing function. Here is my favorite
  • 4:11 - 4:16
    function, hyperbolic secant x. I don't
    know if you have a favorite function yet.
  • 4:16 - 4:21
    It's just a little bit of time before you
    graduate to pick one out. This one's taken
  • 4:21 - 4:27
    by the way, don't even try. and by the
    way. its derivative, it's called ud is
  • 4:30 - 4:36
    sech * tanh. Now what I'm going to do is
    try to compute t hat derivative
  • 4:36 - 4:41
    accurately. And by the way, second
    derivative, let's call it ud2 for the
  • 4:41 - 4:48
    second derivative will be sech x - two *
    sech x^3. something like that, pretty
  • 4:48 - 4:55
    close. I think I actually have it in the
    notes here. sorry, give me just a second
  • 4:55 - 5:03
    here. yeah, actually yeah, that's right.
    Okay. and what I'm gonna do is, now,
  • 5:03 - 5:08
    compute these derivatives using my Fourier
    transform. So let me write at the top.
  • 5:08 - 5:13
    There is u, second or first derivative.
    And first of all, if I wanna calculate the
  • 5:13 - 5:18
    derivatives, first I have to have Fourier
    transform of the function. There it is and
  • 5:18 - 5:23
    I'm gonna calculate now the first
    derivative. Okay? So, to calculate the
  • 5:23 - 5:31
    first derivative, remember, what I gotta
    do is take i * k and multiply it by
  • 5:33 - 5:39
    Fourier transform. There it is. And then,
    what I gotta do with this is inverse
  • 5:39 - 5:45
    Fourier transform it. Right?'Cuz Cuz what
    this is, is the derivative of the it's the
  • 5:45 - 5:51
    Fourier transform of the first derivative.
    So what I can do is just say, okay, if I
  • 5:51 - 5:57
    want the first derivative, let's call it
    my ud s for spectral approximations, first
  • 5:57 - 6:03
    derivative is spectral is ifft. By the way
    that's how you invert a Fourier transform,
  • 6:03 - 6:12
    ifft and fft are the pairs. Okay? And by
    the way, the second derivative, let's call
  • 6:12 - 6:22
    it ud2s is equal to ifft of i times k.
    That being squared now, times ut. There
  • 6:21 - 6:30
    you go. I, I want to call this u, right?
    So I'm kind of done. So, let's make some
  • 6:30 - 6:38
    plots. I'm going to plot here the function
    and for short here I'm going to say, call
  • 6:38 - 6:47
    it ks for k shift will be the fftshift(k)
    and so that way I can make this be just,
  • 6:48 - 6:53
    first I'm gonna plot the function. And
    then I'll plot, in addition to the
  • 6:53 - 6:59
    function, I'm gonna plot the derivative of
    the function, so x versus ud. And my
  • 6:59 - 7:04
    approximation, let's plot that in a red
    line and then I'm gonna plot my
  • 7:04 - 7:16
    approximation to the derivative uds, and
    I'll plot that with magenta circles. what
  • 7:16 - 7:21
    I'm plotting is here is the exact answer,
    right there, x, ud, that's the exact
  • 7:21 - 7:27
    answer and I'm plotting my approximation
    to the exact answer. Oh, that's just the
  • 7:27 - 7:31
    original function. We don't have to have
    that in there. Would you like to take it
  • 7:31 - 7:36
    away? We can just do that. What's that? Oh
    that was the fft, yeah we don't want the
  • 7:36 - 7:41
    fft of the original function. We just want
    the function. Oh, by the way. There you
  • 7:41 - 7:46
    go, look at that. The red line is exact,
    the magenta line is my approximation. Who
  • 7:46 - 7:50
    likes that approximation? Give it up you
    all. Okay. So listen, I know it's the
  • 7:50 - 7:54
    first day of class, we're just warming up.
    We'll have a dance party about midway. We
  • 7:54 - 7:59
    can get disco balls and you know, we can
    shine that light on there. It's pretty
  • 7:59 - 8:03
    sweet. Okay. Alright so there is my
    approximation. By the way, let's zoom in a
  • 8:03 - 8:08
    little bit. Let's just see how well we
    really did. Let's see how long it takes
  • 8:08 - 8:12
    for that magenta ball to fall off. Oh, oh,
    oh, oh, come on, fall off. I'm zooming in,
  • 8:12 - 8:17
    I'm zooming in, I'm zooming in. Look, it's
    pretty dang accurate. Look at that. Oh,
  • 8:17 - 8:21
    it's finally coming off there. I know it,
    I can sense it. Oh, look at that. But look
  • 8:21 - 8:26
    over here on the left, it's like down to
    ten minus five or negative six. Pretty
  • 8:26 - 8:30
    amazing accuracy. Now, you can screw up.
    Let me just show you one other thing.
  • 8:30 - 8:38
    Suppose I take a smaller domain. Oh, not
    quite hold on a little smaller just real
  • 8:38 - 8:45
    quick. Just give me two seconds for the
    final. Oh, crap. Okay. Come on. Let's do
  • 8:45 - 8:50
    four. wanna, I wanna, I wanna show you
    what happens when you kind of start to
  • 8:50 - 8:55
    violate, yeah, there you go, you start
    seeing it. Now, notice what happens here?
  • 8:55 - 9:00
    Here, remember it's supposed to be a two
    pie periodic function. Right? This is
  • 9:00 - 9:05
    clearly not periodic. Before they both
    went to zero, you could say kinda looks
  • 9:05 - 9:10
    periodic. Here, it's up here above point
    two here. It's below negative point two.
  • 9:10 - 9:15
    There's a jump and any time you have a
    jump in a Fourier transform, when you try
  • 9:15 - 9:19
    to represent, you get what's called a
    Gibbs' phenomenon, which is oscillations
  • 9:19 - 9:24
    near the jump and this is what you see
    over here. So it does amazingly well here
  • 9:24 - 9:28
    and over here it starts to oscillate, not
    so well. Okay. We're gonna use this on
  • 9:28 - 9:35
    Friday to do radar problems. Okay. Ha ve a
    good Wednesday. See you guys soon.
Title:
W01_L01_P04 - Derivatives with the FFT
jngiam edited English subtitles for W01_L01_P04 - Derivatives with the FFT
jngiam added a translation

English subtitles

Revisions