Skip to content

Instantly share code, notes, and snippets.

@0xfe
Created February 27, 2020 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xfe/d5b29e9f9b987eeefce1148ec3698b60 to your computer and use it in GitHub Desktop.
Save 0xfe/d5b29e9f9b987eeefce1148ec3698b60 to your computer and use it in GitHub Desktop.
FFT of Sine Wave
# Generate an integer-period sine wave to
# eliminate spectral leak.
hz = 20
x = np.linspace(0, 2 * np.pi * hz, 128 * hz)
y = np.sin(x)
plt.figure(figsize=(10, 3))
plt.plot(x, y)
plt.show()
# We're only interested in the real components
fft_y = fft.rfft(y) / (len(x) / 2)
plt.figure(figsize=(10, 3))
plt.bar(np.arange(100), np.absolute(fft_y)[:100]) # Show just the first 100 bins
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment