很久之前写了一篇信号下的基本时域频域特征(上),后来一直催更,直到今天才更新,实在是不好意思,理解万岁。
这次将所有我平时用的16个时域特征和10个频域特征的程序都放到这里
)了,另外这次单独列出频域的公式供大家参考。
程序如下:大家可以对照公式看程序,如果有错误,欢迎指出交流
# ======================= STATISTICAL FEATURES IN TIME DOMAIN =======================
# def fft_fft(sequence_data):
# fft_trans = np.abs(np.fft.fft(sequence_data))
# dc = fft_trans[0]
# freq_spectrum = fft_trans[1:int(np.floor(len(sequence_data) * 1.0 / 2)) + 1]
# freq_sum_ = np.sum(freq_spectrum)
# return dc, freq_spectrum, freq_sum_
def fft_mean(sequence_data):
def fft_fft(sequence_data):
fft_trans = np.abs(np.fft.fft(sequence_data))
# dc = fft_trans[0]
freq_spectrum = fft_trans[1:int(np.floor(len(sequence_data) * 1.0 / 2)) + 1]
_freq_sum_ = np.sum(freq_spectrum)
return freq_spectrum, _freq_sum_
freq_spectrum, _freq_sum_ = fft_fft(sequence_data)
return np.mean(freq_spectrum)
def fft_var(sequence_data):
def fft_fft(sequence_data):
fft_trans = np.abs(np.fft.fft(sequence_data))
# dc = fft_trans[0]
freq_spectrum = fft_trans[1:int(np.floor(len(sequence_data) * 1.0 / 2)) + 1]
_freq_sum_ = np.sum(freq_spectrum)
return freq_spectrum, _freq_sum_
freq_spectrum, _freq_sum_ = fft_fft(sequence_data)
return np.var(freq_spectrum)
# def fft_std(sequence_data):
# def fft_fft(sequence_data):
# fft_trans = np.abs(np.fft.fft(sequence_data))
# # dc = fft_trans[0]
# freq_spectrum = fft_trans[1:int(np.floor(len(sequence_data) * 1.0 / 2)) + 1]
# _freq_sum_ = np.sum(freq_spectrum)
# return freq_spectrum, _freq_sum_
# freq_spectrum, _freq_sum_ = fft_fft(sequence_data)
# return np.std(freq_spectrum)
def fft_entropy(sequence_data):
def fft_fft(sequence_data):
fft_trans = np.abs(np.fft.fft(sequence_data))
# dc = fft_trans[0]
freq_spectrum = fft_trans[1:int(np.floor(len(sequence_data) * 1.0 / 2)) + 1]
_freq_sum_ = np.sum(freq_spectrum)
return freq_spectrum, _freq_sum_
freq_spectrum, _freq_sum_ = fft_fft(sequence_data)
pr_freq = freq_spectrum * 1.0 / _freq_sum_
entropy = -1 * np.sum([np.log2(p+1e-5) * p for p in pr_freq])
return entropy
def fft_energy(sequence_data):
def fft_fft(sequence_data):
fft_trans = np.abs(np.fft.fft(sequence_data))
# dc = fft_trans[0]
freq_spectrum = fft_trans[1:int(np.floor(len(sequence_data) * 1.0 / 2)) + 1]
_freq_sum_ = np.sum(freq_spectrum)
return freq_spectrum, _freq_sum_
freq_spectrum, _freq_sum_ = fft_fft(sequence_data)
return np.sum(freq_spectrum ** 2) / len(freq_spectrum)
def fft_skew(sequence_data):
def fft_fft(sequence_data):
fft_trans = np.abs(np.fft.fft(sequence_data))
# dc = fft_trans[0]
freq_spectrum = fft_trans[1:int(np.floor(len(sequence_data) * 1.0 / 2)) + 1]
_freq_sum_ = np.sum(freq_spectrum)
return freq_spectrum, _freq_sum_
freq_spectrum, _freq_sum_ = fft_fft(sequence_data)
_fft_mean, _fft_std = fft_mean(sequence_data), fft_std(sequence_data)
return np.mean([0 if _fft_std < epsn else np.power((x - _fft_mean) / _fft_std, 3)
for x in freq_spectrum])
def fft_kurt(sequence_data):
def fft_fft(sequence_data):
fft_trans = np.abs(np.fft.fft(sequence_data))
# dc = fft_trans[0]
freq_spectrum = fft_trans[1:int(np.floor(len(sequence_data) * 1.0 / 2)) + 1]
_freq_sum_ = np.sum(freq_spectrum)
return freq_spectrum, _freq_sum_
freq_spectrum, _freq_sum_ = fft_fft(sequence_data)
_fft_mean, _fft_std = fft_mean(sequence_data), fft_std(sequence_data)
return np.mean([0 if _fft_std < epsn else np.power((x - _fft_mean) / _fft_std, 4)
for x in freq_spectrum])
def fft_shape_mean(sequence_data):
def fft_fft(sequence_data):
fft_trans = np.abs(np.fft.fft(sequence_data))
# dc = fft_trans[0]
freq_spectrum = fft_trans[1:int(np.floor(len(sequence_data) * 1.0 / 2)) + 1]
_freq_sum_ = np.sum(freq_spectrum)
return freq_spectrum, _freq_sum_
freq_spectrum, _freq_sum_ = fft_fft(sequence_data)
shape_sum = np.sum([x * freq_spectrum[x]
for x in range(len(freq_spectrum))])
return 0 if _freq_sum_ < epsn else shape_sum * 1.0 / _freq_sum_
def fft_shape_std(sequence_data):
def fft_fft(sequence_data):
fft_trans = np.abs(np.fft.fft(sequence_data))
# dc = fft_trans[0]
freq_spectrum = fft_trans[1:int(np.floor(len(sequence_data) * 1.0 / 2)) + 1]
_freq_sum_ = np.sum(freq_spectrum)
return freq_spectrum, _freq_sum_
freq_spectrum, _freq_sum_ = fft_fft(sequence_data)
shape_mean = fft_shape_mean(sequence_data)
var = np.sum([0 if _freq_sum_ < epsn else np.power((x - shape_mean), 2) * freq_spectrum[x]
for x in range(len(freq_spectrum))]) / _freq_sum_
return np.sqrt(var)
def fft_shape_skew(sequence_data):
def fft_fft(sequence_data):
fft_trans = np.abs(np.fft.fft(sequence_data))
# dc = fft_trans[0]
freq_spectrum = fft_trans[1:int(np.floor(len(sequence_data) * 1.0 / 2)) + 1]
_freq_sum_ = np.sum(freq_spectrum)
return freq_spectrum, _freq_sum_
freq_spectrum, _freq_sum_ = fft_fft(sequence_data)
shape_mean = fft_shape_mean(sequence_data)
return np.sum([np.power((x - shape_mean), 3) * freq_spectrum[x]
for x in range(len(freq_spectrum))]) / _freq_sum_
def fft_shape_kurt(sequence_data):
def fft_fft(sequence_data):
fft_trans = np.abs(np.fft.fft(sequence_data))
# dc = fft_trans[0]
freq_spectrum = fft_trans[1:int(np.floor(len(sequence_data) * 1.0 / 2)) + 1]
_freq_sum_ = np.sum(freq_spectrum)
return freq_spectrum, _freq_sum_
freq_spectrum, _freq_sum_ = fft_fft(sequence_data)
shape_mean = fft_shape_mean(sequence_data)
return np.sum([np.power((x - shape_mean), 4) * freq_spectrum[x] - 3
for x in range(len(freq_spectrum))]) / _freq_sum_
# =================================== END =====================================
关于所有的特征的程序,大家从文章开头的链接里找,Thanks♪(・ω・)ノ