%matplotlib inline import numpy as np import matplotlib.pyplot as plt f_s = 50.0 # Hz f = 1.0 # Hz time = np.arange(0.0, 3.0, 1/f_s) x = 5 * np.sin(2 * np.pi * f * time) + 2 * np.sin(10 * 2 * np.pi * f * time) plt.plot(time, x) plt.xlabel("Time (sec)") plt.ylabel("x") fft_x = np.fft.fft(x) n = len(fft_x) freq = np.fft.fftfreq(n, 1/f_s) print n print freq plt.plot(np.abs(fft_x)) fft_x_shifted = np.fft.fftshift(fft_x) freq_shifted = np.fft.fftshift(freq) plt.plot(freq_shifted, np.abs(fft_x_shifted)) plt.xlabel("Frequency (Hz)") half_n = np.ceil(n/2.0) fft_x_half = (2.0 / n) * fft_x[:half_n] freq_half = freq[:half_n] plt.plot(freq_half, np.abs(fft_x_half)) plt.xlabel("Frequency (Hz)") plt.ylabel("Amplitude")