Home > sgwt_toolbox > demo > demo2.m

demo2

PURPOSE ^

demo2 : Allows exploring wavelet scale and approximation accuracy

SYNOPSIS ^

function demo2

DESCRIPTION ^

 demo2 : Allows exploring wavelet scale and approximation accuracy

 This demo builds the SGWT for the minnesota traffic graph, a graph
 representing the connectivity of the minnesota highway system. One center
 vertex is chosen, and then the exact (naive forward transform) and the
 approximate (via chebyshev polynomial approximation) wavelet transforms
 are computed for a particular value of the wavelet scale t. The relative
 error of the exact and approximate wavelets is computed. The user may
 then adjust the value of t, the degree m of the chebyshev polynomial
 approximation, and the center vertex in order to explore their effects.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 % demo2 : Allows exploring wavelet scale and approximation accuracy
0002 %
0003 % This demo builds the SGWT for the minnesota traffic graph, a graph
0004 % representing the connectivity of the minnesota highway system. One center
0005 % vertex is chosen, and then the exact (naive forward transform) and the
0006 % approximate (via chebyshev polynomial approximation) wavelet transforms
0007 % are computed for a particular value of the wavelet scale t. The relative
0008 % error of the exact and approximate wavelets is computed. The user may
0009 % then adjust the value of t, the degree m of the chebyshev polynomial
0010 % approximation, and the center vertex in order to explore their effects.
0011 
0012 function demo2
0013 close all
0014 fprintf('Welcome to SGWT demo #2\n');
0015 
0016 % touch variables to be shared among sub-functions
0017 gb=[]; c=[]; 
0018 
0019 % create UI elements
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 %% Load graph and compute Laplacian
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 % initial values
0061 t=3; % wavelet scale
0062 
0063 m=20; % chebyshev polynomial order, for approximation
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   %clf('reset');
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   % select wavelet kernel
0091   t1=1;
0092   t2=2;
0093   a=2;
0094   b=2;
0095   tmin=t1/lmax; 
0096 % scales t<tmin will show same wavelet shape as t=tmin, as
0097 % wavelet kernel g is monomial in interval [0,1)
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   % polynomial approximation
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   % show wavelet (naive)
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   % show wavelet (chebyshev)
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; % make colorbar thinner
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

Generated on Fri 30-Apr-2010 17:49:57 by m2html © 2003