clear
clc
%路径y,采样点距为t
% t=0:0.01:2;
% y=0.2*sin(2*pi*3*t)+0.8*(t-3).^2;
t=0:0.01:6;
% % y=10*sin(2*pi*t);
y=[];
for i=1:length(t)
if(t(i)<2)
y(i)=0;
elseif (t(i)<4)
y(i)=10;
else
y(i)=0;
end
end
ymin=min(y);ymax=max(y);
h=figure(1);
subplot(3,1,1);plot(y);title('noised signal');
axis([0,600,ymin,ymax]);
axis off
%转换为二值图像表示
n=length(y);
avg=(ymax-ymin)/n; %获得Y轴的比例尺
z=round(y./avg);
m=max(z)-min(z)+1;
I=zeros(m,n);
for i=1:n
for j=1:max(z)-z(i)
I(j,i)=1;
end
end
subplot(3,1,2);
imagesc(I);title('转化为图像');axis off
%使用matlab中的Image Processing
SE=strel('disk',30,4);%定义一个圆盘膨胀结构元素
J=imerode(I,SE);
subplot(3,1,3);imagesc(J);title('形态学滤波');
axis off
out_y=[];
[M,N]=size(J);
for k=1:N
for l=1:M
if (J(l,k)==0)
out_y(k)=(M-l).*avg;
break
end
end
end
h1=figure(2);
plot(t,out_y,'b',t,y,'k');
axis([0,max(t),min(y),max(y)]); |