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

波变换

开发平台:

Matlab

  1. function out1 = wp1dmngr(option,win_wptool,in3,in4,in5,in6,in7)
  2. %WP1DMNGR Wavelet packets 1-D general manager.
  3. %   OUT1 = WP1DMNGR(OPTION,WIN_WPTOOL,IN3,IN4,IN5,IN6,IN7)
  4. %   M. Misiti, Y. Misiti, G. Oppenheim, J.M. Poggi 12-Mar-96.
  5. %   Last Revision: 28-Jun-2001.
  6. %   Copyright 1995-2002 The MathWorks, Inc.
  7. %   $Revision: 1.17 $
  8. % Default values.
  9. %----------------
  10. max_lev_anal = 12;
  11. default_nbcolors = 128;
  12. % Memory Blocks of stored values.
  13. %================================
  14. % MB0.
  15. %-----
  16. n_InfoInit   = 'WP1D_InfoInit';
  17. ind_filename = 1;
  18. ind_pathname = 2;
  19. nb0_stored   = 2;
  20. % MB1.
  21. %-----
  22. n_param_anal   = 'WP1D_Par_Anal';
  23. ind_sig_name   = 1;
  24. ind_wav_name   = 2;
  25. ind_lev_anal   = 3;
  26. ind_ent_anal   = 4;
  27. ind_ent_par    = 5;
  28. ind_sig_size   = 6;
  29. ind_act_option = 7;
  30. ind_thr_val    = 8;
  31. nb1_stored     = 8;
  32. % MB2.
  33. %-----
  34. n_wp_utils = 'WP_Utils';
  35. ind_tree_lin  = 1;
  36. ind_tree_txt  = 2;
  37. ind_type_txt  = 3;
  38. ind_sel_nodes = 4;
  39. ind_gra_area  = 5;
  40. ind_nb_colors = 6;
  41. nb2_stored    = 6;
  42. % MB3.
  43. %-----
  44. n_structures = 'Structures';
  45. ind_tree_st  = 1;
  46. ind_data_st  = 2;
  47. nb3_stored   = 2;
  48. % MB4.
  49. %-----
  50. n_sav_struct    = 'Sav_Struct';
  51. ind_sav_tree_st = 1;
  52. ind_sav_data_st = 2;
  53. nb4_stored      = 2;
  54. switch option
  55.     case 'load_sig'
  56.         % in3 optional (new_anal)
  57.         %------------------------
  58.         % Loading file.
  59.         %-------------
  60.         [sigInfos,Signal_Anal,ok] = ...
  61.             utguidiv('load_sig',win_wptool,'Signal_Mask','Load Signal');
  62.         
  63.         if ~ok, return; end
  64.         % Cleaning.
  65.         %----------
  66.         wwaiting('msg',win_wptool,'Wait ... cleaning');
  67.         wp1dutil('clean',win_wptool,option,'');
  68.         % Setting Analysis parameters.
  69.         %-----------------------------
  70.         wmemtool('wmb',win_wptool,n_param_anal, ...
  71.                        ind_act_option,option,     ...
  72.                        ind_sig_name,sigInfos.name,...
  73.                        ind_sig_size,sigInfos.size ...
  74.                        );
  75.         wmemtool('wmb',win_wptool,n_InfoInit, ...
  76.                        ind_filename,sigInfos.filename, ...
  77.                        ind_pathname,sigInfos.pathname  ...
  78.                        );
  79.         wmemtool('wmb',win_wptool,n_wp_utils,...
  80.                        ind_nb_colors,default_nbcolors);
  81.         % Setting GUI values.
  82.         %--------------------
  83.         wp1dutil('set_gui',win_wptool,option);
  84.         % Drawing.
  85.         %---------
  86.         wp1ddraw('sig',win_wptool,Signal_Anal);
  87.         % Setting enabled values.
  88.         %------------------------
  89.         wp1dutil('enable',win_wptool,option);
  90.         % End waiting.
  91.         %-------------
  92.         wwaiting('off',win_wptool);
  93.     case 'load_dec'
  94.         switch nargin
  95.             case 2
  96.                 % Testing file.
  97.                 %--------------
  98.                 winTitle = 'Load Wavelet Packet Analysis (1D)';
  99.                 fileMask = {...
  100.                     '*.wp1;*.mat' , 'Decomposition  (*.wp1;*.mat)';
  101.                     '*.*','All Files (*.*)'};                
  102.                 [filename,pathname,ok] = utguidiv('load_wpdec',win_wptool, ...
  103.                     fileMask,winTitle,2);
  104.                 if ~ok, return; end
  105.                 % Loading file.
  106.                 %--------------
  107.                 load([pathname filename],'-mat');
  108.                 if isa(tree_struct,'wptree') , data_struct = []; end
  109.                 if ~exist('data_name','var') , data_name = 'no name'; end
  110.             case {3,4}
  111.                 filename    = '';
  112.                 pathname    = '';
  113.                 tree_struct = in3;
  114.                 if nargin==3
  115.                     if isa(tree_struct,'wptree')
  116.                         data_struct = [];
  117.                     else
  118.                         wwaiting('off',win_wptool);
  119.                         msg = strvcat(['The decomposition is not a' ...
  120. ' valid one dimensional analysis'],' ');
  121.                         errordlg(msg,...
  122.                                 'Load Wavelet Packet Analysis (1D)',...
  123.                                 'modal');
  124.                         return
  125.                     end
  126.                 else
  127.                     data_struct = in4;
  128.                 end
  129.                 data_name = 'input var';
  130.         end
  131.         % Cleaning.
  132.         %----------
  133.         wwaiting('msg',win_wptool,'Wait ... cleaning');
  134.         wp1dutil('clean',win_wptool,option);
  135.         % Getting Analysis parameters.
  136.         %-----------------------------
  137.         if isa(tree_struct,'wptree')
  138.             [Wave_Name,Ent_Name,Ent_Par,Signal_Size] = ...
  139.                 read(tree_struct,'wavname','entname','entpar','sizes',0);
  140.         else
  141.             Wave_Name = wdatamgr('read_wave',data_struct);
  142.             [Ent_Name,Ent_Par] = wdatamgr('read_tp_ent',data_struct);
  143.             sizes       = wdatamgr('rsizes',data_struct);
  144.             Signal_Size = sizes(1,1);
  145.         end
  146.         Level_Anal  = treedpth(tree_struct);
  147.         Signal_Name = data_name;
  148.         % Setting Analysis parameters
  149.         %-----------------------------
  150.         wmemtool('wmb',win_wptool,n_param_anal,  ...
  151.                        ind_act_option,option,    ...
  152.                        ind_sig_name,Signal_Name, ...
  153.                        ind_wav_name,Wave_Name,   ...
  154.                        ind_lev_anal,Level_Anal,  ...
  155.                        ind_sig_size,Signal_Size, ...
  156.                        ind_ent_anal,Ent_Name,    ...
  157.                        ind_ent_par,Ent_Par       ...
  158.                        );
  159.         wmemtool('wmb',win_wptool,n_wp_utils,      ...
  160.                        ind_nb_colors,default_nbcolors ...
  161.                        );
  162.         % Writing structures.
  163.         %----------------------
  164.         wmemtool('wmb',win_wptool,n_sav_struct,     ...
  165.                        ind_sav_tree_st,tree_struct, ...
  166.                        ind_sav_data_st,data_struct);
  167.         wmemtool('wmb',win_wptool,n_structures, ...
  168.                        ind_tree_st,tree_struct, ...
  169.                        ind_data_st,data_struct);
  170.         % Setting GUI values.
  171.         %--------------------
  172.         wp1dutil('set_gui',win_wptool,option);
  173.         % Computing and Drawing Original Signal.
  174.         %---------------------------------------
  175.         wwaiting('msg',win_wptool,'Wait ... computing');
  176.         if isa(tree_struct,'wptree')
  177.             Signal_Anal = wprec(tree_struct);
  178.         else
  179.             Signal_Anal = wprec(tree_struct,data_struct);
  180.         end
  181.         wp1ddraw('sig',win_wptool,Signal_Anal);
  182.         % Decomposition drawing
  183.         %----------------------
  184.         wp1ddraw('anal',win_wptool);
  185.         % Setting enabled values.
  186.         %------------------------
  187.         wp1dutil('enable',win_wptool,option);
  188.         % End waiting.
  189.         %-------------
  190.         wwaiting('off',win_wptool);
  191.     case 'demo'
  192.         % in3 = Signal_Name
  193.         % in4 = Wave_Name
  194.         % in5 = Level_Anal
  195.         % in6 = Ent_Name
  196.         % in7 = Ent_Par (optional)
  197.         %--------------------------
  198.         Signal_Name = deblank(in3);
  199.         Wave_Name   = deblank(in4);
  200.         Level_Anal  = in5;
  201.         Ent_Name    = deblank(in6);
  202.         if nargin==6 ,  Ent_Par = 0 ; else , Ent_Par = in7; end
  203.         % Loading file.
  204.         %-------------
  205.         filename = [Signal_Name '.mat'];       
  206.         pathname = utguidiv('WTB_DemoPath',filename);
  207.         [sigInfos,Signal_Anal,ok] = ...
  208.             utguidiv('load_dem1D',win_wptool,pathname,filename);
  209.         if ~ok, return; end
  210.         % Cleaning.
  211.         %----------
  212.         wwaiting('msg',win_wptool,'Wait ... cleaning');
  213.         wp1dutil('clean',win_wptool,option);
  214.         % Setting Analysis parameters
  215.         %-----------------------------
  216.         wmemtool('wmb',win_wptool,n_param_anal,    ...
  217.                        ind_act_option,option,      ...
  218.                        ind_sig_name,sigInfos.name, ...
  219.                        ind_wav_name,Wave_Name,     ...
  220.                        ind_lev_anal,Level_Anal,    ...
  221.                        ind_sig_size,sigInfos.size, ...
  222.                        ind_ent_anal,Ent_Name,      ...
  223.                        ind_ent_par,Ent_Par         ...
  224.                         );
  225.         wmemtool('wmb',win_wptool,n_InfoInit, ...
  226.                        ind_filename,sigInfos.filename, ...
  227.                        ind_pathname,sigInfos.pathname  ...
  228.                        );
  229.         wmemtool('wmb',win_wptool,n_wp_utils,      ...
  230.                        ind_nb_colors,default_nbcolors ...
  231.                        );
  232.         % Setting GUI values.
  233.         %--------------------
  234.         wp1dutil('set_gui',win_wptool,option);
  235.         % Drawing.
  236.         %---------
  237.         wp1ddraw('sig',win_wptool,Signal_Anal);
  238.         % Calling Analysis.
  239.         %-----------------
  240.         wp1dmngr('step2',win_wptool,option);
  241.         % Setting enabled values.
  242.         %------------------------
  243.         wp1dutil('enable',win_wptool,option);
  244.         % End waiting.
  245.         %-------------
  246.         wwaiting('off',win_wptool);
  247.     case 'save_synt'
  248.         % Testing file.
  249.         %--------------
  250.         [filename,pathname,ok] = utguidiv('test_save',win_wptool, ...
  251.                                      '*.mat','Save Synthesized Signal');
  252.         if ~ok, return; end
  253.         [name,ext] = strtok(filename,'.');
  254.         % Begin waiting.
  255.         %--------------
  256.         wwaiting('msg',win_wptool,'Wait ... saving');
  257.         % Saving Synthesized Signal.
  258.         %---------------------------
  259.         [wname,valTHR] = wmemtool('rmb',win_wptool,n_param_anal,...
  260.                                   ind_wav_name,ind_thr_val);
  261.         str_name = name;
  262.         hdl_node = wpssnode('r_synt',win_wptool);
  263.         if ~isempty(hdl_node)
  264.             eval([str_name '= get(hdl_node,''userdata'');']);       
  265.         else
  266.             % Reading structures.
  267.             %--------------------
  268.             [tree_struct,data_struct] =     ...
  269.                     wmemtool('rmb',win_wptool,n_structures,...
  270.                                    ind_tree_st,ind_data_st);
  271.             if isa(tree_struct,'wptree')
  272.                 eval([str_name '= wprec(tree_struct);']);
  273.             else
  274.                 eval([str_name '= wprec(tree_struct,data_struct);']);
  275.             end
  276.         end
  277.         % Saving file.
  278.         %--------------
  279.         [name,ext] = strtok(filename,'.');
  280.         if isempty(ext) | isequal(ext,'.')
  281.             ext = '.mat'; filename = [name ext];
  282.         end
  283.         saveStr = str_name;
  284.         wwaiting('off',win_wptool);
  285.         try
  286.           save([pathname filename],saveStr,'valTHR','wname');
  287.         catch
  288.           errargt(mfilename,'Save FAILED !','msg');
  289.         end
  290.     case 'save_dec'
  291.          % Testing file.
  292.         %--------------
  293.          fileMask = {...
  294.                '*.wp1;*.mat' , 'Decomposition  (*.wp1;*.mat)';
  295.                '*.*','All Files (*.*)'};                
  296.         [filename,pathname,ok] = utguidiv('test_save',win_wptool, ...
  297.                                    fileMask,'Save Wavelet Packet Analysis (1D)');
  298.         if ~ok, return; end
  299.         % Begin waiting.
  300.         %--------------
  301.         wwaiting('msg',win_wptool,'Wait ... saving decomposition');
  302.         % Getting Analysis parameters.
  303.         %-----------------------------
  304.         data_name = wmemtool('rmb',win_wptool,n_param_anal,ind_sig_name);
  305.         % Reading structures.
  306.         %--------------------
  307.         [tree_struct,data_struct] = wmemtool('rmb',win_wptool,n_structures,...
  308.                                                    ind_tree_st,ind_data_st);
  309.         % Saving file.
  310.         %--------------
  311.         valTHR = wmemtool('rmb',win_wptool,n_param_anal,ind_thr_val);
  312.         [name,ext] = strtok(filename,'.');
  313.         if isempty(ext) | isequal(ext,'.')
  314.             ext = '.wp1'; filename = [name ext];
  315.         end
  316.         if isa(tree_struct,'wptree')
  317.             saveStr = {'tree_struct','data_name','valTHR'};
  318.         else
  319.             saveStr = {'tree_struct','data_struct','data_name','valTHR'};
  320.         end
  321.         wwaiting('off',win_wptool);
  322.         try
  323.           save([pathname filename],saveStr{:});
  324.         catch
  325.           errargt(mfilename,'Save FAILED !','msg');
  326.         end
  327.     case 'anal'
  328.         active_option = wmemtool('rmb',win_wptool,n_param_anal,ind_act_option);
  329.         if ~strcmp(active_option,'load_sig')
  330.             % Cleaning. 
  331.             %----------
  332.             wwaiting('msg',win_wptool,'Wait ... cleaning');
  333.             wp1dutil('clean',win_wptool,'load_sig','new_anal');
  334.             wp1dutil('enable',win_wptool,'load_sig');
  335.         else
  336.             wmemtool('wmb',win_wptool,n_param_anal,ind_act_option,'anal');
  337.         end
  338.         % Waiting message.
  339.         %-----------------
  340.         wwaiting('msg',win_wptool,'Wait ... computing');
  341.         % Setting Analysis parameters
  342.         %-----------------------------
  343.         [Wave_Name,Level_Anal] = cbanapar('get',win_wptool,'wav','lev');
  344.         [Ent_Name,Ent_Par,err] = utentpar('get',win_wptool,'ent');
  345.         if err>0
  346.             wwaiting('off',win_wptool);
  347.             switch err
  348.               case 1 , msg = 'Invalid entropy parameter value! ';
  349.               case 2 , msg = 'Invalid entropy name! ';
  350.             end
  351.             errargt(mfilename,msg,'msg');
  352.             utentpar('set',win_wptool);
  353.             return
  354.         end
  355.         wmemtool('wmb',win_wptool,n_param_anal, ...
  356.                        ind_wav_name,Wave_Name, ...
  357.                        ind_lev_anal,Level_Anal,...
  358.                        ind_ent_anal,Ent_Name,  ...
  359.                        ind_ent_par,Ent_Par     ...
  360.                        );
  361.         % Calling Analysis.
  362.         %------------------
  363.         wp1dmngr('step2',win_wptool,option);
  364.         % Setting enabled values.
  365.         %------------------------
  366.         wp1dutil('enable',win_wptool,option);
  367.         % End waiting.
  368.         %-------------
  369.         wwaiting('off',win_wptool);
  370.     case 'step2'
  371.         % Begin waiting.
  372.         %---------------
  373.         wwaiting('msg',win_wptool,'Wait ... computing');
  374.         % Getting  Analysis parameters.
  375.         %------------------------------
  376.         [Signal_Name,Signal_Size,Wave_Name,Level_Anal,Ent_Name,Ent_Par] = ...
  377.                 wmemtool('rmb',win_wptool,n_param_anal, ...
  378.                                ind_sig_name,ind_sig_size, ...
  379.                                ind_wav_name,ind_lev_anal, ...
  380.                                ind_ent_anal,ind_ent_par);
  381.         active_option = wmemtool('rmb',win_wptool,n_param_anal,ind_act_option);
  382.         [filename,pathname] = wmemtool('rmb',win_wptool,n_InfoInit, ...
  383.                                              ind_filename,ind_pathname);
  384.         % Computing.
  385.         %-----------   
  386.         objVersion = wtbxmngr('get','objVersion');
  387.         switch active_option
  388.           case {'demo','anal'}
  389.             try
  390.               load([pathname filename],'-mat');
  391.               Signal_Anal = eval(Signal_Name);
  392.             catch
  393.                 [Signal_Anal,ok] = utguidiv('direct_load_sig',win_wptool,pathname,filename);
  394.                 if ~ok
  395.                     wwaiting('off',win_wptool);
  396.                     msg = sprintf('File %s not found!', filename);
  397.                     errordlg(msg,'Load ERROR','modal');
  398.                     return
  399.                 end
  400.             end
  401.           case 'load_dec'       % second time only for load_dec
  402.             Signal_Anal = get(wp1ddraw('r_orig',win_wptool),'Ydata');
  403.             tree_struct = wmemtool('rmb',win_wptool,n_structures,ind_tree_st);
  404.             objVersion  = isa(tree_struct,'wptree');
  405.         end
  406.         if objVersion>0
  407.             tree_struct = wpdec(Signal_Anal,Level_Anal,Wave_Name,Ent_Name,Ent_Par);
  408.             data_struct = [];
  409.         else
  410.             [tree_struct,data_struct] = ...
  411.                     wpdec(Signal_Anal,Level_Anal,Wave_Name,Ent_Name,Ent_Par);
  412.         end
  413.         % Writing structures.
  414.         %----------------------
  415.         wmemtool('wmb',win_wptool,n_sav_struct,     ...
  416.                        ind_sav_tree_st,tree_struct, ...
  417.                        ind_sav_data_st,data_struct);
  418.         wmemtool('wmb',win_wptool,n_structures,  ...
  419.                        ind_tree_st,tree_struct,  ...
  420.                        ind_data_st,data_struct);
  421.         % Decomposition drawing
  422.         %----------------------
  423.         wp1ddraw('anal',win_wptool);
  424.         % End waiting.
  425.         %-------------
  426.         wwaiting('off',win_wptool);
  427.     case 'comp'
  428.         mousefrm(0,'watch');
  429.         drawnow;
  430.         wp1dutil('enable',win_wptool,option);
  431.         out1 = feval('wp1dcomp','create',win_wptool);
  432.     case 'deno'
  433.         mousefrm(0,'watch');
  434.         drawnow;
  435.         wp1dutil('enable',win_wptool,option);
  436.         out1 = feval('wp1ddeno','create',win_wptool);
  437.     case {'return_comp','return_deno'}
  438.         % in3 = 1 : preserve compression
  439.         % in3 = 0 : discard compression
  440.         % in4 = hdl_line (optional)
  441.         %--------------------------------------
  442.         if in3==1
  443.             % Begin waiting.
  444.             %--------------
  445.             wwaiting('msg',win_wptool,'Wait ... drawing');
  446.             if strcmp(option,'return_comp')
  447.                 namesig = 'cs';
  448.             else
  449.                 namesig = 'ds';
  450.             end
  451.             wpssnode('plot',win_wptool,namesig,1,in4,[]);
  452.             % End waiting.
  453.             %-------------
  454.             wwaiting('off',win_wptool);
  455.         end
  456.         wp1dutil('enable',win_wptool,option);
  457.     otherwise
  458.         errargt(mfilename,'Unknown Option','msg');
  459.         error('*');
  460. end