0

I am trying to calculate the magnitude and angle of the DFT of a rectangular window. The code below gets the magnitude I expect, but the phase looks wrong. Can anyone help me figure out what I'm doing wrong? Thanks!

This is what I get:

enter image description here

This is what the book I'm reading (by Richard Lyons) has:

enter image description here

fs = 256  # sample freq - number of samples per second
k  = 11
window = np.array([0] * 64 + [1] * k + [0] * 64)
freq = fft(window)

plt.stem(fftfreq(len(freq), 1/fs), np.abs(freq), 'r')
plt.show()
plt.stem(fftfreq(len(freq), 1/fs), np.angle(freq), 'r')
plt.xlim([-30, 30])
plt.show()
moinudin
  • 134,091
  • 45
  • 190
  • 216
  • Does this answer your question? [Sign on results of fft](https://stackoverflow.com/questions/26171838/sign-on-results-of-fft) – Cris Luengo Mar 12 '21 at 22:31

1 Answers1

0

https://stackoverflow.com/a/60764461/89806 solved my problem. I just had to add fftshift(window) before calling fft.

moinudin
  • 134,091
  • 45
  • 190
  • 216
  • If your question is answered by an answer in another question, please close it as a duplicate. It keeps things much more organized than by posting an answer that is not much more than a link to another answer. – Cris Luengo Mar 12 '21 at 22:32