matplotlib繪製引力波

詳細註釋

'''
引力波繪製
'''
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile#讀取波形的庫
'''
從配置文檔中讀取時間相關數據
r表示轉義,當出現特殊字符時加一個r表示原來的字符
H1_Strain.wav、L1_Strain.wav:音頻文件
wf_template.txt:引力波的理論模型
np.genfromtxt():執行兩個循環:將文件的每一行轉化爲字符串序列和將每一個字符串序列轉化爲相應的數據類型;即是一個兩行的矩陣
transpose():矩陣轉置
'''
rate_h,hstrain=wavfile.read(r"H1_Strain.wav","rb") #rate:速率 strain:數據矩陣
rate_l,lstrain=wavfile.read(r"L1_Strain.wav","rb")
reftime,ref_H1=np.genfromtxt(r"wf_template.txt").transpose()#reftime:時間序列 ref_H1:信號的數據

'''
求導數,得到波形的時間間隔
'''
htime_interval=1/rate_h
ltime_interval=1/rate_h

'''
丟失信號起始點
'''
htime_len=hstrain.shape[0]/rate_h  
#hstrain.shape[0]表示讀取矩陣的第一維的長度即數據點的個數,除以座標軸rate就可以得到函數在座標軸上的總長度
htime=np.arange(-htime_len/2,htime_len/2,htime_interval)
#繪製出關於原點對稱的圖像,以-1/2爲起點1/2,interval爲時間間隔 創造時間序列htime
ltime_len=lstrain.shape[0]/rate_l 
ltime=np.arange(-ltime_len/2,ltime_len/2,ltime_interval)

'''
繪製圖形y
繪製以時間爲x軸,應變數據爲Y軸的圖像,並設置標題和座標軸的標籤
'''
fig=plt.figure(figsize=(12,6))#創繪圖空間12*6

plth=fig.add_subplot(221)
plth.plot(htime,hstrain,'y')
plth.set_xlabel('Tiem (seconds)')
plth.set_ylabel('H1 Strain')
plth.set_title('H1 Strain')

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("Wave.png")
plt.show()
plt.close(fig)

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章