utguidiv.m
上传用户:haiyisale
上传日期:2013-01-09
资源大小:3246k
文件大小:14k
源码类别:

波变换

开发平台:

Matlab

  1. function varargout = utguidiv(option,varargin)
  2. %UTGUIDIV Utilities for testing inputs for different "TOOLS" files.
  3. %   VARARGOUT = UTGUIDIV(OPTION,VARARGIN)
  4. %   M. Misiti, Y. Misiti, G. Oppenheim, J.M. Poggi 12-May-98.
  5. %   Last Revision: 21-May-2003.
  6. %   Copyright 1995-2004 The MathWorks, Inc.
  7. %   $Revision: 1.8.4.2 $  $Date: 2004/03/15 22:42:06 $
  8. switch option
  9.     case 'ini'
  10.         winAttrb = [];
  11.         optval   = varargin{1};
  12.         switch nargin
  13.             case 2
  14.                 if ~ischar(optval)
  15.                     winAttrb = optval; optval = 'create';
  16.                 end
  17.             otherwise
  18.                 if isequal(optval,'create') ,  winAttrb = varargin{2}; end
  19.             end
  20.             varargout = {optval,winAttrb};
  21.         
  22.     case 'WTB_DemoPath'
  23.         testfile = varargin{1};
  24.         dum = which('dguicf1d.m','-all');
  25.         pathname = fileparts(dum{1});
  26.         pathname = which([pathname filesep testfile]);
  27.         if ~isempty(pathname)
  28.             ind = findstr(pathname,testfile);
  29.             pathname = pathname(1:ind-1);
  30.         end
  31.         varargout{1} = pathname;       
  32.         
  33.     case {'test_load','test_save'}
  34.         fig  = varargin{1};
  35.         mask = varargin{2};
  36.         txt  = varargin{3};
  37.         [xpos,ypos] = wfigutil('left_up',fig,10,30);
  38.         verMAT = version; 
  39.         verMAT = str2double(verMAT(1:3));
  40.         switch option
  41.             case 'test_load',
  42.                 if verMAT<7
  43.                     [filename,pathname] = uigetfile(mask,txt,xpos,ypos);
  44.                 else
  45.                     [filename,pathname] = uigetfile(mask,txt,'location',[xpos,ypos]);
  46.                 end
  47.             case 'test_save'
  48.                 if verMAT<7
  49.                     [filename,pathname] = uiputfile(mask,txt,xpos,ypos);
  50.                 else
  51.                     [filename,pathname] = uiputfile(mask,txt,'location',[xpos,ypos]);
  52.                 end
  53.                 try
  54.                     [name,ext] = strtok(filename,'.');
  55.                     eval([name ' = 0;'])
  56.                 catch
  57.                     filename = 0;
  58.                 end
  59.         end
  60.         if isempty(filename) | filename==0 , ok = 0;
  61.         else , ok = 1;
  62.         end
  63.         varargout = {filename,pathname,ok};
  64.         
  65.     case {'load_sig','load_dem1D'}
  66.         fig  = varargin{1};
  67.         switch option
  68.             case 'load_sig'       
  69.                 mask = varargin{2};
  70.                 if isequal(mask,'Signal_Mask')
  71.                     mask = {...
  72.                        '*.mat;*.wav;*.au','Signal ( *.mat , *.wav , *.au)';
  73.                        '*.*','All Files (*.*)'};
  74.                 end
  75.                 txt  = varargin{3};
  76.                 [filename,pathname,ok] = utguidiv('test_load',fig,mask,txt);
  77.                 
  78.             case 'load_dem1D'
  79.                 pathname = varargin{2};
  80.                 filename = varargin{3};
  81.                 ok = 1;
  82.         end
  83.         
  84.         % default.
  85.         %---------
  86.         sigInfos = struct(...
  87.             'pathname',pathname, ...
  88.             'filename',filename, ...
  89.             'name','',     ...
  90.             'size',0       ...
  91.             );
  92.         sig_Anal = [];
  93.         
  94.         if ok
  95.             wwaiting('msg',fig,'Wait ... loading');
  96.             [sigInfos.name,ext,fullName,fileStruct,err] = ...
  97.                 getFileINFO(pathname,filename);
  98.             if ~err
  99.                 err = 1;
  100.                 for k = 1:length(fileStruct)
  101.                     if isequal(fileStruct(k).class,'double')
  102.                         siz = fileStruct(k).size;
  103.                         if min(siz)==1 & max(siz)>1
  104.                             err = 0;
  105.                             sigInfos.name = fileStruct(k).name;
  106.                             break
  107.                         end
  108.                     end
  109.                 end
  110.                 if ~err
  111.                     try
  112.                         load(fullName,'-mat');
  113.                         sig_Anal = eval(sigInfos.name);
  114.                     catch
  115.                         err = 1; numMSG = 1;
  116.                     end
  117.                 else
  118.                     numMSG = 2;
  119.                 end
  120.             else
  121.                 numMSG = 1;
  122.                 [sig_Anal,err,msg] = load_1D_NotMAT(pathname,filename);
  123.                 if ~isempty(msg) , numMSG = msg; end
  124.             end
  125.             if ~err
  126.                 err = ~isreal(sig_Anal);
  127.                 if err , numMSG = 4; end
  128.             end        
  129.             if err ,  dispERROR_1D(fig,sigInfos.filename,numMSG); end
  130.             ok = ~err;
  131.         end
  132.         if ok
  133.             if size(sig_Anal,1)>1 , sig_Anal = sig_Anal'; end
  134.             sigInfos.size = length(sig_Anal);        
  135.         end
  136.         varargout = {sigInfos,sig_Anal,ok};
  137.         
  138.     case {'direct_load_sig'}
  139.         pathname = varargin{2};
  140.         filename = varargin{3};
  141.         [sig_Anal,err] = load_1D_NotMAT(pathname,filename);
  142.         ok = ~err;
  143.         varargout = {sig_Anal,ok};
  144.         
  145.     case {'load_img','load_dem2D'}
  146.         fig  = varargin{1}; 
  147.         switch option
  148.             case 'load_img'       
  149.                 mask = varargin{2};
  150.                 txt  = varargin{3};
  151.                 [filename,pathname,ok] = utguidiv('test_load',fig,mask,txt);
  152.                 
  153.             case 'load_dem2D'
  154.                 pathname = varargin{2};
  155.                 filename = varargin{3};
  156.                 ok = 1;
  157.         end
  158.         default_nbcolors = varargin{4};
  159.         
  160.         % default.
  161.         %---------
  162.         imgInfos = struct(...
  163.             'pathname',pathname,  ...    
  164.             'filename',filename,  ...
  165.             'name','',      ...
  166.             'true_name','', ...
  167.             'type','mat',   ...
  168.             'self_map',0,   ...
  169.             'size',[0 0]    ...
  170.             );
  171.         X = []; map = [];
  172.         
  173.         if ok
  174.             wwaiting('msg',fig,'Wait ... loading');
  175.             [imgInfos.name,ext,fullName,fileStruct,err] = ...
  176.                 getFileINFO(pathname,filename);
  177.             if ~err
  178.                 err = 1;
  179.                 for k = 1:length(fileStruct)
  180.                     mm = min(fileStruct(k).size);
  181.                     if mm>3
  182.                         err = 0;
  183.                         imgInfos.true_name = fileStruct(k).name;
  184.                         break
  185.                     end
  186.                 end
  187.                 if ~err
  188.                     try
  189.                         load(fullName,'-mat');
  190.                         imgInfos.type = 'mat';
  191.                         X = eval(imgInfos.true_name);
  192.                         X = double(X);
  193.                     catch
  194.                         err = 1; numMSG = 1;
  195.                     end
  196.                 else
  197.                     numMSG = 2;
  198.                 end
  199.             else
  200.                 numMSG = 1;
  201.                 try
  202.                     [X,map,imgFormat,colorType,err] = ...
  203.                         load_2D_NotMAT(pathname,filename);
  204.                     if ~err
  205.                         mi = min(min(X));
  206.                         if mi<1 , X = X-mi+1; end
  207.                         if isempty(map)
  208.                             ma  = max(max(X));
  209.                             map = pink(ma);
  210.                             X   = wcodemat(X,ma);
  211.                         end
  212.                         [dummy,name,ext,dummy] = fileparts(filename);
  213.                         imgInfos.type = imgFormat;
  214.                         imgInfos.name = [name,ext];
  215.                         imgInfos.true_name = 'X';
  216.                         err = 0;
  217.                     else
  218.                         numMSG = 3;
  219.                     end
  220.                 catch
  221.                     numMSG = lasterr;
  222.                 end
  223.             end
  224.             if ~err
  225.                 err = ~isreal(X);
  226.                 if err , numMSG = 4; end
  227.             end
  228.             ok = ~err;
  229.             if ~err
  230.                 imgInfos.self_map = ~isempty(map);
  231.                 if ~imgInfos.self_map
  232.                     mi = round(min(min(X)));
  233.                     ma = round(max(max(X)));
  234.                     if mi<=0 , ma = ma-mi+1; end
  235.                     ma  = min([default_nbcolors,max([2,ma])]);
  236.                     map = pink(ma);
  237.                 end
  238.                 imgInfos.size = fliplr(size(X));
  239.             else
  240.                 dispERROR_2D(fig,imgInfos.filename,numMSG);
  241.             end
  242.         end
  243.         varargout = {imgInfos,X,map,ok};
  244.     case {'direct_load_img'}
  245.         pathname = varargin{2};
  246.         filename = varargin{3};
  247.         [X,map,imgFormat,colorType,err] = load_2D_NotMAT(pathname,filename);
  248.         varargout = {X,map,imgFormat,colorType,err};
  249.         
  250.     case 'load_var'
  251.         fig  = varargin{1};
  252.         mask = varargin{2};
  253.         txt  = varargin{3};
  254.         vars = varargin{4};
  255.         [filename,pathname,ok] = utguidiv('test_load',fig,mask,txt);
  256.         if ok
  257.             wwaiting('msg',fig,'Wait ... loading');
  258.             try
  259.                 err = 0;
  260.                 load([pathname filename],'-mat');
  261.                 for k = 1:length(vars)
  262.                     var = vars{k};
  263.                     if ~exist(vars{k},'var') , err = 1; break; end
  264.                 end
  265.                 if err , msg = sprintf('variable : %s not found !', var); end
  266.             catch
  267.                 err = 1;
  268.                 msg = sprintf('File %s is not a valid file.', filename);
  269.             end
  270.             if err
  271.                 wwaiting('off',fig);
  272.                 errordlg(msg,'Load ERROR','modal');
  273.                 ok = 0;
  274.             end
  275.         end
  276.         varargout = {filename,pathname,ok};
  277.         
  278.     case 'load_wpdec'
  279.         fig  = varargin{1};
  280.         mask = varargin{2};
  281.         txt  = varargin{3};
  282.         ord  = varargin{4};
  283.         [filename,pathname,ok] = utguidiv('test_load',fig,mask,txt);
  284.         if ok
  285.             wwaiting('msg',fig,'Wait ... loading');
  286.             fullName = fullfile(pathname,filename);
  287.             try
  288.                 err = 0;
  289.                 load(fullName,'-mat');
  290.                 if ~exist('tree_struct','var')
  291.                     err = 1; var = 'tree_struct';
  292.                 elseif ~exist('data_struct','var')
  293.                     if ~isa(tree_struct,'wptree')
  294.                         err = 1; var = 'data_struct';
  295.                     end
  296.                 end
  297.                 if ~err
  298.                     order = treeord(tree_struct);
  299.                     err = ~isequal(ord,order);
  300.                     if err
  301.                         msg = strvcat(sprintf('The decomposition is not a %s dimensional analysis',...
  302.                             int2str(ord)),' ');
  303.                     end
  304.                 else
  305.                     msg = sprintf('variable : %s not found !', var);
  306.                 end
  307.             catch
  308.                 err = 1;
  309.                 msg = sprintf('File %s is not a valid file.', filename);
  310.             end
  311.             if err
  312.                 wwaiting('off',fig);
  313.                 errordlg(msg,'Load ERROR','modal');
  314.                 ok = 0;
  315.             end
  316.         end
  317.         varargout = {filename,pathname,ok};
  318. end
  319. %------------------------------------------------------------------------------
  320. function [name,ext,fullName,fileStruct,err] = getFileINFO(pathname,filename)
  321. fullName = fullfile(pathname,filename);
  322. [name,ext] = strtok(filename,'.');
  323. if ~isempty(ext) , ext = ext(2:end); end
  324. try
  325.     [fileStruct,err] = wfileinf(fullName);
  326.     err = 0;
  327. catch
  328.     err = 1; fileStruct = [];
  329. end
  330. %------------------------------------------------------------------------------
  331. function dispERROR_1D(fig,fileName,numMSG)
  332. if isnumeric(numMSG)
  333.     switch numMSG
  334.     case 1 ,
  335.         msg = sprintf('File %s is not a valid file.',fileName);
  336.         
  337.     case 2 ,
  338.         msg = strvcat(...
  339.       sprintf('File %s doesn''t contain one dimensional Signal.', fileName),' ');
  340.     case 3 ,
  341.         msg = strvcat(...
  342.       sprintf('File %s is not a valid file or is empty.',fileName),' ');
  343.     case 4 ,
  344.         msg = strvcat(...
  345.       sprintf('File %s doesn''t contain a real Signal.',fileName),' ');    
  346.     end
  347. else
  348.     msg = numMSG;
  349. end
  350. wwaiting('off',fig);
  351. errordlg(msg,'Load Signal ERROR','modal');
  352. %------------------------------------------------------------------------------
  353. function dispERROR_2D(fig,fileName,numMSG)
  354. if isnumeric(numMSG)
  355.     switch numMSG
  356.     case 1
  357.         msg = strvcat(...
  358.             sprintf('File %s is not a valid file or is empty.', fileName),' ');
  359.     case 2
  360.         msg = strvcat(...
  361.             sprintf('File %s doesn''t contain an Image.', fileName),' ');
  362.     case 3
  363.         msg = strvcat(...
  364.             sprintf('File %s doesn''t contain  an indexed Image.', fileName),' ');
  365.         
  366.     case 4
  367.         msg = strvcat(...
  368.     sprintf('File %s doesn''t contain real data.', fileName),' ');
  369.     end
  370. else
  371.     msg = numMSG;
  372. end
  373. wwaiting('off',fig);
  374. errordlg(msg,'Load Image ERROR','modal');
  375. %------------------------------------------------------------------------------
  376. function [sig,err,msg] = load_1D_NotMAT(pathname,filename)
  377. fullName = fullfile(pathname,filename);
  378. [name,ext] = strtok(filename,'.');
  379. if ~isempty(ext) , ext = ext(2:end); end
  380. sig = []; err = 1; msg = '';
  381. switch lower(ext)
  382. case 'wav' , try , sig = wavread(fullName); err = 0; catch , msg = lasterr; end
  383. case 'au'  , try , sig = auread(fullName);  err = 0; catch , msg = lasterr; end                
  384. end
  385. if ~err & size(sig,1)>1 , sig = sig'; end          
  386. %------------------------------------------------------------------------------
  387. function [X,map,imgFormat,colorType,err] = load_2D_NotMAT(pathname,filename)
  388. [name,ext,fullName,fileStruct,err] = getFileINFO(pathname,filename);
  389. switch ext
  390.     case {'bmp','hdf','jpg','jpeg','pcx','tif','tiff','gif'}
  391.         [info,msginfo] = imfinfo(fullName,ext);
  392.     otherwise
  393.         [info,msginfo] = imfinfo(fullName);
  394. end
  395. imgFormat = info.Format;
  396. colorType = info.ColorType;
  397. err = 0;
  398. [X,map] = imread(fullName,ext);
  399. X = double(X);
  400. switch lower(colorType)
  401.     case {'rgb','truecolor'}
  402.         try
  403.             Xrgb = 0.2990*X(:,:,1) + 0.5870*X(:,:,2) + 0.1140*X(:,:,3);
  404.             X = round(Xrgb);
  405.         catch
  406.             err = 1;
  407.         end
  408.     case {'indexed','grayscale'}
  409. end
  410. %------------------------------------------------------------------------------