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

波变换

开发平台:

Matlab

  1. function [out1,out2,out3,out4] = dw1dfile(option,win_dw1dtool,in3,in4)
  2. %DW1DFILE Discrete wavelet 1-D file manager.
  3. %   [OUT1,OUT2,OUT3,OUT4] = DW1DFILE(OPTION,WIN_DW1DTOOL,IN3,IN4)
  4. %   M. Misiti, Y. Misiti, G. Oppenheim, J.M. Poggi 12-Mar-96.
  5. %   Last Revision: 27-Jun-2001.
  6. %   Copyright 1995-2002 The MathWorks, Inc.
  7. %   $Revision: 1.17 $
  8. % MemBloc0 of stored values.
  9. %---------------------------
  10. n_InfoInit   = 'DW1D_InfoInit';
  11. ind_filename =  1;
  12. ind_pathname =  2;
  13. nb0_stored   =  2;
  14. % MemBloc1 of stored values.
  15. %---------------------------
  16. n_param_anal   = 'DWAn1d_Par_Anal';
  17. ind_sig_name   = 1;
  18. ind_sig_size   = 2;
  19. ind_wav_name   = 3;
  20. ind_lev_anal   = 4;
  21. ind_axe_ref    = 5;
  22. ind_act_option = 6;
  23. ind_ssig_type  = 7;
  24. ind_thr_val    = 8;
  25. nb1_stored     = 8;
  26. % MemBloc2 of stored values.
  27. %---------------------------
  28. n_coefs_longs = 'Coefs_and_Longs';
  29. ind_coefs     = 1;
  30. ind_longs     = 2;
  31. nb2_stored    = 2;
  32. % MemBloc3 of stored values.
  33. %---------------------------
  34. n_synt_sig = 'Synt_Sig';
  35. ind_ssig   =  1;
  36. nb3_stored =  1;
  37. % MemBloc4 of stored values.
  38. %---------------------------
  39. n_miscella     = 'DWAn1d_Miscella';
  40. ind_graph_area =  1;
  41. ind_view_mode  =  2;
  42. ind_savepath   =  3;
  43. nb4_stored     =  3;
  44. % Figure handle.
  45. %---------------
  46. numfig = int2str(win_dw1dtool);
  47. % Default values.
  48. %---------------- 
  49. epsilon = 0.01;
  50. nbMinPt = 20;
  51. switch option
  52.     case 'anal'
  53.         %******************************************************%
  54.         %** OPTION = 'anal' -  Computing and saving Analysis.**%
  55.         %******************************************************%
  56.         % in3 optional (for 'load_dec' or 'synt' or 'new_anal')
  57.         %------------------------------------------------------
  58.         if nargin==2
  59.             numopt = 1;
  60.         elseif strcmp(in3,'new_anal')
  61.             numopt = 2;
  62.         else
  63.             numopt = 3;
  64.         end     
  65.         % Getting  Analysis parameters.
  66.         %------------------------------
  67.         [Signal_Name,Signal_Size,Wave_Name,Level_Anal] =   ...
  68.                 wmemtool('rmb',win_dw1dtool,n_param_anal,  ...
  69.                                ind_sig_name,ind_sig_size, ...
  70.                                ind_wav_name,ind_lev_anal  ...
  71.                                );
  72.         pathname = wmemtool('rmb',win_dw1dtool,n_InfoInit,ind_pathname);
  73.         filename = wmemtool('rmb',win_dw1dtool,n_InfoInit,ind_filename);
  74.         if numopt<3
  75.             if numopt==1
  76.                 try
  77.                     load([pathname filename],'-mat');
  78.                     Signal_Anal = eval(Signal_Name);
  79.                     if size(Signal_Anal,1)>1 , Signal_Anal = Signal_Anal'; end         
  80.                 catch
  81.                     [Signal_Anal,ok] = utguidiv('direct_load_sig',win_dw1dtool,pathname,filename);
  82.                     if ~ok
  83.                         msg = sprintf('File %s is not a valid file.',filename);
  84.                         wwaiting('off',win_dw1dtool);
  85.                         errordlg(msg,'Load Signal ERROR','modal');
  86.                         return
  87.                     end
  88.                 end
  89.             else
  90.                 Signal_Anal = dw1dfile('sig',win_dw1dtool);
  91.             end
  92.             [coefs,longs] = wavedec(Signal_Anal,Level_Anal,Wave_Name);
  93.             % Writing coefficients.
  94.             %----------------------
  95.             wmemtool('wmb',win_dw1dtool,n_coefs_longs,...
  96.                            ind_coefs,coefs,ind_longs,longs);
  97.         else
  98.             [coefs,longs] = wmemtool('rmb',win_dw1dtool,n_coefs_longs,...
  99.                                            ind_coefs,ind_longs);
  100.         end
  101.         % Cleaning files.
  102.         %----------------
  103.         dw1dfile('del',win_dw1dtool);
  104.         % Test for saving.
  105.         %-----------------
  106.         try
  107.           err = 0;
  108.           sig_rec = 1;
  109.           save(['sig_rec.' numfig],'sig_rec','-mat');
  110.         catch
  111.           err = 1;
  112.         end
  113.         if err==0
  114.             % Computing and Saving Approximations
  115.             %------------------------------------
  116.             pathname = cd;
  117.             if ~isequal(pathname(length(pathname)),filesep)
  118.                 pathname = [pathname filesep];
  119.             end
  120.             wmemtool('wmb',win_dw1dtool,n_miscella,...
  121.                            ind_savepath,pathname);
  122.             app_rec = wrmcoef('a',coefs,longs,Wave_Name);
  123.             if nargin==2
  124.                 sig_rec = Signal_Anal;
  125.             else
  126.                 sig_rec = app_rec(1,:);
  127.             end
  128.             save(['sig_rec.' numfig],'sig_rec','-mat');
  129.             clear sig_rec
  130.             ssig_rec = app_rec(1,:);
  131.             save(['ssig_rec.' numfig],'ssig_rec','-mat');
  132.             if nargin==3 , out1 = ssig_rec; end
  133.             wmemtool('wmb',win_dw1dtool,n_synt_sig,ind_ssig,ssig_rec);
  134.             clear ssig_rec
  135.             app_rec = app_rec(2:Level_Anal+1,:);
  136.             save(['app_rec.' numfig],'app_rec','-mat');
  137.             clear app_rec
  138.             % Computing and Saving Details
  139.             %-------------------------------
  140.             det_rec = wrmcoef('d',coefs,longs,Wave_Name);
  141.             save(['det_rec.' numfig],'det_rec','-mat');
  142.             clear det_rec
  143.             % Computing and Saving Coefficients
  144.             %----------------------------------
  145.             cfs_beg = wrepcoef(coefs,longs);
  146.             save(['cfs_beg.' numfig],'cfs_beg','-mat');
  147.         else
  148.             out1 = wrcoef('a',coefs,longs,Wave_Name);
  149.             wmemtool('wmb',win_dw1dtool,n_synt_sig,ind_ssig,out1);
  150.         end
  151.     case 'comp_ss'
  152.         %***********************************************************%
  153.         %** OPTION = 'comp_ss' -  Computing and saving Synt. Sig. **%
  154.         %***********************************************************%
  155.         % Used by return_comp & return_deno
  156.         % in3 = hdl_lin
  157.         %------------------------------------
  158.         ssig_rec = get(in3,'Ydata');
  159.         pathname = wmemtool('rmb',win_dw1dtool,n_miscella,ind_savepath);
  160.         filename = ['ssig_rec.' numfig];
  161.         saveStr  = 'ssig_rec';
  162.         try
  163.           save([pathname filename],saveStr)
  164.         catch
  165.           wmemtool('wmb',win_dw1dtool,n_synt_sig,ind_ssig,ssig_rec);
  166.         end
  167.     case 'app'
  168.         pathname = wmemtool('rmb',win_dw1dtool,n_miscella,ind_savepath);
  169.         filename = ['app_rec.' numfig];
  170.         try
  171.           load([pathname filename],'-mat')
  172.           out1 = app_rec(in3,:);
  173.         catch
  174.           [coefs,longs] = wmemtool('rmb',win_dw1dtool,n_coefs_longs,...
  175.                                          ind_coefs,ind_longs);
  176.           Wave_Name = wmemtool('rmb',win_dw1dtool,n_param_anal,ind_wav_name);
  177.           out1 = wrmcoef('a',coefs,longs,Wave_Name,in3);
  178.         end
  179.         if nargin<4 , return; end;
  180.         switch in4
  181.             case 1
  182.                 mx = (2.^(in3+1));
  183.                 lx = size(out1,2);
  184.                 l3 = length(in3);
  185.                 out2 = zeros(1,l3);
  186.                 out4 = epsilon*ones(1,l3);
  187.                 out3 = -out4;
  188.                 for k = 1:l3
  189.                     bord = mx(k);
  190.                     lrem = lx+1-2*bord;
  191.                     if lrem>nbMinPt
  192.                         out2(k) = 1;
  193.                         xmax = lrem+bord;
  194.                         out3(k) = min(out1(k,bord:xmax))-epsilon;
  195.                         out4(k) = max(out1(k,bord:xmax))+epsilon;
  196.                     end
  197.                 end
  198.             case 2
  199.                 out2 = (min(out1'))'-epsilon;
  200.                 out3 = (max(out1'))'+epsilon;
  201.             case 3
  202.                 mx  = (2.^(in3+1));
  203.                 lx  = size(out1,2);
  204.                 lx2 = lx/2;
  205.                 l3  = length(in3);
  206.                 for k = 1:l3
  207.                     bord = mx(k);
  208.                     lrem = lx+1-2*bord;
  209.                     if lrem>nbMinPt
  210.                         xmax = lrem+bord;
  211.                         out2(k) = min(out1(k,bord:xmax))-epsilon;
  212.                         out3(k) = max(out1(k,bord:xmax))+epsilon;
  213.                     else
  214.                         out2(k) = min(out1(k,:))-epsilon;
  215.                         out3(k) = max(out1(k,:))+epsilon;
  216.                     end
  217.                 end
  218.         end
  219.     case 'det'
  220.         pathname = wmemtool('rmb',win_dw1dtool,n_miscella,ind_savepath);
  221.         filename = ['det_rec.' numfig];
  222.         try
  223.           load([pathname filename],'-mat')
  224.           out1 = det_rec(in3,:);
  225.         catch
  226.           [coefs,longs] = wmemtool('rmb',win_dw1dtool,n_coefs_longs,...
  227.                                          ind_coefs,ind_longs);
  228.           Wave_Name = wmemtool('rmb',win_dw1dtool,n_param_anal,ind_wav_name);
  229.           out1 = wrmcoef('d',coefs,longs,Wave_Name,in3);
  230.         end
  231.         if nargin<4 , return; end;
  232.         if in4==1
  233.             mx = (2.^(in3+1));
  234.             lx = size(out1,2);
  235.             l3 = length(in3);
  236.             out2 = zeros(1,l3);
  237.             out4 = epsilon*ones(1,l3);
  238.             out3 = -out4;
  239.             for k = 1:l3
  240.                 bord = mx(k);
  241.                 lrem = lx+1-2*bord;
  242.                 if lrem>nbMinPt
  243.                     out2(k) = 1;
  244.                     xmax = lrem+bord;
  245.                     out3(k) = min(out1(k,bord:xmax))-epsilon;
  246.                     out4(k) = max(out1(k,bord:xmax))+epsilon;
  247.                 end
  248.             end
  249.         elseif in4==2
  250.             out2 = (min(out1'))'-epsilon;
  251.             out3 = (max(out1'))'+epsilon;
  252.         end
  253.     case 'sig'
  254.         pathname = wmemtool('rmb',win_dw1dtool,n_miscella,ind_savepath);
  255.         filename = ['sig_rec.' numfig];
  256.         try
  257.           load([pathname filename],'-mat')
  258.           out1 = sig_rec;
  259.         catch
  260.           [coefs,longs] = wmemtool('rmb',win_dw1dtool,n_coefs_longs,...
  261.                                          ind_coefs,ind_longs);
  262.           Wave_Name = wmemtool('rmb',win_dw1dtool,n_param_anal,ind_wav_name);
  263.           out1 = wrcoef('a',coefs,longs,Wave_Name,0);
  264.         end
  265.         if nargin==3
  266.             out2 = min(out1)-epsilon;
  267.             out3 = max(out1)+epsilon;
  268.         end
  269.     case 'ssig'
  270.         pathname = wmemtool('rmb',win_dw1dtool,n_miscella,ind_savepath);
  271.         filename = ['ssig_rec.' numfig];
  272.         try
  273.           load([pathname filename],'-mat')
  274.           out1 = ssig_rec;
  275.         catch
  276.           out1 = wmemtool('rmb',win_dw1dtool,n_synt_sig,ind_ssig);
  277.         end
  278.         if nargin==3
  279.             out2 = min(out1)-epsilon;
  280.             out3 = max(out1)+epsilon;
  281.         end
  282.     case 'cfs_beg'
  283.         pathname = wmemtool('rmb',win_dw1dtool,n_miscella,ind_savepath);
  284.         filename = ['cfs_beg.' numfig];
  285.         try
  286.           load([pathname filename],'-mat')
  287.         catch
  288.           [coefs,longs] = wmemtool('rmb',win_dw1dtool,n_coefs_longs,...
  289.                                          ind_coefs,ind_longs);
  290.           cfs_beg = wrepcoef(coefs,longs);
  291.         end
  292.         out1 = cfs_beg(in3,:);
  293.         if nargin<4 , return; end;
  294.         if in4==1
  295.             mx = (2.^(in3+1));
  296.             lx = size(out1,2);
  297.             l3 = length(in3);
  298.             out2 = zeros(1,l3);
  299.             out4 = epsilon*ones(1,l3);
  300.             out3 = -out4;
  301.             for k = 1:l3
  302.                 bord = mx(k);
  303.                 lrem = lx+1-2*bord;
  304.                 if lrem>nbMinPt
  305.                     out2(k) = 1;
  306.                     xmax = lrem+bord;
  307.                     out3(k) = min(out1(k,bord:xmax))-epsilon;
  308.                     out4(k) = max(out1(k,bord:xmax))+epsilon;
  309.                 end
  310.             end
  311.         elseif in4==2
  312.             out2 = (min(out1'))'-epsilon;
  313.             out3 = (max(out1'))'+epsilon;
  314.         end
  315.     case 'app_cfs'
  316.         [coefs,longs] = wmemtool('rmb',win_dw1dtool,n_coefs_longs,...
  317.                                        ind_coefs,ind_longs);
  318.         Wave_Name = wmemtool('rmb',win_dw1dtool,n_param_anal,ind_wav_name);
  319.         out1 = appcoef(coefs,longs,Wave_Name,in3);
  320.         if nargin<4 , return; end;
  321.         if in4==1
  322.             bord = (2.^(in3+1));
  323.             lx = size(out1,2);
  324.             out2 = 0;
  325.             out3 = -epsilon;
  326.             out4 = epsilon;
  327.             lrem = lx+1-2*bord;
  328.             if lrem>nbMinPt
  329.                 out2 = 1;
  330.                 xmax = lrem+bord;
  331.                 out3 = min(out1(bord:xmax))-epsilon;
  332.                 out4 = max(out1(bord:xmax))+epsilon;
  333.             end
  334.         elseif in4==2
  335.             out2 = (min(out1'))'-epsilon;
  336.             out3 = (max(out1'))'+epsilon;
  337.         end
  338.     case 'det_cfs'
  339.         [coefs,longs] = wmemtool('rmb',win_dw1dtool,n_coefs_longs,...
  340.                                        ind_coefs,ind_longs);
  341.         out1 = detcoef(coefs,longs,in3);
  342.         if nargin<4 , return; end;
  343.         if in4==1
  344.             bord = (2.^(in3+1));
  345.             lx = size(out1,2);
  346.             out2 = 0;
  347.             out3 = -epsilon;
  348.             out4 = epsilon;
  349.             lrem = lx+1-2*bord;
  350.             if lrem>nbMinPt
  351.                 out2 = 1;
  352.                 xmax = lrem+bord;
  353.                 out3 = min(out1(bord:xmax))-epsilon;
  354.                 out4 = max(out1(bord:xmax))+epsilon;
  355.             end
  356.         elseif in4==2
  357.             out2 = (min(out1'))'-epsilon;
  358.             out3 = (max(out1'))'+epsilon;
  359.         end
  360.     case 'del'
  361.         %************************************%
  362.         %** OPTION = 'del' -  Delete files.**%
  363.         %************************************%
  364.         pathname = wmemtool('rmb',win_dw1dtool,n_miscella,ind_savepath);
  365.         if ~isempty(pathname)
  366.            olddir = cd;
  367.            try   , cd(pathname);
  368.            catch , return;
  369.            end
  370.            sig_file = ['sig_rec.' numfig];
  371.            deleteFile(sig_file)
  372.            ssig_file = ['ssig_rec.' numfig];
  373.            deleteFile(ssig_file);
  374.            app_file = ['app_rec.' numfig];
  375.            deleteFile(app_file);
  376.            det_file = ['det_rec.' numfig];
  377.            deleteFile(det_file);
  378.            cfs_file = ['cfs_beg.' numfig];
  379.            deleteFile(cfs_file);
  380.            cd(olddir);
  381.         end
  382.         wmemtool('wmb',win_dw1dtool,n_miscella,ind_savepath,'');
  383.     otherwise
  384.         errargt(mfilename,'Unknown Option','msg');
  385.         error('*');
  386. end
  387. %----------------------------
  388. function deleteFile(f)
  389. if exist(f)==2 ,
  390.     try , delete(f); end
  391. end
  392. %----------------------------