1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
import numpy as np import matplotlib.pyplot as plt
Fs = 100 # sampling rate Ts = 1.0/Fs # sampling interval t = np.arange(0,1,Ts) # time vector ff = 5 # frequency of the signal y = np.sin(2 * np.pi * ff * t)
plt.subplot(2,1,1) plt.plot(t,y,'b-') plt.xlabel('time') plt.ylabel('amplitude')
plt.subplot(2,1,2) n = len(y) # length of the signal k = np.arange(n) T = n/Fs frq = k/T # two sides frequency range freq = frq[range(int(n/2))] # one side frequency range
Y = np.fft.fft(y)/n # fft computing and normalization Y = Y[range(int(n/2))]
plt.plot(freq, abs(Y), 'r-') plt.xlabel('freq (Hz)') plt.ylabel('|Y(freq)|')
plt.show()
|
近期评论