Home > NoiseTools > nt_plotxx.m

nt_plotxx

PURPOSE ^

nt_plotxx(fname,bounds,chans) - plot using index file

SYNOPSIS ^

function [bstats,wstats]=nt_plotxx(fname,bounds,chans,imageflag)

DESCRIPTION ^

nt_plotxx(fname,bounds,chans) - plot using index file

  fname: name of file to plot (or of its index file)
  bounds: [start,stop] (s) range to plot [default: all]
  chans: channels to plot [default: all]
 
 Data are plotted using cheap data representation in index file.
 All channels are plotted unless specified. 
 The entire file is plotted unless specified.
 The mean of each channel (over entire data) is removed before plotting.

 A limited form of zooming and navigation is available using the arrow keys.

 NoiseTools

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [bstats,wstats]=nt_plotxx(fname,bounds,chans,imageflag)
0002 %nt_plotxx(fname,bounds,chans) - plot using index file
0003 %
0004 %  fname: name of file to plot (or of its index file)
0005 %  bounds: [start,stop] (s) range to plot [default: all]
0006 %  chans: channels to plot [default: all]
0007 %
0008 % Data are plotted using cheap data representation in index file.
0009 % All channels are plotted unless specified.
0010 % The entire file is plotted unless specified.
0011 % The mean of each channel (over entire data) is removed before plotting.
0012 %
0013 % A limited form of zooming and navigation is available using the arrow keys.
0014 %
0015 % NoiseTools
0016 
0017 nt_greetings;
0018 
0019 assert(nargin>0, '!');
0020 if nargin<2; bounds=[]; end
0021 if nargin<3; chans=[]; end
0022 if nargin<4; imageflag=false; end
0023 
0024 if ischar(fname) % file name
0025     [FILEPATH,NAME,EXT]=fileparts(fname);
0026     
0027     if isempty(FILEPATH) 
0028         FILEPATH='./'; 
0029         fname=[FILEPATH,filesep,fname];
0030     end
0031     
0032     disp(EXT)
0033     if ~strcmp(EXT,'.idxx') % must be data file, find index
0034         fname_data=fname;
0035         fname=[FILEPATH,filesep,'idxx',filesep,NAME,EXT,'.idxx'];
0036     end
0037     
0038     if 2~=exist(fname)
0039         disp('No index file found.  Create one.');
0040         if 2~=exist(fname_data)
0041             error('No data file either...');
0042         end
0043         disp('This may take a while...');
0044         tic;
0045         nt_idxx(fname);
0046         toc;
0047     end
0048     disp('read from file...'); tic;
0049     load(fname, '-mat', 'bstats', 'wstats'); % ignores 'cstats' and 'sstats'
0050     disp('done'); toc
0051     i=bstats;
0052     ii=wstats;
0053 elseif iscell(fname) %  index struct
0054     i=fname{1};
0055     ii=fname{2};
0056 else
0057     error('!');
0058 end
0059 
0060 if 2==exist('get_axes_width')  % tbd: replicate to remove dependency
0061     % estimate how many pixels fit within a window
0062     axes_width=get_axes_width(gca);
0063 else
0064     warning('get_axes_width() not found');
0065     disp('Download https://www.mathworks.com/matlabcentral/fileexchange/40790-plot-big');
0066     axes_width=500;
0067 end
0068 
0069 % decode from int structure
0070 if isempty(chans); chans=1:i.nchans; end
0071 mn=nt_double2int(ii.min,{[],chans});
0072 mx=nt_double2int(ii.max,{[],chans});
0073 mnn=nt_double2int(ii.mean,{[],chans});
0074 
0075 % remove mean from min and max for each channel
0076 mn=bsxfun(@minus,mn,mean(mnn));
0077 mx=bsxfun(@minus,mx,mean(mnn));
0078 
0079 srr=i.sr/i.dsr;     % sampling rate
0080 
0081 % select data within requested bounds (in seconds)
0082 maxbound=(size(mn,1)-1)/srr;    % s, end of file
0083 if ~maxbound;
0084     disp('warning: empty file');
0085     return
0086 end
0087 if isempty(bounds); bounds=[0,maxbound]; end
0088 start=1+max(0,min(size(mn,1)-1, round(bounds(1)*srr)));
0089 stop=1+max(0,min(size(mn,1)-1, round(bounds(2)*srr)));
0090 mn=mn(start:stop,:);
0091 mx=mx(start:stop,:);
0092 
0093 % first display using 'plot'
0094 if size(mx,1)<axes_width
0095     % running into the limits of the resolution of the index, we should go
0096     % to data file - tbd
0097 end
0098 if imageflag
0099     dsr=round(size(mx,1)/(axes_width));
0100     mmx=dsmmx(cat(3,mn,mx),dsr);
0101     mn=mmx(:,:,1); mx=mmx(:,:,2);
0102     srr=srr/dsr;
0103     % plot max-min range as image
0104     imagesc((mx-mn)');
0105 else
0106     if size(mx,1)>axes_width*50
0107         % coalesce first if really big
0108         dsr=round(size(mx,1)/(axes_width*50));
0109         mmx=dsmmx(cat(3,mn,mx),dsr);
0110         mn=mmx(:,:,1); mx=mmx(:,:,2);
0111         srr=srr/dsr;
0112     end
0113 
0114     % plot as waveform
0115     zz1=mn; zz1(1:2:end,:)=mx(1:2:end,:);
0116     zz2=mn; zz2(2:2:end,:)=mx(2:2:end,:);
0117     yy=[mn; flipud(mx); zz1; flipud(zz2)]; 
0118     xx=[(0:size(mn,1)-1)' ; (size(mn,1)-1:-1:0)'; (0:size(mn,1)-1)' ; (size(mn,1)-1:-1:0)'];
0119     if bounds(1)+max(xx/srr)<3600
0120         plot(bounds(1)+xx/srr , yy); % plot min forward & max backward
0121         xlabel('time (s)');
0122         xlim(bounds);
0123     else 
0124         plot((bounds(1)+xx/srr)/3600 , yy); % plot min forward & max backward
0125         xlabel('time (h)');
0126         xlim(bounds/3600);
0127     end
0128     a=min(mn(:));
0129     b=max(mx(:));
0130     if b>a; ylim([a-(b-a)*0.1,b+(b-a)*0.1]); end
0131 
0132 end
0133     
0134 drawnow;
0135 
0136 if 0
0137     % then write over with fill, better aspect, in principle, but slow
0138     hold on;
0139     if size(mx,1)>axes_width
0140         dsr=round(size(mx,1)/(axes_width));
0141         mmx=dsmmx(cat(3,mn,mx),dsr);
0142         mn=mmx(:,:,1); mx=mmx(:,:,2);
0143         srr=srr/dsr;
0144     end
0145     npoints=size(mn,1);
0146     X=bounds(1)+(0:npoints-1)'/srr;
0147     h=fill([X;flipud(X)],[mn;flipud(mx)], 'k', 'LineStyle', 'none');
0148     colororder=get(gca,'colororder');
0149     for iPatch=1:numel(h);
0150         h(iPatch).FaceColor=colororder(1+rem(iPatch-1,7),:);
0151         %h(iPatch).EdgeColor=colororder(1+rem(iPatch-1,7),:);
0152     end
0153     hold off
0154 end
0155 %
0156 % a=min(mn(:));
0157 % b=max(mx(:));
0158 % ylim([a-(b-a)*0.1,b+(b-a)*0.1])
0159 
0160 % GUI
0161 userdata.i=i;
0162 userdata.ii=ii;
0163 userdata.bounds=bounds;
0164 set(gcf,'UserData',userdata);
0165 %set(gcf, 'KeyPressFcn',@keyfunction)
0166 end
0167 
0168 function keyfunction(fig,eventDat) 
0169 userdata=get(fig,'UserData');
0170 b=userdata.bounds;
0171 i=userdata.i;
0172 ii=userdata.ii;
0173 fname=i.fname; 
0174 switch eventDat.Key
0175     case 'rightarrow'
0176         newbounds=[b(1)+(b(2)-b(1))*.5, b(1)+(b(2)-b(1))*1.5];
0177         disp(round([newbounds, diff(newbounds)]))
0178         nt_plotxx({i,ii},newbounds);
0179     case 'leftarrow'
0180         newbounds=[b(1)-(b(2)-b(1))*.5, b(1)+(b(2)-b(1))*.5];
0181          disp(round([newbounds, diff(newbounds)]))
0182         nt_plotxx({i,ii},newbounds);
0183     case 'uparrow'
0184         newbounds=[b(1)+(b(2)-b(1))*.25, b(1)+(b(2)-b(1))*.75];
0185          disp(round([newbounds, diff(newbounds)]))
0186        nt_plotxx({i,ii},newbounds);
0187     case 'downarrow'
0188         newbounds=[b(1)-(b(2)-b(1))*.5, b(1)+(b(2)-b(1))*1.5];
0189         disp(round([newbounds, diff(newbounds)]))
0190         nt_plotxx({i,ii},newbounds);
0191     otherwise
0192 end
0193 if nargout==0; 
0194     %just return stats
0195     clear bstats wstats; 
0196 end
0197 end
0198 
0199 
0200 
0201 function y=dsmmx(mmx,dsr) % downsample min-max array
0202     assert(dsr<size(mmx,1), '!');
0203     assert(size(mmx,3)==2, '!');
0204     n=floor(size(mmx,1)/dsr);
0205     xtra=mmx(n*dsr+1:end,:,:); 
0206     mmx=mmx(1:n*dsr,:,:);
0207     [nsamples,nchans,~]=size(mmx);
0208     mmx=permute(mmx,[3 1 2]); % --> 2 X nsamples X nchans
0209     mmx=reshape(mmx, [dsr*2,nsamples/dsr, nchans]);
0210     mn=min(mmx); 
0211     mx=max(mmx);
0212     y=cat(3,shiftdim(mn,1),shiftdim(mx,1));
0213     % process xtra tbd
0214 end

Generated on Sat 29-Apr-2023 17:15:46 by m2html © 2005