代码:
import numpy as np #科学计算所用的numpy库
import matplotlib.pyplot as plt #绘图所用的库
from scipy.io import wavfile #读取波形文件所用的库
rate_h, hstrain= wavfile.read(r"H1_Strain.wav","rb") #读取音频文件
rate_l, lstrain= wavfile.read(r"L1_Strain.wav","rb")
#reftime, ref_H1 = np.genfromtxt('GW150914_4_NR_waveform_template.txt').transpose()
reftime, ref_H1 = np.genfromtxt('wf_template.txt').transpose() #使用python123.io下载文件
htime_interval = 1/rate_h
ltime_interval = 1/rate_l
fig = plt.figure(figsize=(12, 6)) #创建大小为12*6的绘图空间
# 丢失信号起始点
htime_len = hstrain.shape[0]/rate_h #读取数据第一维的长度,得到函数在坐标轴上总长度
htime = np.arange(-htime_len/2, htime_len/2 , htime_interval) #(起点,终点,时间间隔)
plth = fig.add_subplot(221) #设置绘图区域
plth.plot(htime, hstrain, 'y') #画出以时间为x轴,应变数据为y轴的图像,‘y'为黄色
plth.set_xlabel('Time (seconds)')
plth.set_ylabel('H1 Strain')
plth.set_title('H1 Strain')
ltime_len = lstrain.shape[0]/rate_l
ltime = np.arange(-ltime_len/2, ltime_len/2 , ltime_interval)
pltl = fig.add_subplot(222)
pltl.plot(ltime, lstrain, 'g')
pltl.set_xlabel('Time (seconds)')
pltl.set_ylabel('L1 Strain')
pltl.set_title('L1 Strain')
pltref = fig.add_subplot(212)
pltref.plot(reftime, ref_H1)
pltref.set_xlabel('Time (seconds)')
pltref.set_ylabel('Template Strain')
pltref.set_title('Template')
fig.tight_layout() #自动调整图像外部边缘
plt.savefig("Gravitational_Waves_Original.png") #保存图像为png格式
plt.show()
plt.close(fig)
结果图: