前缀 :将一维机械振动信号构造为训练集和测试集(Python)
https://mp.weixin.qq.com/s/DTKjBo6_WAQ7bUPZEdB1TA
import pandas as pd
import numpy as np
import scipy.io as sio
import statistics_hamming
from statistics_hamming import *
signals_train_mat = sio.loadmat('./ProcessedData/signals_train.mat')
signals_train_mat
{'__header__': b'MATLAB 5.0 MAT-file Platform: nt, Created on: Wed May 5 09:16:40 2021',
'__version__': '1.0',
'__globals__': [],
'Signal': array([[ 0.00470634, 0.00733712, 0.00963905, ..., 0.00536404,
0.00470634, 0.00503519],
[-0.00614561, -0.00581676, -0.00450137, ..., 0.00503519,
0.00799482, 0.0093102 ],
[-0.01699756, -0.01436679, -0.01140716, ..., 0.0777104 ,
0.09053544, 0.08330081],
...,
[-0.00219944, -0.0018706 , -0.00154175, ..., 0.00963905,
0.0093102 , 0.00898136],
[ 0.00503519, 0.00207557, 0.00174672, ..., -0.00022636,
-0.0012129 , -0.00285714],
[-0.00252829, -0.00219944, -0.0018706 , ..., -0.01173601,
-0.00647446, 0.00043133]]),
'Tipo': array(['Outer', 'Outer', 'Inner', 'Sano ', 'Inner', 'Sano ', 'Outer',
'Inner', 'Inner', 'Outer', 'Sano ', 'Outer', 'Sano ', 'Inner',
'Outer', 'Sano ', 'Inner', 'Outer', 'Sano ', 'Outer', 'Inner',
'Inner', 'Inner', 'Outer', 'Sano ', 'Sano ', 'Sano '], dtype='<U5')}
X_train = signals_train_mat['Signal']
y_train = signals_train_mat['Tipo']
signals_test_mat = sio.loadmat('./ProcessedData/signals_test.mat')
X_test = signals_test_mat['Signal']
y_test = signals_test_mat['Tipo']
signal = X_train[0][0:20000]
signal
array([ 0.00470634, 0.00733712, 0.00963905, ..., -0.01206486,
-0.01009178, -0.00614561])
kurt_train = []
impulse_factor_train = []
RMS_train = []
margin_factor_train = []
skewness_train = []
shape_factor_train = []
peak_to_peak_train = []
crest_factor_train = []
for i in range(len(X_train)):
for j in range(10):
signal = X_train[i][200000 * j : 200000 * (j+1)]
kurt, impulse_factor, RMS, margin_factor, skewness, shape_factor, peak_to_peak, crest_factor = parameters_hamming(signal)
kurt_train.append(kurt)
impulse_factor_train.append(impulse_factor)
RMS_train.append(RMS)
margin_factor_train.append(margin_factor)
skewness_train.append(skewness)
shape_factor_train.append(shape_factor)
peak_to_peak_train.append(peak_to_peak)
crest_factor_train.append(crest_factor)
y_train = signals_train_mat['Tipo']
tipo_train = []
for i in range(len(y_train)):
for j in range(10):
tipo_train.append(y_train[i])
y_train = tipo_train
len(y_train)
270
kurt_test = []
impulse_factor_test = []
RMS_test = []
margin_factor_test = []
skewness_test = []
shape_factor_test = []
peak_to_peak_test = []
crest_factor_test = []
for i in range(len(X_test)):
for j in range(10):
signal = X_test[i][200000 * j : 200000 * (j+1)]
kurt, impulse_factor, RMS, margin_factor, skewness, shape_factor, peak_to_peak, crest_factor = parameters_hamming(signal)
kurt_test.append(kurt)
impulse_factor_test.append(impulse_factor)
RMS_test.append(RMS)
margin_factor_test.append(margin_factor)
skewness_test.append(skewness)
shape_factor_test.append(shape_factor)
peak_to_peak_test.append(peak_to_peak)
crest_factor_test.append(crest_factor)
y_test = signals_test_mat['Tipo']
tipo_test = []
for i in range(len(y_test)):
for j in range(10):
tipo_test.append(y_test[i])
y_test = tipo_test
len(y_test)
90
df_train = pd.DataFrame({'Tipo': np.core.defchararray.replace(y_train, ' ', ''), 'Kurtosis': kurt_train,
'Impulse factor': impulse_factor_train,
'RMS': RMS_train, 'Margin factor': margin_factor_train, 'Skewness': skewness_train,
'Shape factor': shape_factor_train, 'Peak to peak': peak_to_peak_train,
'Crest factor': crest_factor_train})
df_test = pd.DataFrame({'Tipo': np.core.defchararray.replace(y_test, ' ', ''), 'Kurtosis': kurt_test,
'Impulse factor': impulse_factor_test,
'RMS': RMS_test, 'Margin factor': margin_factor_test, 'Skewness': skewness_test,
'Shape factor': shape_factor_test, 'Peak to peak': peak_to_peak_test,
'Crest factor': crest_factor_test})
df_test
df_train.to_csv('./ProcessedData/statistics_10_train.csv', index = False, header = True, sep = ',')
df_test.to_csv('./ProcessedData/statistics_10_test.csv', index = False, header = True, sep = ',')
特征提取辅助函数
import scipy.stats
from scipy.stats import kurtosis, skew
from scipy import signal
import matplotlib.pyplot as plt
import numpy as np
def highlowfilter(k_filter, input_signal):
'''
Given a signal, it applies the high pass or low pass band filter to it, depending on the input choice.
INPUT:
- k_filter: kind of filter applied:
* 'hp' high pass
* 'low': low pass
- input_signal: signal to which to apply the filter
'''
b, a = signal.butter(3, 0.05, k_filter)
zi = signal.lfilter_zi(b, a)
z, _ = signal.lfilter(b, a, input_signal, zi = zi * input_signal[0])
z2, _ = signal.lfilter(b, a, z, zi = zi * z[0])
y = signal.filtfilt(b, a, input_signal)
return z, z2, y
def parameters_hamming(xsignal):
'''
Given the signal 'xsignal', it applies the Hamming window function, a low pass filter,
and calculates certain statistics and parameters.
'''
xsignal = xsignal * signal.hamming(len(xsignal))
_, _, x = highlowfilter('low', xsignal)
N = len(x)
n_inf = max(abs(x))
kurt = kurtosis(x)
impulse_factor = N * n_inf / sum(abs(x))
RMS = np.sqrt(sum(x**2))
margin_factor = n_inf / RMS**2
skewness = skew(x)
shape_factor = N * RMS / sum(abs(x))
peak_to_peak = max(x) - min(x)
crest_factor = n_inf / RMS
return kurt, impulse_factor, RMS, margin_factor, skewness, shape_factor, peak_to_peak, crest_factor
知乎学术咨询:
https://www.zhihu.com/consult/people/792359672131756032?isMe=1
擅长领域:现代信号处理,机器学习,深度学习,数字孪生,时间序列分析,设备缺陷检测、设备异常检测、设备智能故障诊断与健康管理PHM等。