Instantly share code, notes, and snippets.

# jedludlow/ipython_fft_example.ipynb

Created October 19, 2012 16:16
Show Gist options
• Save jedludlow/3919130 to your computer and use it in GitHub Desktop.
IPython Notebook FFT Example
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

### tarokiritani commented Oct 19, 2016

I assume the DC amplitude appears only once in fft_x, and therefore should not be multiplied by 2? Am I mistaken?

### jedludlow commented Nov 16, 2016 • edited

@tarokiritani Yes, that is correct. More precisely, the FFT returns energy across the spectrum of positive, negative, and zero frequencies. The folding process takes all the energy from the negative frequencies and places it at the matching positive frequencies. There is a single entry in `fft_x` for zero frequency, so there is no folding required there.

### raproth commented Aug 16, 2018

is the lowest plot equivalent to the PSD?

### misterjoa commented Mar 7, 2019

This notebook is gold ! Thank you very much !

### impvd commented Oct 30, 2020

@jedludlow I know you create this gist 8 years ago. Just curious if maybe we could make it do the `ifft` operation? We have already know that the signal was the combination with `5 * np.sin(2 * np.pi * f * time)` and `2 * np.sin(10 * 2 * np.pi * f * time)` , what if we don't know the formula before, and we only get the value of `x`, which is like:

```array([ 0.00000000e+00,  2.52877920e+00,  2.41901994e+00,  6.65052259e-01,
5.06655338e-01,  2.93892626e+00,  5.32484856e+00,  5.02813672e+00,
3.04606912e+00,  2.62202223e+00,  4.75528258e+00,  6.81354929e+00,
6.16570415e+00,  3.81456314e+00,  3.00932322e+00,  4.75528258e+00,
6.42624829e+00,  5.39721013e+00,  2.67699571e+00,  1.52062250e+00,
2.93892626e+00,  4.31088140e+00,  3.01619327e+00,  6.78789312e-02,
-1.27544686e+00, -1.83697020e-15,  1.27544686e+00, -6.78789312e-02,
-3.01619327e+00, -4.31088140e+00, -2.93892626e+00, -1.52062250e+00,
-2.67699571e+00, -5.39721013e+00, -6.42624829e+00, -4.75528258e+00,
-3.00932322e+00, -3.81456314e+00, -6.16570415e+00, -6.81354929e+00,
-4.75528258e+00, -2.62202223e+00, -3.04606912e+00, -5.02813672e+00,
-5.32484856e+00, -2.93892626e+00, -5.06655338e-01, -6.65052259e-01,
-2.41901994e+00, -2.52877920e+00, -6.12323400e-15,  2.52877920e+00,
2.41901994e+00,  6.65052259e-01,  5.06655338e-01,  2.93892626e+00,
5.32484856e+00,  5.02813672e+00,  3.04606912e+00,  2.62202223e+00,
4.75528258e+00,  6.81354929e+00,  6.16570415e+00,  3.81456314e+00,
3.00932322e+00,  4.75528258e+00,  6.42624829e+00,  5.39721013e+00,
2.67699571e+00,  1.52062250e+00,  2.93892626e+00,  4.31088140e+00,
3.01619327e+00,  6.78789312e-02, -1.27544686e+00, -1.97217653e-14,
1.27544686e+00, -6.78789312e-02, -3.01619327e+00, -4.31088140e+00,
-2.93892626e+00, -1.52062250e+00, -2.67699571e+00, -5.39721013e+00,
-6.42624829e+00, -4.75528258e+00, -3.00932322e+00, -3.81456314e+00,
-6.16570415e+00, -6.81354929e+00, -4.75528258e+00, -2.62202223e+00,
-3.04606912e+00, -5.02813672e+00, -5.32484856e+00, -2.93892626e+00,
-5.06655338e-01, -6.65052259e-01, -2.41901994e+00, -2.52877920e+00,
-1.22464680e-14,  2.52877920e+00,  2.41901994e+00,  6.65052259e-01,
5.06655338e-01,  2.93892626e+00,  5.32484856e+00,  5.02813672e+00,
3.04606912e+00,  2.62202223e+00,  4.75528258e+00,  6.81354929e+00,
6.16570415e+00,  3.81456314e+00,  3.00932322e+00,  4.75528258e+00,
6.42624829e+00,  5.39721013e+00,  2.67699571e+00,  1.52062250e+00,
2.93892626e+00,  4.31088140e+00,  3.01619327e+00,  6.78789312e-02,
-1.27544686e+00,  5.02600372e-15,  1.27544686e+00, -6.78789312e-02,
-3.01619327e+00, -4.31088140e+00, -2.93892626e+00, -1.52062250e+00,
-2.67699571e+00, -5.39721013e+00, -6.42624829e+00, -4.75528258e+00,
-3.00932322e+00, -3.81456314e+00, -6.16570415e+00, -6.81354929e+00,
-4.75528258e+00, -2.62202223e+00, -3.04606912e+00, -5.02813672e+00,
-5.32484856e+00, -2.93892626e+00, -5.06655338e-01, -6.65052259e-01,
-2.41901994e+00, -2.52877920e+00])```

And we need to separate it to `x1` and `x2`, I mean `x1` and `x2` would be the signal with the same basic frequency.

Awesome thanks again for your share.

### jedludlow commented Oct 30, 2020 • edited

@raproth, I apologize for not replying to your comment from so long ago. I somehow missed any notification about it.

The plot is not precisely a PSD. The power spectral density expresses signal power distribution as a function of frequency and has the property that the integral under the PSD over some frequency range represents the total signal power in that frequency range.

The plot I'm producing here is really just a plot of the magnitude of the Fourier coefficients. It's useful for reasoning about the amplitude of the various sinusoids that make up the underlying signal.

### jedludlow commented Oct 30, 2020

@pivdets, I'm not sure I fully understand your question, but I'll make an attempt at an answer to the `ifft` part.

I don't have time right now to extend the notebook to include the `ifft` case, but I can describe how you can figure it out on your own. You can take the Fourier spectrum results that are plotted in cell 9 at the end of the notebook and try to work backwards through the problem, using the `ifft` instead of `fft` in cell 4. If your code reproduces the signal plotted in cell 3 then you know you've got it working correctly. Maybe you can submit a pull request if you get it working. Once you have proven your method you can apply it to other Fourier spectra to recover the underlying signal.

to join this conversation on GitHub. Already have an account? Sign in to comment