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

波变换

开发平台:

Matlab

  1. function Fact_and_LS(IN1,IN2,IN3)
  2. %FACT_AND_LS Factorizations and lifting schemes for wavelets. 
  3. %   === NOT DOCUMENTED ===
  4. %   M. Misiti, Y. Misiti, G. Oppenheim, J.M. Poggi 30-May-2003.
  5. %   Last Revision 12-Nov-2003.
  6. %   Copyright 1995-2004 The MathWorks, Inc.
  7. %   $Revision: 1.1.6.3 $ $Date: 2004/04/13 00:39:36 $ 
  8. % Initialization.
  9. %----------------
  10. prec_To_Keep = 1.E-14;
  11. tolerance = 1.E-8;
  12. waveCell_ALL = wavenames('all');
  13. % FLAGS and type of display.
  14. %---------------------------
  15. filterFLAG = false;   % Flag for writing filters.
  16. fileFLAG   = true;     % Flag for writing on files.   
  17. typeDISP_VAL = {'fact' , 'LS'};
  18. % typeDISP_VAL = {'All'};
  19. % Defaults.
  20. %----------
  21. waveCell_DEFAULT = {...
  22.         'db1','db2','db3','db4','db5','db6',...
  23.         'sym2','sym3','sym4','sym5','sym6',...
  24.         'coif1','coif2',  ...
  25.     };
  26. % Check argument.
  27. %----------------
  28. if nargin<1
  29.     waveCell = waveCell_DEFAULT;
  30. elseif iscell(IN1)
  31.     waveCell = IN1;
  32. elseif isstr(IN1)
  33.     if isequal(IN1,'all')
  34.         waveCell = waveCell_ALL;
  35.     elseif isequal(IN1,'haar')
  36.         waveCell = {'db1'};
  37.     else
  38.         if any(strcmp(IN1,waveCell_ALL))
  39.             waveCell = {IN1};
  40.         else
  41.             error('Invalid wavelet name.')
  42.         end
  43.     end
  44. end
  45. % Power Max setting.
  46. %-------------------
  47. powMAX_AUTO = false;
  48. if nargin<2
  49.     powMAX_AUTO = true;
  50. elseif isequal(IN2,'auto')
  51.     powMAX_AUTO = true;
  52. elseif isequal(IN2,'set_1')
  53.     powMAX_VAL  = [0,1,NaN];
  54. elseif isnumeric(IN2)
  55.     powMAX_VAL  = IN2;
  56. end
  57. % Diff. Power setting.
  58. %---------------------
  59. difPOW_AUTO = false;
  60. if nargin<3
  61.     if ~powMAX_AUTO
  62.         difPOW_VAL = 0;
  63.     else
  64.         difPOW_AUTO = true;
  65.     end
  66. else
  67.     difPOW_VAL = IN3;
  68. end
  69. % Begin display.
  70. %---------------
  71. dispSTR_1 = '#====#====#====#====#====#====#====#====#====#====#====#====#====#====#';
  72. dispSTR_2 = '=================================';
  73. dispSTR_3 = '+---+---+---+---+';
  74. dispSTR_4 = '-------------------';
  75. nb_WAVE_CUR = length(waveCell);
  76. for k = 1:nb_WAVE_CUR
  77.     wname = waveCell{k};
  78.     
  79.     if powMAX_AUTO
  80.         TMP = wfilters(wname);
  81.         LEN = length(TMP);
  82.         powMAX_VAL  = [-LEN:LEN];
  83.         if difPOW_AUTO
  84.             difPOW_VAL = [0:2:LEN];
  85.         end
  86.     end
  87.     
  88.     typeDISP = typeDISP_VAL{1};
  89.     dispGENERAL_Info(typeDISP,fileFLAG,wname,dispSTR_1)
  90.     dispGENERAL_Info(typeDISP,fileFLAG,wname, ...
  91.         ['BEGIN - Current Wavelet: ' wname]);
  92.     dispGENERAL_Info(typeDISP,fileFLAG,wname,dispSTR_2)
  93.     
  94.     % Initialization.
  95.     All_dual_APMF = {};
  96.     All_prim_APMF = {};
  97.     All_dual_LS = {};
  98.     All_prim_LS = {};
  99.     All_dual_ERR = [];
  100.     All_prim_ERR = [];
  101.     All_prim_POW = [];
  102.     All_dual_POW = [];
  103.     All_prim_DIF = [];
  104.     All_dual_DIF = [];
  105.   
  106.     for j = 1:length(powMAX_VAL)
  107.         powMAX = powMAX_VAL(j);
  108.         dispGENERAL_Info(typeDISP,fileFLAG,wname, ...
  109.             ['Current power MAX: ' sprintf('%2.0f',powMAX)]);
  110.         for m = 1:length(difPOW_VAL)
  111.             difPOW = difPOW_VAL(m);
  112.             % Compute Laurent Polynomials associated to the wavelet.
  113.             [Hs,Gs,Ha,Ga] = wave2lp(wname,powMAX,difPOW);
  114.             
  115.             % Synthesis Polyphase Matrix, factorizations
  116.             % and Lifting Steps.
  117.             [MatFACT,PolyMAT] = ppmfact(Hs,Gs);
  118.             nbFACT = length(MatFACT);
  119.             % Compute and Display .... 
  120.             if nbFACT>0
  121.                 % Compute Analyzis Polyphase Matrix Factorizations.
  122.                 [dual_APMF,prim_APMF] = pmf2apmf(MatFACT,'twoFact');
  123.                 
  124.                 % Compute Lifting Steps.
  125.                 dual_LS = apmf2ls(dual_APMF);
  126.                 prim_LS = apmf2ls(prim_APMF);
  127.                 
  128.                 % Control the factorizations.
  129.                 [dual_errTAB,dual_errFlags] = errlsdec(dual_LS,tolerance);
  130.                 [prim_errTAB,prim_errFlags] = errlsdec(prim_LS,tolerance);
  131.                                
  132.                 % Display Current diff POW.
  133.                  dispGENERAL_Info(typeDISP,fileFLAG,wname, ...
  134.                     ['Current diff POW: ' sprintf('%2.0f',difPOW)]);
  135.                 % Display Laurent Polynomials Information. 
  136.                 dispLP_Info_INI(typeDISP,fileFLAG,wname,Ha,Ga,Hs,Gs)
  137.                 dispLP_Info(typeDISP,fileFLAG,wname,Hs,Gs,PolyMAT)
  138.                 
  139.                 % Display factorizations Information. 
  140.                 dispFACT_Info(typeDISP,fileFLAG,wname,nbFACT,dual_errTAB,dual_errFlags)
  141.                 dispFACT_Info(typeDISP,fileFLAG,wname,nbFACT,prim_errTAB,prim_errFlags)
  142.                 
  143.                 % Add new Factorizations and LS ... 
  144.                 All_dual_APMF = {All_dual_APMF{:} , dual_APMF{:}};
  145.                 All_prim_APMF = {All_prim_APMF{:} , prim_APMF{:}};
  146.                 All_dual_LS   = {All_dual_LS{:}   , dual_LS{:}}; 
  147.                 All_prim_LS   = {All_prim_LS{:}   , prim_LS{:}}; 
  148.                 All_dual_ERR  = [All_dual_ERR     , dual_errTAB];
  149.                 All_prim_ERR  = [All_prim_ERR     , prim_errTAB];
  150.                 All_dual_POW  = [All_dual_POW     , powMAX*ones(1,nbFACT)];
  151.                 All_prim_POW  = [All_prim_POW     , powMAX*ones(1,nbFACT)];
  152.                 All_dual_DIF  = [All_dual_DIF     , difPOW*ones(1,nbFACT)];
  153.                 All_prim_DIF  = [All_prim_DIF     , difPOW*ones(1,nbFACT)];
  154.                 
  155.             end
  156.             dispGENERAL_Info(typeDISP,fileFLAG,wname,dispSTR_3)
  157.             
  158.         end
  159.         dispGENERAL_Info(typeDISP,fileFLAG,wname,dispSTR_4)
  160.     end
  161.     
  162.     % Merge direct and reverse Factorizations
  163.     %----------------------------------------
  164.     All_FACT = {All_dual_APMF{:} , All_prim_APMF{:}}; 
  165.     All_LS  = {All_dual_LS{:}   , All_prim_LS{:}}; 
  166.     All_ERR = [All_dual_ERR     , All_prim_ERR];
  167.     All_POW = [All_dual_POW     , All_prim_POW];
  168.     All_DIF = [All_dual_DIF     , All_prim_DIF];
  169.     
  170.     % Suppress same factorizations.
  171.     %------------------------------
  172.     eqTAB = tablseq(All_LS,tolerance);
  173.     toDEL = [];
  174.     for j = 1:length(eqTAB)
  175.         num = eqTAB{j};
  176.         if ~isempty(num) && (num > j)
  177.             toDEL = [toDEL , num];
  178.         end
  179.     end
  180.     All_FACT(toDEL) = [];
  181.     All_LS(toDEL)  = [];
  182.     All_ERR(toDEL) = [];
  183.     All_POW(toDEL) = [];
  184.     All_DIF(toDEL) = [];
  185.     
  186.     for j = 1:length(typeDISP_VAL)
  187.         typeDISP = typeDISP_VAL{j};
  188.         switch typeDISP
  189.             case 'fact' , 
  190.                 dispFACT(typeDISP,fileFLAG,wname,All_FACT);
  191.             case 'LS'   , 
  192.                 dispLS(typeDISP,fileFLAG,wname,All_LS,...
  193.                     All_ERR,All_POW,All_DIF,filterFLAG);
  194.             case 'All'  ,
  195.                 dispFACT(typeDISP,fileFLAG,wname,All_FACT);
  196.                 dispLS(typeDISP,fileFLAG,wname,All_LS,...
  197.                     All_ERR,All_POW,All_DIF,filterFLAG);
  198.         end
  199.     end
  200.     dispGENERAL_Info(typeDISP,fileFLAG,wname,dispSTR_1)
  201. end
  202. %---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---%
  203. %---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---%
  204. function dispGENERAL_Info(typeDISP,fileFLAG,wname,dispSTR)
  205. openDIARY(0,typeDISP,fileFLAG,wname)
  206. disp(dispSTR)
  207. diary off
  208. %---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---%
  209. function dispLP_Info_INI(typeDISP,fileFLAG,wname,Ha,Ga,Hs,Gs)
  210. % Open diary.
  211. %------------ 
  212. openDIARY('fact',typeDISP,fileFLAG,wname)
  213. % Perfect Reconstruction and Anti-Aliasing Conditions.
  214. %-----------------------------------------------------
  215. CNS_5_2 = Hs * reflect(Ha) + Gs * reflect(Ga);
  216. CNS_5_3 = Hs * modulate(reflect(Ha)) + Gs * modulate(reflect(Ga));
  217. %--------------------------------------
  218. disp('##############################');
  219. disp(['Wavelet: ', wname])
  220. disp('----------------------');
  221. disp(disp(Hs));
  222. disp(disp(Gs));
  223. disp(disp(Ha));
  224. disp(disp(Ga));
  225. disp(disp(CNS_5_2));
  226. disp(disp(CNS_5_3));
  227. disp('----------------------');
  228. % Close diary.
  229. %-------------
  230. diary off
  231. %---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---%
  232. function dispLP_Info(typeDISP,fileFLAG,wname,Hs,Gs,PolyMAT)
  233. % Open diary.
  234. %------------
  235. openDIARY(0,typeDISP,fileFLAG,wname)
  236. % Display.
  237. %---------
  238. len_H   = length(lp2num(Hs));
  239. len_G   = length(lp2num(Gs));
  240. len_E_H = length(lp2num(PolyMAT{1,1}));
  241. len_O_H = length(lp2num(PolyMAT{2,1}));
  242. len_E_G = length(lp2num(PolyMAT{1,2}));
  243. len_O_G = length(lp2num(PolyMAT{2,2}));
  244. str2disp_1 = [...
  245.         'len_Hs = ' , sprintf('%2.0f',len_H) , ' - ' ,...
  246.         'len_Gs = ' , sprintf('%2.0f',len_G)  ...
  247.     ];
  248. str2disp_2 = [...    
  249.         'len_EvenHs = ' , sprintf('%2.0f',len_E_H) , ' - ' ,...
  250.         'len_OddHs  = ' , sprintf('%2.0f',len_O_H)  ...
  251.     ];
  252. str2disp_3 = [...    
  253.         'len_EvenGs = ' , sprintf('%2.0f',len_E_G) , ' - ' ,...
  254.         'len_OddGs  = ' , sprintf('%2.0f',len_O_G)  ...
  255.     ];
  256. disp(str2disp_1);
  257. disp('%---+---+---+---+---+---+---+---%')
  258. disp(str2disp_2);
  259. disp(str2disp_3);
  260. disp('%---+---+---+---+---+---+---+---+---+---+---%')
  261. % Close diary.
  262. %-------------
  263. diary off
  264. %---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---%
  265. %---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---%
  266. function dispFACT_Info(typeDISP,fileFLAG,wname,nbFACT,errTAB,errFlags)
  267. % Open diary.
  268. %------------
  269. openDIARY(0,typeDISP,fileFLAG,wname)
  270. % Display.
  271. %---------
  272. disp(['Number of factorizations: ' sprintf('%2.0f',nbFACT)]);
  273. disp(['Test for ' int2str(nbFACT) ' factorisation(s)'])
  274. disp(['Errors indic.: ' sprintf('%3.0f',find(errFlags==0))]);
  275. disp(['Errors values: ' sprintf('%12.8f',errTAB(errFlags==0))]);
  276. % Close diary.
  277. %-------------
  278. diary off
  279. %---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---%
  280. %---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---%
  281. function dispFACT(typeDISP,fileFLAG,wname,All_FACT);
  282. % Format.
  283. %--------
  284. format long
  285. % Open diary.
  286. %------------
  287. openDIARY(1,typeDISP,fileFLAG,wname)
  288. % Display.
  289. %----------
  290. nbFACT = length(All_FACT);
  291. disp('#################################################################')
  292. disp(['Number of factorizations for ' wname ': ' sprintf('%2.0f',nbFACT)])
  293. disp('====================================='); disp(' ')
  294. for k = 1:nbFACT
  295.     disp(['Decomposition n