1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
| function varargout=wtc(x,y,varargin)
-validate and reformat timeseries.
[x,dt]=formatts(x);
[y,dty]=formatts(y);
if dt~=dty
error('timestep must be equal between time series')
end
t=(max(x(1,1),y(1,1)):dt:min(x(end,1),y(end,1)))'; %common time period
if length(t)<4
error('The two time series must overlap.')
end
n=length(t);
%----------default arguments for the wavelet transform-----------
Args=struct('Pad',1,... % pad the time series with zeroes (recommended)
'Dj',1/12, ... % this will do 12 sub-octaves per octave
'S0',2*dt,... % this says start at a scale of 2 years
'J1',[],...
'Mother','Morlet', ...
'MaxScale',[],... %a more simple way to specify J1
'MakeFigure',(nargout==0),...
'MonteCarloCount',10000,...
'BlackandWhite',1,...
'AR1',[0 0],...
'ArrowDensity',[30 30],...
'ArrowSize',0,...
'ArrowHeadSize',0);
Args=parseArgs(varargin,Args,{'BlackandWhite'});
if isempty(Args.J1)
if isempty(Args.MaxScale)
Args.MaxScale=(n*.17)*2*dt; %auto maxscale
end
Args.J1=round(log2(Args.MaxScale/Args.S0)/Args.Dj);
end
ad=mean(Args.ArrowDensity);
Args.ArrowSize=Args.ArrowSize*30*.03/ad;
Args.ArrowHeadSize=Args.ArrowHeadSize*Args.ArrowSize*220;
if ~strcmpi(Args.Mother,'morlet')
warning('Smoothing operator is designed for morlet wavelet.')
end
if strcmpi(Args.AR1,'auto')
Args.AR1=[ar1nv(x(:,2)) ar1nv(y(:,2))];
if any(isnan(Args.AR1))
error('Automatic AR1 estimation failed. Specify it manually (use arcov or arburg).')
end
end
nx=size(x,1);
sigmax=std(x(:,2));
ny=size(y,1);
sigmay=std(y(:,2));
%-----------:::::::::::::--------- ANALYZE ----------::::::::::::------------
[X,period,scale,coix] = wavelet(x(:,2),dt,Args.Pad,Args.Dj,Args.S0,Args.J1,Args.Mother);
[Y,period,scale,coiy] = wavelet(y(:,2),dt,Args.Pad,Args.Dj,Args.S0,Args.J1,Args.Mother);
%Smooth X and Y before truncating! (minimize coi)
sinv=1./(scale');
sX=smoothwavelet(sinv(:,ones(1,nx)).*(abs(X).^2),dt,period,Args.Dj,scale);
sY=smoothwavelet(sinv(:,ones(1,ny)).*(abs(Y).^2),dt,period,Args.Dj,scale);
% truncate X,Y to common time interval (this is first done here so that the coi is minimized)
dte=dt*.01; %to cricumvent round off errors with fractional timesteps
idx=find((x(:,1)>=(t(1)-dte))&(x(:,1)<=(t(end)+dte)));
X=X(:,idx);
sX=sX(:,idx);
coix=coix(idx);
idx=find((y(:,1)>=(t(1))-dte)&(y(:,1)<=(t(end)+dte)));
Y=Y(:,idx);
sY=sY(:,idx);
coiy=coiy(idx);
coi=min(coix,coiy);
% -------- Cross wavelet -------
Wxy=X.*conj(Y);
% ----------------------- Wavelet coherence ---------------------------------
sWxy=smoothwavelet(sinv(:,ones(1,n)).*Wxy,dt,period,Args.Dj,scale);
Rsq=abs(sWxy).^2./(sX.*sY);
if (nargout>0)||(Args.MakeFigure)
wtcsig=wtcsignif(Args.MonteCarloCount,Args.AR1,dt,length(t)*2,Args.Pad,Args.Dj,Args.S0,Args.J1,Args.Mother,.6);
wtcsig=(wtcsig(:,2))*(ones(1,n));
wtcsig=Rsq./wtcsig;
end
if Args.MakeFigure
Yticks = 2.^(fix(log2(min(period))):fix(log2(max(period))));
if Args.BlackandWhite
levels = [0 .5 .6 .7 .8 .9 1];
levels = [0 .7 .75 .8 .85 .9 .95 1];
% levels = [-1 -.7 -.5 .0 .5 .9 .95 1];
[cout,H]=safecontourf(t,log2(period),Rsq,levels);
colorbarf(cout,H)
cmap=[0 1;.5 .9;.6 .8;.7 .7;.8 .6;.9 .5;1 .4];
cmap=interp1(cmap(:,1),cmap(:,2),(0:.1:1)');
% cmap=[0 1;.7 .9;.75 .8;.8 .7;.85 .6;.9 .5;.95 .4;1 .3];
% cmap=interp1(cmap(:,1),cmap(:,2),(0:.05:1)');
cmap=cmap(:,[1 1 1]);
colormap(cmap)
set(gca,'YLim',log2([min(period),max(period)]), ...
'YDir','reverse', 'layer','top', ...
'YTick',log2(Yticks(:)), ...
'YTickLabel',num2str(Yticks'), ...
'layer','top')
ylabel('Frequency (in years)')
xlabel('Time')
hold on
%phase plot
aWxy=angle(Wxy);
lowRidx=find(Rsq<.5); %remove phase indication where Rsq is low
aaa=aWxy;
aaa(lowRidx)=NaN;
%[xx,yy]=meshgrid(t(1:5:end),log2(period));
phs_dt=round(length(t)/30); tidx=max(floor(phs_dt/2),1):phs_dt:length(t);
phs_dp=round(length(period)/30); pidx=max(floor(phs_dp/2),1):phs_dp:length(period);
% phaseplot(t(tidx),log2(period(pidx)),aaa(pidx,tidx),Args.ArrowSize,Args.ArrowHeadSize);
if ~all(isnan(wtcsig))
[c,h] = contour(t,log2(period),wtcsig,[1 1],'k');
set(h,'linewidth',3) %
end
%suptitle([sTitle ' coherence']);
% plot(t,log2(coi),'k','linewidth',2,'LineStyle','--') %
hold off
else
H=imagesc(t,log2(period),Rsq);
%[c,H]=safecontourf(t,log2(period),Rsq,[0:.05:1]);
%set(H,'linestyle','none')
set(gca,'clim',[0 1])
HCB=safecolorbar;
set(gca,'YLim',log2([min(period),max(period)]), ...
'YDir','reverse', 'layer','top', ...
'YTick',log2(Yticks(:)), ...
'YTickLabel',num2str(Yticks'), ...
'layer','top')
ylabel('')
hold on
%phase plot
aWxy=angle(Wxy);
lowRidx=find(Rsq<.5); %remove phase indication where Rsq is low
aaa=aWxy;
aaa(lowRidx)=NaN;
%[xx,yy]=meshgrid(t(1:5:end),log2(period));
phs_dt=round(length(t)/30); tidx=max(floor(phs_dt/2),1):phs_dt:length(t);
phs_dp=round(length(period)/30); pidx=max(floor(phs_dp/2),1):phs_dp:length(period);
phaseplot(t(tidx),log2(period(pidx)),aaa(pidx,tidx),Args.ArrowSize,Args.ArrowHeadSize);
if ~all(isnan(wtcsig))
[c,h] = contour(t,log2(period),wtcsig,[1 1],'k');
set(h,'linewidth',2)
end
%suptitle([sTitle ' coherence']);
tt=[t([1 1])-dt*.5;t;t([end end])+dt*.5];
hcoi=fill(tt,log2([period([end 1]) coi period([1 end])]),'w');
set(hcoi,'alphadatamapping','direct','facealpha',.5)
hold off
end
end
varargout={Rsq,period,scale,coi,wtcsig};
varargout=varargout(1:nargout);
12345678910111213141516 | function [cout,H]=safecontourf(varargin)
vv=sscanf(version,'%i.');
if (version('-release')<14)|(vv(1)<7)
[cout,H]=contourf(varargin{:});
else
[cout,H]=contourf('v6',varargin{:});
end
function hcb=safecolorbar(varargin)
vv=sscanf(version,'%i.');
if (version('-release')<14)|(vv(1)<7)
hcb=colorbar(varargin{:});
else
hcb=colorbar('v6',varargin{:});
end |
Je vous communique également le code responsable du colorbar:
function hcb=colorbarf(cout,H,loc)
changeNextPlot = 1;
% colorbar must have output parameters from the contourf function
if (nargin < 2)
error('colorbarf requires a minimum of two input parameters');
end
% default location for colorbar
if nargin<3, loc = 'vert'; end
% if an axes handle is passed for the location
ax = [];
if nargin==3,
if ishandle(loc)
ax = loc;
if ~strcmp(get(ax,'type'),'axes'),
error('Requires axes handle.');
end
units = get(ax,'units'); set(ax,'units','pixels');
rect = get(ax,'position'); set(ax,'units',units)
if rect(3) > rect(4), loc = 'horiz'; else loc = 'vert'; end
changeNextPlot = 0;
end
end
h = gca;
if (nargin == 2)
% Search for existing colorbar so that we can plot over it if we find it
ch = get(findobj(gcf,'type','axes','tag','Colorbar')); ax = [];
for i=1:length(ch),
ud = ch(i).UserData; % ch.UserData changed to ch(i).UserData - suggested by P. Brickley
d = ud.PlotHandle;
if prod(size(d))==1 && isequal(d,h),
ax = findobj(gcf,'type','axes','tag','Colorbar');
pos = ch.Position;
if pos(3)<pos(4), loc = 'vert'; else loc = 'horiz'; end
changeNextPlot = 0;
break;
end
end
elseif ((nargin == 3) && (~ishandle(loc)))
% Search for existing colorbar so that we can plot over it if we find it
ch = get(findobj(gcf,'type','axes','tag','Colorbar')); ax = [];
for i=1:length(ch),
ud = ch(i).UserData; % ch.UserData changed to ch(i).UserData - suggested by P. Brickley
d = ud.PlotHandle;
if prod(size(d))==1 && isequal(d,h),
ax = findobj(gcf,'type','axes','tag','Colorbar');
pos = ch.Position;
if pos(3)<pos(4), loc = 'vert'; else loc = 'horiz'; end
changeNextPlot = 0;
break;
end
end
end
origNextPlot = get(gcf,'NextPlot');
if strcmp(origNextPlot,'replacechildren') || strcmp(origNextPlot,'replace'),
set(gcf,'NextPlot','add')
end
%%%%%%%%%%%%%% Filled Colorbar %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Blair Greenan Dec 16, 1998
% strip out the information about the contours from the output of the contourf
% function
i = 1;
while ~isempty(cout)
C1(i) = cout(1,1); % contour level C1(i) = cout(1,1)
N1 = cout(2,1); % number of points in contour N1 = cout(2,1)
% Version 1.4 removed for loop to speed things up
cout(:,1:N1+1) = []; % shrink the matrix
if (N1 > 1) % has to be more than 1 point in contour to be valid ***VERSION 1.5 to accomodate MATLAB6****
i = i + 1;
end
end
C2 = unique(C1); % find the unique contour levels and sort
numLevels = length(C2);
if strcmp(get(H,'type'),'hggroup') %added by Aslak Grinsted to ensure v7 compatibility
H=get(H,'children');
end
for j = 1:length(H)
colors(j) = get(H(j),'CData'); % get the color used to fill the patches
%set(H(j),'CData')
end
colors = unique(colors); % create a list of unique colors used in fills
minc = min(colors);
maxc = max(colors);
colors(isnan(colors))=[];
if ((length(colors)-numLevels)>1)
% chop off extra colors that don't have a corresponding contour level
colors(1:((length(colors)-numLevels)-1))=[];
end
% special case where no mimima is enclosed by a contour
if (length(colors) == numLevels)
colors(length(colors)+1)=NaN;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if loc(1)=='v', % Append vertical scale to right of current plot
if isempty(ax),
units = get(h,'units'); set(h,'units','normalized')
pos = get(h,'Position');
[az,el] = view;
stripe = 0.075; edge = 0.02;
if all([az,el]==[0 90]), space = 0.05; else space = .1; end
set(h,'Position',[pos(1) pos(2) pos(3)*(1-stripe-edge-space) pos(4)])
rect = [pos(1)+(1-stripe-edge)*pos(3) pos(2) stripe*pos(3) pos(4)];
ud.origPos = pos;
% Create axes for stripe and
% create DeleteProxy object (an invisible text object in
% the target axes) so that the colorbar will be deleted
% properly.
ud.DeleteProxy = text('parent',h,'visible','off',...
'tag','ColorbarDeleteProxy',...
'handlevisibility','off',...
'deletefcn','eval(''delete(get(gcbo,''''userdata''''))'','''')');
ax = axes('Position', rect,'Tag','TMW_COLORBAR');
set(ud.DeleteProxy,'userdata',ax)
set(h,'units',units)
else % if colobar axes already exist
axes(ax);
ud = get(ax,'userdata');
end
% Create color stripe by drawing the appropriate number of filled rectangles
if any(isnan(colors)) % if the bottom level is not filled
for j = 1:length(C2)+1
k = j - 1; % don't fill bottom rectangle on colorbar
x(1) = 0;
x(2) = 1;
x(3) = 1;
x(4) = 0;
y(1) = (j-1)*(1/(length(C2)+1));
y(2) = (j-1)*(1/(length(C2)+1));
y(3) = j*(1/(length(C2)+1));
y(4) = j*(1/(length(C2)+1));
if (k == 0)
hfill = fill(x,y,colors(length(colors))); % fill with NaN
else
hfill = fill(x,y,colors(k));
end
hold on
end
else %we have filled the bottom contour level
for j = 1:length(C2)+1
x(1) = 0;
x(2) = 1;
x(3) = 1;
x(4) = 0;
y(1) = (j-1)*(1/(length(C2)+1));
y(2) = (j-1)*(1/(length(C2)+1));
y(3) = j*(1/(length(C2)+1));
y(4) = j*(1/(length(C2)+1));
hfill = fill(x,y,colors(j));
hold on
end
end
set(ax,'YAxisLocation','right')
set(ax,'xtick',[])
ylimits = get(gca,'ylim');
myYticks = ylimits(1):(ylimits(2)-ylimits(1))/(length(C2)+1):ylimits(2)...
-((ylimits(2)-ylimits(1))/(length(C2)+1));
set(gca,'ytick',myYticks);
myStr{1} = ' ';
for kk = 1:length(C2)
myStr{kk+1} = num2str(C2(kk));
end
myStr{(length(C2)+2)} = ' ';
set(gca,'YTickLabel',myStr)
set(gca,'CLim',[minc maxc])
% set up axes deletefcn
set(ax,'tag','Colorbar','deletefcn','colorbar(''delete'')')
elseif loc(1)=='h', % Append horizontal scale to top of current plot
if isempty(ax),
units = get(h,'units'); set(h,'units','normalized')
pos = get(h,'Position');
stripe = 0.075; space = 0.1;
set(h,'Position',...
[pos(1) pos(2)+(stripe+space)*pos(4) pos(3) (1-stripe-space)*pos(4)])
rect = [pos(1) pos(2) pos(3) stripe*pos(4)];
ud.origPos = pos;
% Create axes for stripe and
% create DeleteProxy object (an invisible text object in
% the target axes) so that the colorbar will be deleted
% properly.
ud.DeleteProxy = text('parent',h,'visible','off',...
'tag','ColorbarDeleteProxy',...
'handlevisibility','off',...
'deletefcn','eval(''delete(get(gcbo,''''userdata''''))'','''')');
ax = axes('Position', rect,'Tag','TMW_COLORBAR');
set(ud.DeleteProxy,'userdata',ax)
set(h,'units',units)
else % if colobar axes already exist
axes(ax);
ud = get(ax,'userdata');
end
% Create color stripe by drawing the appropriate number of filled rectangles
if any(isnan(colors)) % if the bottom level is not filled
for j = 1:length(C2)+1
k = j - 1; % don't fill bottom rectangle on colorbar
y(1) = 0;
y(2) = 1;
y(3) = 1;
y(4) = 0;
x(1) = (j-1)*(1/(length(C2)+1));
x(2) = (j-1)*(1/(length(C2)+1));
x(3) = j*(1/(length(C2)+1));
x(4) = j*(1/(length(C2)+1));
if (k == 0)
hfill = fill(x,y,colors(length(colors))); % fill with NaN
else
hfill = fill(x,y,colors(k));
end
hold on
end
else %we have filled the bottom contour level
for j = 1:length(C2)+1
y(1) = 0;
y(2) = 1;
y(3) = 1;
y(4) = 0;
x(1) = (j-1)*(1/(length(C2)+1));
x(2) = (j-1)*(1/(length(C2)+1));
x(3) = j*(1/(length(C2)+1));
x(4) = j*(1/(length(C2)+1));
hfill = fill(x,y,colors(j));
hold on
end
end
set(ax,'ytick',[])
xlimits = get(gca,'xlim');
myXticks = xlimits(1):(xlimits(2)-xlimits(1))/(length(C2)+1):xlimits(2)...
-((xlimits(2)-xlimits(1))/(length(C2)+1));
set(gca,'xtick',myXticks);
myStr{1} = ' ';
for kk = 1:length(C2)
myStr{kk+1} = num2str(C2(kk));
end
myStr{(length(C2)+2)} = ' ';
set(gca,'XTickLabel',myStr)
set(gca,'CLim',[minc maxc])
% set up axes deletefcn
set(ax,'tag','Colorbar','deletefcn','colorbar(''delete'')')
else
error('COLORBAR expects a handle, ''vert'', or ''horiz'' as input.')
end
if ~isfield(ud,'DeleteProxy'), ud.DeleteProxy = []; end
if ~isfield(ud,'origPos'), ud.origPos = []; end
ud.PlotHandle = h;
set(ax,'userdata',ud)
set(gcf,'CurrentAxes',h)
set(gcf,'NextPlot',origNextPlot)
if nargout>0, hcb = ax; end |
Partager