Created
February 27, 2020 22:29
-
-
Save 0xfe/d5b29e9f9b987eeefce1148ec3698b60 to your computer and use it in GitHub Desktop.
FFT of Sine Wave
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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