import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.pylab import mpl
mpl.rcParams[‘font.sans-serif’] = [‘SimHei’] #顯示中文
mpl.rcParams[‘axes.unicode_minus’]=False #顯示負號
data_broken= pd.read_csv(‘broken.csv’,usecols=[‘value’]).dropna()
data_healthy = pd.read_csv(‘healthy.csv’,usecols=[‘value’]).dropna()
data_broken= data_broken[147500:155000].values
data_broken= data_broken.reshape(len(data_belt),) #超級重要,少了這行FFT怎麼轉都失敗
data_healthy= data_healthy[131000:138000].values
data_healthy= data_healthy.reshape(len(data_normal),) #超級重要,少了這行FFT怎麼轉都失敗
sampling_rate = 10000 #取樣頻率,ex:每秒取10000筆,則頻率為10000Hz
fft_size = len(data_healthy)
t = np.arange(0, 1.0, 1.0/sampling_rate)
xs = data_healthy
xf = np.fft.rfft(xs)/fft_size
freqs = np.linspace(0, sampling_rate/2, fft_size/2+1)
xfp = np.abs(xf)
fft_size_b = len(data_broken)
t_b = np.arange(0, 1.0, 1.0/sampling_rate)
xs_b = data_broken
xf_b = np.fft.rfft(xs_b)/fft_size_b
freqs_b = np.linspace(0, sampling_rate/2, fft_size_b/2+1)
xfp_b = np.abs(xf_b)
plt.figure(figsize=(20,10))
plt.subplot(211)
plt.plot(t[:fft_size], xs, color=’blue’)
plt.xlabel(u”Time(S)”)
plt.title(“data_healthy”)
plt.show()
plt.subplot(212)
plt.plot(t_b[:fft_size_b], xs_b, color=’red’)
plt.xlabel(u”Time(S)”)
plt.title(“data_broken”)
plt.show()
plt.figure(figsize=(20,5))
plt.plot(freqs, xfp, color=’blue’)
plt.xlabel(u”Freq(Hz)”)
plt.title(“data_healthy_FFT”)
plt.show()
plt.figure(figsize=(20,5))
plt.plot(freqs_b, xfp_b, color=’red’)
plt.xlabel(u”Freq(Hz)”)
plt.title(“data_broken_FFT”)
plt.show()