基于MATLAB的代码示例,用于提取时域和频域信号的29个特征。这些特征包括常见的时域统计特征和频域特征,适用于信号分析和机器学习分类问题。
MATLAB代码实现
1. 定义特征提取函数
function fea = genFeature(data, fs, featureNamesCell)
% 时域信号特征提取
% 输入:
% data:待特征提取的时域信号
% fs:采样频率,如果不提取频域特征,fs值可以设置为1
% featureNamesCell:拟进行特征提取的特征名称,该变量为cell类型,其中包含的特征名称为字符串
% 输出:
% fea:数据data的特征值数组,其特征值顺序与featureNamesCell一一对应
% 初始化特征数组
fea = zeros(1, length(featureNamesCell));
% 时域特征
if any(strcmp(featureNamesCell, 'max'))
fea(strcmp(featureNamesCell, 'max')) = max(data);
end
if any(strcmp(featureNamesCell, 'min'))
fea(strcmp(featureNamesCell, 'min')) = min(data);
end
if any(strcmp(featureNamesCell, 'mean'))
fea(strcmp(featureNamesCell, 'mean')) = mean(data);
end
if any(strcmp(featureNamesCell, 'peak'))
fea(strcmp(featureNamesCell, 'peak')) = max(data) - min(data);
end
if any(strcmp(featureNamesCell, 'arv'))
fea(strcmp(featureNamesCell, 'arv')) = mean(abs(data));
end
if any(strcmp(featureNamesCell, 'var'))
fea(strcmp(featureNamesCell, 'var')) = var(data);
end
if any(strcmp(featureNamesCell, 'std'))
fea(strcmp(featureNamesCell, 'std')) = std(data);
end
if any(strcmp(featureNamesCell, 'kurtosis'))
fea(strcmp(featureNamesCell, 'kurtosis')) = kurtosis(data);
end
if any(strcmp(featureNamesCell, 'skewness'))
fea(strcmp(featureNamesCell, 'skewness')) = skewness(data);
end
if any(strcmp(featureNamesCell, 'rms'))
fea(strcmp(featureNamesCell, 'rms')) = sqrt(mean(data.^2));
end
if any(strcmp(featureNamesCell, 'waveformF'))
fea(strcmp(featureNamesCell, 'waveformF')) = sqrt(mean(data.^2)) / mean(abs(data));
end
if any(strcmp(featureNamesCell, 'peakF'))
fea(strcmp(featureNamesCell, 'peakF')) = max(abs(data)) / sqrt(mean(data.^2));
end
if any(strcmp(featureNamesCell, 'impulseF'))
fea(strcmp(featureNamesCell, 'impulseF')) = max(abs(data)) / mean(abs(data));
end
if any(strcmp(featureNamesCell, 'clearanceF'))
fea(strcmp(featureNamesCell, 'clearanceF')) = max(abs(data)) / sqrt(mean(sqrt(abs(data))));
end
% 频域特征
if any(strcmp(featureNamesCell, 'FC'))
[Pxx, f] = periodogram(data, [], [], fs);
fea(strcmp(featureNamesCell, 'FC')) = sum(Pxx .* f) / sum(Pxx);
end
if any(strcmp(featureNamesCell, 'MSF'))
[Pxx, f] = periodogram(data, [], [], fs);
fea(strcmp(featureNamesCell, 'MSF')) = sum(Pxx .* (f.^2)) / sum(Pxx);
end
if any(strcmp(featureNamesCell, 'RMSF'))
[Pxx, f] = periodogram(data, [], [], fs);
fea(strcmp(featureNamesCell, 'RMSF')) = sqrt(sum(Pxx .* (f.^2)) / sum(Pxx));
end
if any(strcmp(featureNamesCell, 'VF'))
[Pxx, f] = periodogram(data, [], [], fs);
fea(strcmp(featureNamesCell, 'VF')) = sum(Pxx .* ((f - fea(strcmp(featureNamesCell, 'FC'))).^2)) / sum(Pxx);
end
if any(strcmp(featureNamesCell, 'RVF'))
[Pxx, f] = periodogram(data, [], [], fs);
fea(strcmp(featureNamesCell, 'RVF')) = sqrt(sum(Pxx .* ((f - fea(strcmp(featureNamesCell, 'FC'))).^2)) / sum(Pxx));
end
end
AI 代码解读
2. 测试特征提取函数
% 生成测试信号
fs = 1000; % 采样频率
t = 0:1/fs:1-1/fs; % 时间向量
data = sin(2*pi*50*t) + 0.5*randn(size(t)); % 测试信号
% 定义要提取的特征名称
featureNamesCell = {'max', 'min', 'mean', 'peak', 'arv', 'var', 'std', ...
'kurtosis', 'skewness', 'rms', 'waveformF', 'peakF', ...
'impulseF', 'clearanceF', 'FC', 'MSF', 'RMSF', 'VF', 'RVF'};
% 调用特征提取函数
fea = genFeature(data, fs, featureNamesCell);
% 显示提取的特征
disp('Extracted Features:');
disp(fea);
AI 代码解读
说明
- 该代码定义了一个函数
genFeature
,用于提取时域和频域信号的特征。用户可以通过修改featureNamesCell
来选择需要提取的特征。 - 时域特征包括最大值、最小值、均值、峰峰值、整流平均值、方差、标准差等。
- 频域特征包括重心频率、均方频率、均方根频率、频率方差、频率标准差等。
- 该代码适用于信号分析和机器学习分类问题,可以方便地扩展和修改以满足不同的需求
- 参考代码 利用matlab提取出频域和时域信号的29个特征