test_TE.m
资源名称:KPCA.rar [点击查看]
上传用户:nxf103
上传日期:2017-12-04
资源大小:2690k
文件大小:3k
源码类别:
能源行业(电力石油煤炭)
开发平台:
Matlab
- global ks;%核函数半径
- load x00;
- load t14;
- ks=50;
- train_num = 960;% 训练样本数
- fault_num = 800;
- variable_num=16;
- PCA_num=5;%选取最大主元数
- K=3;%KPCA选取最大主元数
- %%%%%对建模样本作归一化处理%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- [n,m]=size(x00);
- E=mean(x00);
- Train=zeros(n,m);
- for j=1:m,
- V(j)=0;
- for i=1:n,
- V(j)=V(j)+(x00(i,j)-E(j))^2;
- end
- V(j)=sqrt(V(j)/(n-1));
- for i=1:n,
- Train(i,j)=(x00(i,j)-E(j))/V(j);
- end
- end
- %%%%%对故障测试样本作归一化处理%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- [n1,m1]=size(t14);
- fault=zeros(n1,m1);
- for j=1:m1,
- for i=1:n1,
- fault(i,j)=(t14(i,j)-E(j))/V(j);
- end
- end
- %%%%% %主元分析%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- [pc,score,latent,tsquare]=princomp(Train);
- PCA=pc(:,1:PCA_num);
- score_PCA=score(:,1:PCA_num);
- latent_PCA=diag(latent(1:PCA_num));
- %%%%% %新测试数据(故障数据)作主元分析%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- PCA_Test=zeros(n1,PCA_num);
- Fault_guji=zeros(n1,m1);
- TT=zeros(1,n1);
- for i=1:n1,
- for j=1:PCA_num,
- PCA_Test(i,j)=fault(i,:)*PCA(:,j);
- Fault_guji(i,:)=Fault_guji(i,:)+fault(i,:)*PCA(:,j)*PCA(:,j)';
- end
- %求T2统计
- TT(i)=PCA_Test(i,:)*inv(latent_PCA)*PCA_Test(i,:)';
- %求SPE统计
- end
- Fault_ee=fault-Fault_guji;
- for i=1:n1,
- Fault_SPE(i)=Fault_ee(i,:)*Fault_ee(i,:)';
- end
- figure(1);
- subplot(2,1,1);
- plot(1:960,TT,1:8:960,20,'k--');
- xlabel('Sample Number');
- ylabel('PCA-T2');
- subplot(2,1,2);
- plot(1:960,Fault_SPE,1:8:960,30,'k--');
- xlabel('Sample Number');
- ylabel('PCA-SPE');
- %%%%% %Kernel主元分析%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %x00=[x00(1,:); x00(80,:);x00(120,:)];
- [tr,meanp,stdp] = prestd(x00');
- te = trastd(t14',meanp,stdp);
- tr=tr';
- te=te';
- trd=data(tr);
- ted=data(te);
- %[r,a]=train(kpca,tr);
- [r,a]=train(kpca({kernel('rbf',ks)}),trd);
- d=test(a,ted);
- val=a.e_val;
- vec=a.e_vec;
- dvec=d.X;
- s=inv(diag(val(1:K)));
- for i=1:960
- tsquare(i)=dvec(i,1:K)*s*dvec(i,1:K)';
- SPE(i)=dvec(i,:)*dvec(i,:)'-dvec(i,1:K)*dvec(i,1:K)';
- end
- figure(2);
- subplot(2,1,1);
- plot(1:960,tsquare,1:8:960,0.00,'k--');
- xlabel('Sample Number(N)');
- ylabel('KPCA-T2');
- subplot(2,1,2);
- plot(1:960,SPE,1:8:960,0.00,'k--');
- xlabel('Sample Number(N)');
- ylabel('KPCA-SPE');
- %[cn,CSPE]=kpcak(te,tr,vec(:,1:K),s,ks);
- %figure(3)
- %subplot(2,1,1);
- %plot(cn);
- %xlabel('Sample Number(N)');
- %ylabel('CT2');
- %subplot(2,1,2);
- %plot(CSPE);
- %xlabel('Sample Number(N)');
- %ylabel('CSPE');