美文网首页
代码初次演示

代码初次演示

作者: XSeventrap | 来源:发表于2017-11-23 14:44 被阅读0次

step1

读取数据,将数据归一化处理

clear all;
clc;
clf;
data_original=xlsread('data2015.xlsx','0701');%读取原始数据AD值
stem_water=data_original(:,5);%读取茎体水分AD值
stem_water_1=data_original(:,5);%读取茎体水分AD值
stem_water=(stem_water-min(stem_water))/(max(stem_water)-min(stem_water));%归一化
stem_water_1=stem_water_1/4096*3.3;%AD转换电压值
figure(1)
plot(stem_water)
xlabel('采样点');
ylabel('归一值')
title('植物茎体原始水分信号')
figure(2)
plot(stem_water_1)
xlabel('采样点');
ylabel('电压值')
title('植物茎体原始水分信号')

step2

将数据进行傅立叶变换,取其中一半数据并取模值
再进行降序排列,设置稀疏度Needed
进行傅立叶逆变换,保存为xx

%%
%FFT稀疏表示
x=stem_water;
N=length(x);
y=fft(x);%fft变换
mag=abs(y);%取模
figure(3)
plot(mag/N*2,'ro')
[Y,ind]=sort(mag,'descend');%投影系数降序排列
hold on
plot(Y/N*2,'LineWidth',2)
xlabel('频率');
ylabel('幅值');
title('fft变换');
legend('fft变换系数','fft变换系数降序')
axis tight
hold off
Needed=length(find(Y>2));%设置稀疏度K
Needed=16;
y(ind(Needed+1:end))=0;%系数稀疏化
xx=real(ifft(y));%fft逆变换
figure(4)
plot(xx)
xlabel('采样点');
ylabel('归一值');
title('ifft重构后水分信号')
original_wc=norm(xx-x)/norm(x)%稀疏化后的误差
figure(5)
plot(x);
hold on
plot(xx,'r','LineWidth',2);
xlabel('采样点');
ylabel('归一值');
title('原始信号与重构信号');
legend('原始信号','ifft重构信号');

step3

正交匹配追踪算法回复原始数据

%正交匹配追踪重构信号
K=Needed;%设置稀疏度K
% M=ceil(K*log(N/K));%设置观测数据长度
M=61;
CNT=1000;
wc_x=zeros(1,CNT);
for j=1:CNT
    Phi=randn(M,N)/sqrt(M);
    Psi=fft(eye(N))^(-1);
    A=Phi*Psi;
    gc_x=Phi*xx;
   [r_n,theta]= CS_OMP(gc_x,A,K);
    hf_x=Psi*theta;
    hf_x=real(hf_x);
    wc_x(j)=norm(hf_x-xx)/norm(xx);
end
average_wc=sum(wc_x)/length(wc_x)
wc_1=zeros(1,CNT);
wc_1(:)=original_wc;
wc_2=zeros(1,CNT);
wc_2(:)=average_wc;
figure(6)
plot(x);
hold on
plot(hf_x,'r','LineWidth',2);
xlabel('采样点');
ylabel('归一值');
title('原始信号与重构信号')
legend('原始信号','OMP重构信号');

step4

做误差分析

%%
figure(7)
plot(wc_x)
hold on
plot(wc_1,'r','LineWidth',3)
plot(wc_2,'c','LineWidth',3)
xlabel('采样点');
ylabel('归一值');
legend('每次重构误差','稀疏化原始误差','1000次重构平均误差')
title('重构误差')
hold off
figure(8)
stem(gc_x)
xlabel('观测点');
ylabel('幅值');
% title('观测信号')
box off
figure(9)
plot(real(y),'o')
hold on
plot(real(theta),'r*')
xlabel('特征向量');
ylabel('幅值');
title('稀疏表示和压缩重构的投影系数')
legend('稀疏表示系数','压缩重构系数')
figure(10)
plot(xx,'b','LineWidth',2)
hold on
plot(hf_x,'r-.','LineWidth',2)
xlabel('采样点');
ylabel('归一化输出电压');
% title('重构信号')
box off
legend('DFT重构信号','OMP重构信号')
image.png
image.png

相关文章

网友评论

      本文标题:代码初次演示

      本文链接:https://www.haomeiwen.com/subject/wajzvxtx.html