Python读取mat文件,并使用HHT变换计算信号的时间-频率曲线
因为用不到经验模态分解,所以文章内并未涉及。。
程序正文
import numpy as np
from scipy.io import loadmat
from scipy.signal import hilbert
import matplotlib.pyplot as plt
# 定义hht变换函数
def myhht(y,t):
fs = 1/(t[2]-t[1])
hy = hilbert(y) # 希尔伯特变换
amp = abs(hy) # 计算幅值
angle_h = np.angle(hy) # 计算相位
pha = np.unwrap(angle_h) # 去除相位2pi跃变
fre = abs(fs*np.diff(pha/2/np.pi)) # 计算频率并进行坐标变换
return amp,fre,pha # 返回幅值,频率和相位
## read file
file_pos = 'E:\\Data\\space_ranging\\0331\\'
file_name = '00m_000'
file_type = '.mat' # 单纯是为了看起来更简洁,更改起来比较方便
all_name = file_pos+file_name+file_type # 将文件名称拼接起来
all_data = loadmat(all_name) # 下载文件
mydata = all_data["data"] # 提取数据
mytime = all_data["time"] # 提取时间坐标
Y = mydata.flatten() # 将多维数据降为一维数
t = mytime.flatten()
samplingtime = all_data['sampleInterval'] # 提取采样时间间隔
fs = 1/samplingtime[0] # 计算采样频率
amp,fre,pha = myhht(Y,t) # 调用函数
plt.figure()
plt.plot(t,Y) # 图形绘制
plt.title('Signal')
plt.xlabel('Time')
plt.ylabel('Intencity')
plt.figure()
plt.plot(t[0:-1],fre,'r') # 图形绘制
plt.title('Time-Frequence-Line')
plt.xlabel('Time')
plt.ylabel('Frequence')
plt.show()
至此完成所有计算