0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 function demo2
0013 close all
0014 fprintf('Welcome to SGWT demo #2\n');
0015
0016
0017 gb=[]; c=[];
0018
0019
0020 fh=figure('Visible','on','Name','demo 2 ui','Position',[425,920,400,150]);
0021 uipanelh=uipanel('Parent',fh,'Title','','Units','pixels','BorderType','none');
0022 tsliderh=uicontrol(uipanelh,'style','slider','max',50,'min',0,'value',1,...
0023 'sliderstep',[.005 .1],'position',[25,10,300,20],...
0024 'callback',{@tslider_callback});
0025 msliderh=uicontrol(uipanelh,'style','slider','max',100,'min',1,'value',20,...
0026 'sliderstep',[.001 .1],'position',[25,60,300,20],...
0027 'callback',{@mslider_callback});
0028 jbuttonh=uicontrol(uipanelh,'style','pushbutton','position',[50,110,150,20],...
0029 'string','Select center vertex','callback',{@jbutton_callback});
0030 ttexth=uicontrol(uipanelh,'style','text','string','','position',[325,10,100,20]);
0031 mtexth=uicontrol(uipanelh,'style','text','string','','position',[325,60,100,20]);
0032 jtexth=uicontrol(uipanelh,'style','text','string','','position',[325,100,100,20]);
0033 uicontrol(uipanelh,'style','text','string',...
0034 'Chebyshev polynomial order (m)','position',...
0035 [60,80,200,20]);
0036 uicontrol(uipanelh,'style','text','string',...
0037 'Wavelet scale (t)','position',...
0038 [60,30,200,20]);
0039
0040
0041
0042 fprintf('Loading minnesota traffic graph\n');
0043 Q=load('minnesota.mat');
0044 xy=Q.xy;
0045 A=Q.A;
0046 N=size(A,1);
0047 x=xy(:,1);
0048 y=xy(:,2);
0049
0050 fprintf('Computing graph laplacian\n')
0051 [ki,kj]=find(A);
0052 L=sgwt_laplacian(A);
0053 fprintf('Measuring largest eigenvalue, lmax = ');
0054 lmax=sgwt_rough_lmax(L);
0055 fprintf('%g\n',lmax);
0056 arange=[0 lmax];
0057
0058 msize=100;
0059
0060
0061 t=3;
0062
0063 m=20;
0064 jcenter=550;
0065
0066 fprintf('\n');
0067 update_uitext;
0068 update_graphfig
0069 update_kernel
0070 update_waveletfigs
0071
0072 function update_graphfig
0073 figure(2)
0074 set(gcf,'renderer','zbuffer');
0075 fprintf('Displaying traffic graph\n');
0076 set(gcf,'position',[0,600,400,400]);
0077
0078 hold on
0079 scatter(x,y,msize,[.5 .5 .5],'.');
0080 plot([x(ki)';x(kj)'],[y(ki)';y(kj)'],'k');
0081 set(gca,'Xtick',[]);
0082 set(gca,'Ytick',[]);
0083 axis equal
0084 axis off
0085 scatter(x(jcenter),y(jcenter),msize,'r.');
0086 drawnow
0087 end
0088
0089 function update_kernel
0090
0091 t1=1;
0092 t2=2;
0093 a=2;
0094 b=2;
0095 tmin=t1/lmax;
0096
0097
0098 set(tsliderh,'min',tmin);
0099 gb= @(x) sgwt_kernel(x,'a',a,'b',b,'t1',t1,'t2',t2);
0100 g=@(x) gb(t*x);
0101
0102 for k=1:numel(g)
0103 c=sgwt_cheby_coeff(g,m,m+1,arange);
0104 end
0105 lambda=linspace(0,lmax,1e3);
0106 figure(3)
0107 set(gcf,'position',[425,580,600,250])
0108 plot(lambda,g(lambda),lambda,sgwt_cheby_eval(lambda,c,arange));
0109 legend('Exact Wavelet kernel','Chebyshev polynomial approximation');
0110 end
0111
0112 function update_waveletfigs
0113
0114 fprintf('\nReomputing wavelets with t=%g, m=%g\n',t,m);
0115 d=sgwt_delta(N,jcenter);
0116 fprintf('Computing wavelet by naive forward transform\n');
0117 figure(4)
0118 set(gcf,'position',[0,100,400,400])
0119 wp_e=sgwt_ftsd(d,gb,t,L);
0120 show_wavelet(wp_e,x,y);
0121
0122 title('exact wavelet (naive forward transform)');
0123 fprintf('Computing wavelet by Chebyshev approximation\n');
0124 figure(5)
0125 set(gcf,'position',[425,100,400,400])
0126
0127 wp_c=sgwt_cheby_op(d,L,c,arange);
0128 show_wavelet(wp_c,x,y);
0129 title('approximate wavelet (transform via chebyshev approximation)');
0130 relerr=norm(wp_e-wp_c)/norm(wp_e);
0131 fprintf('Relative error between exact and approximate wavelet %g\n',relerr)
0132 end
0133
0134 function show_wavelet(wp,x,y)
0135 [Fs,s_ind]=sort(abs(wp),'descend');
0136 scatter(x(s_ind),y(s_ind),msize,wp(s_ind),'.');
0137 caxis([-1 1]*max(abs(wp)));
0138 hcb=colorbar('location','north');
0139 set(gca,'Xtick',[]);
0140 set(gca,'Ytick',[]);
0141 cxt=get(hcb,'Xtick');
0142 cxt=[cxt(1),0,cxt(end)];
0143 set(hcb,'Xtick',cxt);
0144 cpos=get(hcb,'Position');
0145 cpos(4)=.02;
0146 set(hcb,'Position',cpos);
0147 axis equal
0148 axis off
0149 end
0150
0151 function update_uitext
0152 set(ttexth,'string',sprintf('t=%0.3f',t));
0153 set(mtexth,'string',sprintf('m=%g',m));
0154 set(jtexth,'string',sprintf('j=%g',jcenter));
0155 end
0156
0157 function tslider_callback(source,eventdata)
0158 t=get(tsliderh,'value');
0159 update_uitext;
0160 update_kernel;
0161 update_waveletfigs;
0162 end
0163
0164 function mslider_callback(source,eventdata)
0165 newm=get(msliderh,'value');
0166 if newm<m
0167 m=floor(newm);
0168 else
0169 m=ceil(newm);
0170 end
0171 set(msliderh,'value',m);
0172 update_uitext;
0173 update_kernel;
0174 update_waveletfigs;
0175 end
0176
0177 function jbutton_callback(source,eventdata)
0178 figure(2)
0179 fprintf('Select new center vertex\n');
0180 [xp,yp]=ginput(1);
0181 oldjcenter=jcenter;
0182 jcenter=argmin((xp-x).^2+(yp-y).^2);
0183 scatter(x(jcenter),y(jcenter),msize,'r.');
0184 scatter(x(oldjcenter),y(oldjcenter),msize,[.5 .5 .5],'.');
0185 drawnow
0186 update_uitext
0187 update_waveletfigs
0188 end
0189
0190 end