⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cnvplt4n.m

📁 很多matlab的源代码
💻 M
字号:
function cnvplt4n(xs,ys,tx,ty,ts,mode)
%Support file for CONVPLOT for use with MATLAB 4.0 and above


% ADSP Toolbox: Version 2.0 
% For use with "Analog and Digital Signal Processing", 2nd Ed.
% Published by PWS Publishing Co.
%
% Ashok Ambardar, EE Dept. MTU, Houghton, MI 49931, USA
% http://www.ee.mtu/faculty/akambard.html
% e-mail: akambard@mtu.edu
% Copyright (c) 1998



if nargin < 6, % command-line call to function
 ta=min(tx):ts:max(tx);
 tb=min(ty):ts:max(ty);


  t=ta;
  x=eval(xs);
  t1=t;
  t=tb;
  y=eval(ys);
  t2=t;
 
 d1=max(t1)-min(t1);
 d2=max(t2)-min(t2);
% ddif=abs(d2-d1);
 
 tst=min(t1)+min(t2);
 tend=max(t1)+max(t2);
 t3=min(t1)+(0:ts:tend-tst);
% mint=min(t1)-d2;
 t1=tst+(0:ts:d1);
 t3=(0:ts:d1+d2)+tst;  %%%NEW
 z=ts*conv(x,y);
 yf=fliplr(y);

%For products
 ze=0*t3;
 tp=ze;
 lx=length(x);
 ly=length(y);
% lmax=max(lx,ly);
% lmin=min(lx,ly);
 tp(1,1:lx)=t1;
%tp=t1(1)+(0:length(tp)-1)*ts;
 xp=ze;
 yp=ze;
 xp(1,1:lx)=x;
 yp(1,1:ly)=y;


%For plotting product function
 limits=[max(x);max(y);min(x);min(y);max(x)*max(y);min(x)*min(y);min(z);max(z)];
 limits=[limits;max(x)*min(y);min(x)*max(y)];%%NEW
 prmax=max(limits);
 prmin=min(limits);
 if prmin>0,
  prmin=0;
 end

 l=length(t3);
 t1p=[t1(1) t1 t1(lx)];
 x1p=[0 x 0];
 t2f=(0:ts:d2)-d2+tst;
 mm=50;
 tj=max([1 fix(l/mm)]);

% setup matrix to store information necessary for the mouse motion option
 info_mat = zeros(6,l);
 info_mat(1,:) = t3;
 info_mat(2,:) = z;
 info_mat(3,:) = xp;
 info_mat(4,:) = yp;
 info_mat(5,:) = tp;
 info_mat(6,:) = [lx ly zeros(1,l-2)];

% setup figure and plot function which does not get shifted
 subplot,clf
 plot(t1p,x1p,'g'); 
 axis([tst-d2 tend prmin-(0.1*abs(prmin)) prmax+(0.1*abs(prmax))]);

% annotate graph
 title(['Product(cyan), Conv(yellow) starts at t = ' num2str(tst)]);
 xlabel('time of convolution');grid on;hold on;

% setup line and marker which represent function being shifted
 shift_line=plot([t2f(1),t2f,t2f(length(t2f))],[0,yf,0],'color','r','erase','xor',...
      'buttondownfcn','cnvplt4n([],[],[],[],[],1);');
 shift_mark=plot(t1(1),0,'.','color','g','erase','xor','markersize',20);
 plot([t2f(1),t2f,t2f(length(t2f))],[0,yf,0],'color','m','linestyle',':','erase','xor');

% setup line and marker which represent convolution of the two functions
% conv_line=line('color','y','erase','xor','xdata',[],'ydata',[]);
 conv_line=line('color','y','erase','xor','linewidth',2,'xdata',[],'ydata',[]);
 conv_mark=plot(t3(1),z(1),'.','color','m','erase','xor','markersize',20);

% setup line which represents product of the two functions
% prod_line=line('color','c','erase','xor','xdata',[],'ydata',[]);
% prod_line=line('color','c','erase','xor','linewidth',2,'xdata',[],'ydata',[]);
 prod_line=patch(0,0,'c');
set(prod_line,'erase','xor');

% store handles and data necessary for mouse motion option
 set(shift_line,'userdata',info_mat);
 set(shift_mark,'userdata',t2f);
 set(conv_line,'userdata',ts);
 set(conv_mark,'userdata',lx);
% set(conv_mark,'userdata',lmax);
 set(gcf,'userdata',[shift_line, shift_mark, conv_line, conv_mark, prod_line],...
         'windowbuttonupfcn','set(gcf,''windowbuttonmotionfcn'','''')');
 
%Start of movie
for j=[1:tj:l l],
  ta=t2f+(j-1)*ts;
  la=length(ta);
  tb=t3(j);
  zb=z(j);
  
% Compute product line
  xp1=xp(1:j);
  yp1=fliplr(yp(1:j));
  pr=xp1.*yp1;
%  tpr=tp(1:j);
  ind1=max(1, j-ly+1);
  ind2=min(j,lx);
  tpr=tp(ind1:ind2);
  pr=pr(ind1:ind2);
  lp=length(tpr);if lp==1,pr=0;end

% update shifted, conv and product lines
  set(shift_line,'xdata',[ta(1),ta,ta(la)]);
  set(shift_mark,'xdata',tb);
  set(prod_line,'xdata',[tpr(1) tpr tpr(lp)],'ydata',[0 pr 0]);

  set(conv_line,'xdata',t3(1:j),'ydata',z(1:j));
  set(conv_mark,'xdata',tb,'ydata',zb);
  drawnow;
end

hold off;
disp('   YOU MAY NOW USE THE MOUSE TO SHIFT THE FOLDED (RED) FUNCTION')
return;

elseif mode==1, % initial grab

 handles = get(gcf,'userdata');
 xdata = get(handles(1),'xdata');
 info_mat = get(handles(1),'userdata');
 t3 = info_mat(1,:);
 
 t3_loc = find(t3==xdata(length(xdata)));
 init_pt = get(gca,'currentpoint');

 set(handles(5),'userdata',[init_pt;t3_loc,0,0]);
 set(gcf,'windowbuttonmotionfcn','cnvplt4n([],[],[],[],[],2);');
 
elseif mode==2, % mouse motion function

% obtain handles from current figure's userdata
 handles = get(gcf,'userdata');
 shift_line = handles(1);
 shift_mark = handles(2);
 conv_line = handles(3);
 conv_mark = handles(4);
 prod_line = handles(5);

% obtain convolution data
 info_mat = get(shift_line,'userdata');
 t2f = get(shift_mark,'userdata');
 ts = get(conv_line,'userdata');
 lmax = get(conv_mark,'userdata');
 init_pt = get(prod_line,'userdata');
 t3_loc = init_pt(3,1);
 t3 = info_mat(1,:);
 z = info_mat(2,:);
 xp = info_mat(3,:);
 yp = info_mat(4,:);
 tp = info_mat(5,:);
 lx=info_mat(6,1);
 ly=info_mat(6,2);
 
 cur_pt = get(gca,'currentpoint');
 delta_x = cur_pt(1,1) - init_pt(1,1);
 j = find(t3>=(t3(t3_loc)+delta_x));
 if length(j),
  j = j(1);
 else
  j = length(t3);
 end
 
 ta=t2f+(j-1)*ts;
 la=length(ta);
 tb=t3(j);
 zb=z(j);
 
% Compute product line
 xp1=xp(1:j);
 yp1=fliplr(yp(1:j));
 pr=xp1.*yp1;
 ind1=max(1, j-ly+1);
 ind2=min(j,lx);
 tpr=tp(ind1:ind2);
 pr=pr(ind1:ind2);
 lp=length(tpr);
 if lp==1,pr=0;end

% update shifted and convolved lines
 set(shift_line,'xdata',[ta(1),ta,ta(la)]);
 set(shift_mark,'xdata',tb);
 set(prod_line,'xdata',[tpr(1) tpr tpr(lp)],'ydata',[0 pr 0]);

 set(conv_line,'xdata',t3(1:j),'ydata',z(1:j));
 set(conv_mark,'xdata',tb,'ydata',zb);
 drawnow;
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -