编写matlab代码1故障轴承诊断2西储大学数据集3栈式稀疏自编码器4绘制混淆矩阵
时间: 2023-12-29 10:04:57 浏览: 125
matlab-基于信号稀疏恢复算法的轴承故障诊断matlab仿真-源码
5星 · 资源好评率100%
以下是针对您的问题编写的 MATLAB 代码:
1. 故障轴承诊断:
```matlab
% 加载数据
load('bearing.mat')
% 绘制时域图像
figure
plot(t, bearing)
title('时域图像')
% 计算FFT
Y = fft(bearing);
L = length(bearing);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L;
% 绘制频域图像
figure
plot(f, P1)
title('单侧幅值谱')
xlabel('频率(Hz)')
ylabel('幅值')
```
2. 西储大学数据集:
```matlab
% 加载数据
load('data.mat')
% 随机排列数据
idx = randperm(size(X, 1));
X = X(idx, :);
Y = Y(idx, :);
% 划分数据集
train_ratio = 0.7;
val_ratio = 0.15;
test_ratio = 0.15;
[trainInd,valInd,testInd] = divideblock(size(X, 1),train_ratio,val_ratio,test_ratio);
% 训练模型
net = feedforwardnet([10, 5]);
net = train(net, X(trainInd, :)', Y(trainInd, :)');
% 测试模型
Y_pred = net(X(testInd, :)');
acc = sum(round(Y_pred) == Y(testInd, :)') / length(testInd);
disp(['测试集准确率为:', num2str(acc)])
```
3. 栈式稀疏自编码器:
```matlab
% 加载数据
load('mnist.mat')
% 划分数据集
train_ratio = 0.7;
val_ratio = 0.15;
test_ratio = 0.15;
[trainInd,valInd,testInd] = divideblock(size(X, 1),train_ratio,val_ratio,test_ratio);
% 训练模型
hiddenSize1 = 200;
autoenc1 = trainAutoencoder(X(trainInd, :)', hiddenSize1, ...
'MaxEpochs', 400, 'L2WeightRegularization', 0.004, ...
'SparsityRegularization', 4, 'SparsityProportion', 0.15, ...
'ScaleData', false);
hiddenSize2 = 100;
autoenc2 = trainAutoencoder(encode(autoenc1, X(trainInd, :)'), hiddenSize2, ...
'MaxEpochs', 100, 'L2WeightRegularization', 0.002, ...
'SparsityRegularization', 4, 'SparsityProportion', 0.1, ...
'ScaleData', false);
softnet = trainSoftmaxLayer(encode(autoenc2, X(trainInd, :)'), Y(trainInd, :)');
deepnet = stack(autoenc1, autoenc2, softnet);
% 测试模型
Y_pred = deepnet(X(testInd, :)');
acc = sum(vec2ind(Y_pred) == vec2ind(Y(testInd, :)')) / length(testInd);
disp(['测试集准确率为:', num2str(acc)])
```
4. 绘制混淆矩阵:
```matlab
% 计算混淆矩阵
C = confusionmat(vec2ind(Y(testInd, :)'), vec2ind(Y_pred))
% 绘制混淆矩阵图像
figure
confusionchart(C)
```
希望这些代码对您有所帮助!
阅读全文