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

波变换

开发平台:

Matlab

  1. function varargout = wp1ddeno(option,varargin)
  2. %WP1DDENO Wavelet packets 1-D de-noising.
  3. %   VARARGOUT = WP1DDENO(OPTION,VARARGIN)
  4. %   M. Misiti, Y. Misiti, G. Oppenheim, J.M. Poggi 12-Mar-96.
  5. %   Last Revision: 03-Feb-2003.
  6. %   Copyright 1995-2004 The MathWorks, Inc.
  7. %   $Revision: 1.19.4.2 $
  8. % Memory Blocks of stored values.
  9. %================================
  10. % MB1 (main window).
  11. %-------------------
  12. n_param_anal   = 'WP1D_Par_Anal';
  13. ind_sig_name   = 1;
  14. ind_wav_name   = 2;
  15. ind_lev_anal   = 3;
  16. ind_ent_anal   = 4;
  17. ind_ent_par    = 5;
  18. ind_sig_size   = 6;
  19. ind_act_option = 7;
  20. ind_thr_val    = 8;
  21. nb1_stored     = 8;
  22. % MB3 (main window).
  23. %-------------------
  24. n_structures = 'Structures';
  25. ind_tree_st  = 1;
  26. ind_data_st  = 2;
  27. nb3_stored   = 2;
  28. % MB1 (local).
  29. %-------------
  30. n_misc_loc  = ['MB1_' mfilename];
  31. ind_sav_menus  = 1;
  32. ind_status     = 2;
  33. ind_win_caller = 3;
  34. ind_axe_datas  = 4;
  35. ind_hdl_datas  = 5;
  36. ind_cfsMode    = 6;
  37. nbLOC_1_stored = 6;
  38. % MB2 (local).
  39. %-------------
  40. n_thrDATA = 'thrDATA';
  41. ind_value = 1;
  42. nbLOC_2_stored = 1;
  43. % Tag property (Main Window).
  44. %----------------------------
  45. tag_pop_colm = 'Txt_PopM';
  46. tag_axe_cfs  = 'Axe_Cfs';
  47. if ~isequal(option,'create') , win_denoise = varargin{1}; end
  48. switch option
  49.     case 'create'
  50.         % Get Globals.
  51.         %-------------
  52.         [Def_Btn_Height,Y_Spacing,Def_FraBkColor] = ...
  53.             mextglob('get','Def_Btn_Height','Y_Spacing','Def_FraBkColor');
  54.         % Calling figure.
  55.         %----------------
  56.         win_caller = varargin{1};
  57.         str_win_caller = sprintf('%.0f',win_caller);
  58.         pop = wfindobj(win_caller,'style','popupmenu','tag',tag_pop_colm);
  59.         cfsMode = get(pop,'Value');
  60.         % Window initialization.
  61.         %----------------------
  62.         win_name = 'Wavelet Packet 1-D  --  De-noising';
  63.         [win_denoise,pos_win,win_units,str_win_denoise,...
  64.             frame0,pos_frame0,Pos_Graphic_Area,pus_close] = ...
  65.                 wfigmngr('create',win_name,'','ExtFig_CompDeno',...
  66.                             strvcat(mfilename,'cond'),1,1,0);
  67.         set(win_denoise,'userdata',win_caller);
  68.         if nargout>0 , varargout{1} = win_denoise; end
  69. % Add Help for Tool.
  70. %------------------
  71. wfighelp('addHelpTool',win_denoise,'Signal De-noising','WP1D_DENO_GUI');
  72. % Add Help Item.
  73. %----------------
  74. wfighelp('addHelpItem',win_denoise,'De-noising Procedure','DENO_PROCEDURE');
  75. wfighelp('addHelpItem',win_denoise,'Available Methods','COMP_DENO_METHODS');
  76.         % Menu construction for current figure.
  77.         %--------------------------------------
  78. m_save  = wfigmngr('getmenus',win_denoise,'save');
  79.         sav_menus(1) = uimenu(m_save,...
  80.                                 'Label','De-noised &Signal ',...
  81.                                 'Position',1,                   ...
  82.                                 'Enable','Off',                 ...
  83.                                 'Callback',                     ...
  84.                                 [mfilename '(''save_synt'','    ...
  85.                                         str_win_denoise ');']   ...
  86.                                 );
  87.         sav_menus(2) = uimenu(m_save,...
  88.                                 'Label','&Decomposition ', ...
  89.                                 'Position',2,                  ...
  90.                                 'Enable','Off',                ...
  91.                                 'Callback',                    ...
  92.                                 [mfilename '(''save_dec'','    ...
  93.                                         str_win_denoise ');']  ...
  94.                                 );
  95.         % Begin waiting.
  96.         %---------------
  97.         wwaiting('msg',win_denoise,'Wait ... initialization');
  98.         % Getting variables from wp1dtool figure memory block.
  99.         %-----------------------------------------------------
  100.         [WP_Tree,WP_Data] = wmemtool('rmb',win_caller,n_structures,...
  101.                                            ind_tree_st,ind_data_st);
  102.         depth = treedpth(WP_Tree);
  103.         [Sig_Name,Sig_Size,Wav_Name,Ent_Nam,Ent_Par] =      ...
  104.                  wmemtool('rmb',win_caller,n_param_anal, ...
  105.                                 ind_sig_name,ind_sig_size,...
  106.                                 ind_wav_name,ind_ent_anal,ind_ent_par);
  107.         Wav_Fam = wavemngr('fam_num',Wav_Name);
  108.         % General graphical parameters initialization.
  109.         %--------------------------------------------
  110.         dy = Y_Spacing;
  111.         % Command part of the window.
  112.         %============================
  113.         % Data, Wavelet and Level parameters.
  114.         %------------------------------------
  115.         xlocINI = pos_frame0([1 3]);
  116.         ytopINI = pos_win(4)-dy;
  117.         toolPos = utanapar('create_copy',win_denoise, ...
  118.                     {'xloc',xlocINI,'top',ytopINI},...
  119.                     {'n_s',{Sig_Name,Sig_Size},'wav',Wav_Name,'lev',depth} ...
  120.                     );
  121.         % Entropy parameters.
  122.         %--------------------
  123.         ytopENT = toolPos(2)-4*dy;
  124.         toolPos = utentpar('create_copy',win_denoise, ...
  125.                     {'xloc',xlocINI,'top',ytopENT,...
  126.                      'ent',{Ent_Nam,Ent_Par}} ...
  127.                     );
  128.         % Global De-noising tool.
  129.         %------------------------        
  130.         ytopTHR = toolPos(2)-4*dy;
  131.         utthrwpd('create',win_denoise,'toolOPT','wpdeno1', ...
  132.                  'xloc',xlocINI,'top',ytopTHR ...
  133.                  );
  134.         % Adding colormap GUI.
  135.         %---------------------
  136.         pop_pal_caller = cbcolmap('get',win_caller,'pop_pal');
  137.         prop_pal = get(pop_pal_caller,{'String','Value','Userdata'});
  138.         utcolmap('create',win_denoise, ...
  139.                  'xloc',xlocINI, ...
  140.                  'bkcolor',Def_FraBkColor, ...
  141.                  'briflag',0, ...
  142.                  'enable','on');
  143.         pop_pal_loc = cbcolmap('get',win_denoise,'pop_pal');
  144.         set(pop_pal_loc,'String',prop_pal{1},'Value',prop_pal{2}, ...
  145.                         'Userdata',prop_pal{3});
  146.         set(win_denoise,'Colormap',get(win_caller,'Colormap'));
  147.         % Axes construction.
  148.         %===================
  149.         % General graphical parameters initialization.
  150.         %--------------------------------------------
  151.         bdx     = 0.08*pos_win(3);
  152.         bdy     = 0.06*pos_win(4);
  153.         ecy     = 0.03*pos_win(4);
  154.         y_graph = 2*Def_Btn_Height+dy;
  155.         h_graph = pos_frame0(4)-y_graph;
  156.         w_graph = pos_frame0(1);
  157.         % Axes construction parameters.
  158.         %------------------------------
  159.         w_left     = (w_graph-3*bdx)/2;
  160.         x_left     = bdx;
  161.         w_right    = w_left;
  162.         x_right    = x_left+w_left+5*bdx/4;
  163.         n_axeright = 3;
  164.         ind_right  = n_axeright;
  165.         % Vertical separation.
  166.         %---------------------
  167.         w_fra   = 0.01*pos_win(3);
  168.         x_fra   = (w_graph-w_fra)/2;
  169.         uicontrol('Parent',win_denoise,...
  170.                   'Style','frame',...
  171.                   'Unit',win_units,...
  172.                   'Position',[x_fra,y_graph,w_fra,h_graph],...
  173.                   'Backgroundcolor',Def_FraBkColor ...
  174.                   );
  175.         % Building axes on the right part.
  176.         %---------------------------------
  177.         ecy_right = 2*ecy;
  178.         h_right =(h_graph-2*bdy-(n_axeright-1)*ecy_right)/n_axeright;
  179.         y_right = y_graph+bdy-ecy/2;
  180.         axe_datas = zeros(1,n_axeright);
  181.         pos_right = [x_right y_right w_right h_right];
  182.         for k = 1:n_axeright
  183.             axe_datas(k) = axes('Parent',win_denoise,...
  184.                                 'Units',win_units,...
  185.                                 'Position',pos_right,...
  186.                                 'Drawmode','fast',...
  187.                                 'Box','On'...
  188.                                 );
  189.             pos_right(2) = pos_right(2)+pos_right(4)+ecy_right;
  190.         end
  191.         set(axe_datas(1),'visible','off');
  192.         % Computing and Drawing Original Signal.
  193.         %---------------------------------------
  194.         Sig_Anal = get(wp1ddraw('r_orig',win_caller),'Ydata');
  195.         hdl_datas = [NaN NaN];
  196.         axeAct = axe_datas(3);
  197.         axes(axeAct)
  198.         curr_color = wtbutils('colors','sig');
  199.         xmin = 1;                       
  200.         xmax = length(Sig_Anal);       
  201.         hdl_datas(1) = line(xmin:xmax,Sig_Anal,...
  202.                             'erasemode','none', ...
  203.                             'Color',curr_color, ...
  204.                             'Parent',axeAct);
  205.         wtitle('Original signal','Parent',axeAct);
  206.         ylim = [min(Sig_Anal) max(Sig_Anal)];
  207.         if ylim(1)==ylim(2) , ylim = ylim+[-0.01 0.01]; end
  208.         set(axeAct,'Xlim',[xmin xmax],'Ylim',ylim);
  209.         % Displaying original details coefficients.
  210.         %------------------------------------------
  211.         axe_handles = findobj(get(win_caller,'Children'),'flat','type','axes');
  212.         WP_Axe_Cfs  = findobj(axe_handles,'flat','Tag',tag_axe_cfs);
  213.         xylim = get(WP_Axe_Cfs,{'Xlim','Ylim'});
  214.         ylim = get(WP_Axe_Cfs,'Ylim');
  215.         commonProp = {...
  216.             'Xlim',xylim{1}, ...
  217.             'Ylim',xylim{2}, ...
  218.             'YTicklabelMode','manual', ...
  219.             'YTicklabel',[], ...
  220.             'YTick',[],      ...
  221.             'Box','On'       ...
  222.             };
  223.         axeAct = axe_datas(2);
  224.         axes(axeAct)
  225.         wtitle('Original coefficients','Parent',axeAct);
  226.         set(axeAct,commonProp{:});
  227.         if isa(WP_Tree,'wptree')
  228.             wpplotcf(WP_Tree,cfsMode,axeAct);
  229.         else
  230.             wpplotcf(WP_Tree,WP_Data,cfsMode,axeAct);
  231.         end
  232.         set(axe_datas(1),commonProp{:});
  233.         % Initializing threshold.
  234.         %------------------------
  235.         [valTHR,maxTHR,cfs] = wp1ddeno('compute_GBL_THR',win_denoise,win_caller);
  236.         utthrwpd('setThresh',win_denoise,[0,valTHR,maxTHR]);
  237.         % Displaying perfos.
  238.         %-------------------
  239.         y_axe = y_graph+bdy+3*h_right/2+3*ecy;
  240.         h_axe = 3*h_right/2+ecy/2;
  241.         pos_axe_perfo = [x_left y_axe w_left h_axe];
  242.         y_axe = y_graph+bdy-ecy/2;
  243.         h_axe = 3*h_right/2+ecy/2;
  244.         pos_axe_histo = [x_left y_axe w_left h_axe];
  245.         utthrwpd('displayPerf',win_denoise,pos_axe_perfo,pos_axe_histo,cfs);
  246.         % Memory blocks update.
  247.         %----------------------
  248.         utthrwpd('set',win_denoise,'handleORI',hdl_datas(1));        
  249.         wmemtool('ini',win_denoise,n_misc_loc,nbLOC_1_stored);
  250.         wmemtool('wmb',win_denoise,n_misc_loc,    ...
  251.                        ind_sav_menus,sav_menus,   ...
  252.                        ind_status,0,              ...
  253.                        ind_win_caller,win_caller, ...
  254.                        ind_axe_datas,axe_datas,   ...
  255.                        ind_hdl_datas,hdl_datas,   ...
  256.                        ind_cfsMode,cfsMode        ...
  257.                        );
  258.         wmemtool('ini',win_denoise,n_thrDATA,nbLOC_2_stored);
  259.         % Axes attachment.
  260.         %-----------------
  261.         axe_cmd = axe_datas(3);
  262.         axe_act = axe_datas(1:2);
  263.         dynvtool('init',win_denoise,[],axe_cmd,axe_act,[1 0]);
  264.         % Setting units to normalized.
  265.         %-----------------------------
  266.         wfigmngr('normalize',win_denoise);
  267.         % End waiting.
  268.         %-------------
  269.         wwaiting('off',win_denoise);
  270.     case 'denoise'
  271.         wp1ddeno('clear_GRAPHICS',win_denoise);
  272.  
  273.         % Waiting message.
  274.         %-----------------
  275.         wwaiting('msg',win_denoise,'Wait ... computing');
  276.         % Getting memory blocks.
  277.         %-----------------------
  278.         [axe_datas,hdl_datas] = wmemtool('rmb',win_denoise,n_misc_loc, ...
  279.                                                ind_axe_datas,ind_hdl_datas);
  280.         win_caller = wmemtool('rmb',win_denoise,n_misc_loc,ind_win_caller);
  281.         [WP_Tree,WP_Data] = wmemtool('rmb',win_caller,n_structures,...
  282.                                            ind_tree_st,ind_data_st);
  283.         depth = treedpth(WP_Tree);
  284.         Wav_Name = wmemtool('rmb',win_caller,n_param_anal,ind_wav_name);
  285.         % De-noising depending on the selected thresholding mode.
  286.         %--------------------------------------------------------
  287.         [numMeth,meth,threshold,sorh] = utthrwpd('get_GBL_par',win_denoise);
  288.         thrParams = {sorh,'nobest',threshold,1};
  289.         if isa(WP_Tree,'wptree')
  290.             [C_Sig,C_Tree] = wpdencmp(WP_Tree,thrParams{:}); C_Data = [];
  291.         else
  292.             [C_Sig,C_Tree,C_Data] = wpdencmp(WP_Tree,WP_Data,thrParams{:});
  293.         end
  294.         % Displaying de-noised signal.
  295.         %-----------------------------
  296.         lin_deno = hdl_datas(2);
  297.         if ~ishandle(lin_deno)
  298.             curr_color  = wtbutils('colors','ssig');
  299.             lin_deno = line(...
  300.                             'Parent',axe_datas(3),...
  301.                             'Xdata',1:length(C_Sig),...
  302.                             'Ydata',C_Sig,...
  303.                             'color',curr_color);
  304.             hdl_datas(2) = lin_deno;
  305.             utthrwpd('set',win_denoise,'handleTHR',hdl_datas(2));
  306.             wmemtool('wmb',win_denoise,n_misc_loc,ind_hdl_datas,hdl_datas);
  307.         else
  308.             set(lin_deno,'Ydata',C_Sig,'Visible','on');
  309.         end
  310.         wtitle('Original and de-noised signals','Parent',axe_datas(3));
  311.         % Displaying thresholded details coefficients.
  312.         %---------------------------------------------
  313.         cfsMode = wmemtool('rmb',win_denoise,n_misc_loc,ind_cfsMode);
  314.         axeAct = axe_datas(1);
  315.         delete(findobj(axeAct,'type','image'));
  316.         if isa(C_Tree,'wptree')
  317.             wpplotcf(C_Tree,cfsMode,axeAct);
  318.         else
  319.             wpplotcf(C_Tree,C_Data,cfsMode,axeAct);
  320.         end
  321.         xylim = get(axe_datas(2),{'Xlim','Ylim'});
  322.         set(axeAct,'Xlim',xylim{1},'Ylim',xylim{2});
  323.         wtitle('Thresholded coefficients','Parent',axeAct);
  324.         set(findobj(axeAct),'Visible','on');       
  325.         % Memory blocks update.
  326.         %----------------------
  327.         wmemtool('wmb',win_denoise,n_thrDATA,ind_value,...
  328.                  {C_Sig,C_Tree,C_Data,threshold});
  329.         wp1ddeno('enable_menus',win_denoise,'on');
  330.  
  331.         % End waiting.
  332.         %-------------
  333.         wwaiting('off',win_denoise);
  334.     case 'compute_GBL_THR'
  335.         win_caller = varargin{2};
  336.         pause(0.01)
  337.         [numMeth,meth,threshold,sorh] = utthrwpd('get_GBL_par',win_denoise);
  338.         [WP_Tree,WP_Data] = wmemtool('rmb',win_caller,n_structures,...
  339.                                            ind_tree_st,ind_data_st);
  340.         [valTHR,maxTHR,cfs] = wthrmngr('wp1ddenoGBL',meth,WP_Tree,WP_Data);
  341.         if   nargout==1 , varargout = {valTHR};
  342.         else            , varargout = {valTHR,maxTHR,cfs};
  343.         end
  344.     case 'update_GBL_meth'
  345.         wp1ddeno('clear_GRAPHICS',win_denoise);
  346.         win_caller = wmemtool('rmb',win_denoise,n_misc_loc,ind_win_caller);
  347.         valTHR = wp1ddeno('compute_GBL_THR',win_denoise,win_caller);
  348.         utthrwpd('update_GBL_meth',win_denoise,valTHR);
  349.     case 'clear_GRAPHICS'
  350.         status = wmemtool('rmb',win_denoise,n_misc_loc,ind_status);
  351.         if status == 0 , return; end
  352.         % Diseable save Menus.
  353.         %--------------------
  354.         wp1ddeno('enable_menus',win_denoise,'off');
  355.         % Get Handles.
  356.         %-------------
  357.         [axe_datas,hdl_datas] = wmemtool('rmb',win_denoise,n_misc_loc, ...
  358.                                                ind_axe_datas,ind_hdl_datas);
  359.         % Setting the de-noised coefs axes invisible.
  360.         %--------------------------------------------
  361.         lin_deno = hdl_datas(2);
  362.         if ishandle(lin_deno)
  363.             vis = get(lin_deno,'Visible');
  364.             if vis(1:2)=='on'
  365.                 axes(axe_datas(3));
  366.                 set(lin_deno,'Visible','off');
  367.                 wtitle('Original signal','Parent',axe_datas(3));
  368.                 set(findobj(axe_datas(1)),'Visible','off');       
  369.             end
  370.         end
  371.         drawnow
  372.     case 'enable_menus'
  373.         enaVal = varargin{2};
  374.         sav_menus = wmemtool('rmb',win_denoise,n_misc_loc,ind_sav_menus);
  375.         set(sav_menus,'Enable',enaVal);
  376.         utthrwpd('enable_tog_res',win_denoise,enaVal);
  377.         if strncmpi(enaVal,'on',2) , status = 1; else , status = 0; end
  378.         wmemtool('wmb',win_denoise,n_misc_loc,ind_status,status);
  379.     case 'save_synt'
  380.        % Testing file.
  381.         %--------------
  382.         [filename,pathname,ok] = utguidiv('test_save',win_denoise, ...
  383.                                      '*.mat','Save De-noised Signal');
  384.         if ~ok, return; end
  385.         % Begin waiting.
  386.         %--------------
  387.         wwaiting('msg',win_denoise,'Wait ... saving');
  388.         % Getting Analysis values.
  389.         %-------------------------
  390.         win_caller = wmemtool('rmb',win_denoise,n_misc_loc,ind_win_caller);
  391.         wname = wmemtool('rmb',win_caller,n_param_anal,ind_wav_name);
  392.         thrDATA = wmemtool('rmb',win_denoise,n_thrDATA,ind_value);
  393.         xc = thrDATA{1};
  394.         valTHR = thrDATA{4};
  395.         % Saving file.
  396.         %--------------
  397.         [name,ext] = strtok(filename,'.');
  398.         if isempty(ext) | isequal(ext,'.')
  399.             ext = '.mat'; filename = [name ext];
  400.         end
  401.         saveStr = name;
  402.         eval([saveStr '= xc ;']);
  403.         wwaiting('off',win_denoise);
  404.         try
  405.           save([pathname filename],saveStr,'valTHR','wname');
  406.         catch
  407.           errargt(mfilename,'Save FAILED !','msg');
  408.         end
  409.     case 'save_dec'
  410.         % Testing file.
  411.         %--------------
  412.         [filename,pathname,ok] = utguidiv('test_save',win_denoise, ...
  413.                                    '*.wp1','Save Wavelet Packet Analysis (1D)');
  414.         if ~ok, return; end
  415.         % Begin waiting.
  416.         %--------------
  417.         wwaiting('msg',win_denoise,'Wait ... saving decomposition');
  418.         % Getting Analysis parameters.
  419.         %-----------------------------
  420.         win_caller = wmemtool('rmb',win_denoise,n_misc_loc,ind_win_caller);
  421.         data_name  = wmemtool('rmb',win_caller,n_param_anal,ind_sig_name);
  422.         % Getting Analysis values.
  423.         %-------------------------
  424.         thrDATA = wmemtool('rmb',win_denoise,n_thrDATA,ind_value);
  425.         tree_struct = thrDATA{2};
  426.         data_struct = thrDATA{3};
  427.         valTHR = thrDATA{4};
  428.         % Saving file.
  429.         %--------------
  430.         [name,ext] = strtok(filename,'.');
  431.         if isempty(ext) | isequal(ext,'.')
  432.             ext = '.wp1'; filename = [name ext];
  433.         end
  434.         if isa(tree_struct,'wptree')
  435.             saveStr = {'tree_struct','data_name','valTHR'};
  436.         else
  437.             saveStr = {'tree_struct','data_struct','data_name','valTHR'};
  438.         end
  439.         wwaiting('off',win_denoise);
  440.         try
  441.           save([pathname filename],saveStr{:});
  442.         catch
  443.           errargt(mfilename,'Save FAILED !','msg');
  444.         end
  445.     case 'close'
  446.         [status,win_caller] = wmemtool('rmb',win_denoise,n_misc_loc,...
  447.                                              ind_status,ind_win_caller);
  448.         if status==1
  449.             % Test for Updating.
  450.             %--------------------
  451.             status = wwaitans({win_denoise,'WP1D De-noising'},...
  452.                               'Update the synthesized signal ?',2,'cancel');
  453.         end
  454.         switch status
  455.           case 1
  456.             wwaiting('msg',win_denoise,'Wait ... computing');
  457.             thrDATA = wmemtool('rmb',win_denoise,n_thrDATA,ind_value);
  458.             valTHR  = thrDATA{4};
  459.             wmemtool('wmb',win_caller,n_param_anal,ind_thr_val,valTHR);
  460.             hdl_datas = wmemtool('rmb',win_denoise,n_misc_loc,ind_hdl_datas);
  461.             lin_deno = hdl_datas(2);
  462.             wp1dmngr('return_deno',win_caller,status,lin_deno);
  463.             wwaiting('off',win_denoise);
  464.           case 0 , wp1dmngr('return_deno',win_caller,status);
  465.         end
  466.         if nargout>0 , varargout{1} = status; end
  467.     otherwise
  468.         errargt(mfilename,'Unknown Option','msg');
  469.         error('*');
  470. end