3

my original problem was the following:

I have a pulse-envelope in an array a (0-element = time 0, last element = time T). I want to fourier spectrum of the pulse. So what I did was np.fft.fftshift(np.fft.fft(a)). All good.

But then I was told to do a shift beforehand, too: np.fft.fftshift(np.fft.fft(np.fft.fftshift(a))). Then oscillations arised.

Now I wonder why one would do 2 shifts as shown above and why oscillations arise...

Here the example: I have the following code

x = np.arange(100)
a =np.sin(np.pi*x**2/1000)

a: a(x)

a_fft = np.fft.fft(a)

a_fft: enter image description here

 a_fft_shift = np.fft.fftshift(a_fft)

a_fft_shift: enter image description here

    a_shift = np.fft.fftshift(a)
    a_shift_fft = np.fft.fft(a_shift)

a_shift_fft: enter image description here

    a_shift_fft_shift = np.fft.fftshift(a_shift_fft)

a_shift_fft_shift: enter image description here

refle
  • 587
  • 3
  • 6
  • 19
  • 3
    Before the shift : the low frequencies are at 0 and 100. After the shift, they are at 50, as if they corresponded to high frequencies. FFT and FFT^{-1} are really close. Hence, as the FFT of the shifted signal is computed, high frequency occilations occur. `np.fft.fftshift()` is only useful to display low frequencies in the middle of the plot. Use it for display only ! – francis Jun 15 '16 at 07:18

1 Answers1

4

Your line

a_shift = np.fft.fftshift(a)

reorders your original time-domain signal. That means in terms of FFT that you are altering the phases. Note also that there is a discontinuity in your signal. By the line above, this discontinuity is shifted to the center of the signal, which causes the FFT to produce an infinite amount of high frequency cosine components. If you shift it to another place, the energy will be distributed accordingly. The other problem is that you are only considering the real part of the spectrum, i. e., the cosine components. Always look at the imaginary part, too! Take also a look at the magnitude spectrum to see that the position of the discontinuity only affects the phase. The total energy remains always the same.

comparison

MaxPowers
  • 5,235
  • 2
  • 44
  • 69