sig2ma.m
上传用户:joshua
上传日期:2008-09-12
资源大小:394k
文件大小:12k
源码类别:

matlab例程

开发平台:

Matlab

  1. function [ma,ASAsellog,ASAcontrol] = sig2ma(sig,cand_order,last)
  2. %SIG2MA MA model identification
  3. %   [MA,SELLOG] = SIG2MA(SIG) estimates moving average models from the 
  4. %   data vector SIG and selects a model with optimal predictive 
  5. %   qualities. The selected model is returned in the parameter vector MA. 
  6. %   The structure SELLOG provides additional information on the selection 
  7. %   process.
  8. %   
  9. %   SIG2MA(SIG,CAND_ORDER) selects only from candidate models whose 
  10. %   orders are entered in CAND_ORDER. CAND_ORDER must either be a row of 
  11. %   ascending orders, or a single order (in which case no true order 
  12. %   selection is performed).
  13. %   
  14. %   Without user intervention, the mean of SIG is subtracted from the 
  15. %   data. To control the subtraction of the mean, see the help topics on 
  16. %   ASAGLOB_SUBTR_MEAN and ASAGLOB_MEAN_ADJ.
  17. %     
  18. %   SIG2MA is an ARMASA main function.
  19. %   
  20. %   See also: SIG2AR, SIG2ARMA, ARMASEL.
  21. %   References: P. M. T. Broersen, Autoregressive Model Orders for
  22. %               Durbin's MA and ARMA estimators, IEEE Transactions on
  23. %               Signal Processing, Vol. 48, No. 8, August 2000,
  24. %               pp. 2454-2457.
  25. %Header
  26. %=========================================================================
  27. %Declaration of variables
  28. %------------------------
  29. %Declare and assign values to local variables
  30. %according to the input argument pattern
  31. switch nargin
  32. case 1 
  33.    if isa(sig,'struct'), ASAcontrol=sig; sig=[];
  34.    else, ASAcontrol=[];
  35.    end
  36.    cand_order=[];
  37. case 2 
  38.    if isa(cand_order,'struct'), ASAcontrol=cand_order; cand_order=[]; 
  39.    else, ASAcontrol=[]; 
  40.    end
  41. case 3 
  42.    if isa(last,'struct'), ASAcontrol=last;
  43.    else, error(ASAerr(39))
  44.    end
  45. otherwise
  46.    error(ASAerr(1,mfilename))
  47. end
  48. if isequal(nargin,1) & ~isempty(ASAcontrol)
  49.       %ASAcontrol is the only input argument
  50.    ASAcontrol.error_chk = 0;
  51.    ASAcontrol.run = 0;
  52. end
  53. %Declare ASAglob variables 
  54. ASAglob = {'ASAglob_subtr_mean';'ASAglob_mean_adj'; ...
  55.       'ASAglob_rc';'ASAglob_ar';'ASAglob_final_f'; ...
  56.       'ASAglob_final_b';'ASAglob_ar_cond'};
  57. %Assign values to ASAglob variables by screening the
  58. %caller workspace
  59. for ASAcounter = 1:length(ASAglob)
  60.    ASAvar = ASAglob{ASAcounter};
  61.    eval(['global ' ASAvar]);
  62.    if evalin('caller',['exist(''' ASAvar ''',''var'')'])
  63.       eval([ASAvar '=evalin(''caller'',ASAvar);']);
  64.    else
  65.       eval([ASAvar '=[];']);
  66.    end
  67. end
  68. %ARMASA-function version information
  69. %-----------------------------------
  70. %This ARMASA-function is characterized by
  71. %its current version,
  72. ASAcontrol.is_version = [2000 12 30 20 0 0];
  73. %and its compatability with versions down to,
  74. ASAcontrol.comp_version = [2000 12 30 20 0 0];
  75. %This function calls other functions of the ARMASA
  76. %toolbox. The versions of these other functions must
  77. %be greater than or equal to:
  78. ASAcontrol.req_version.burg = [2000 12 30 20 0 0];
  79. ASAcontrol.req_version.cic = [2000 12 30 20 0 0];
  80. ASAcontrol.req_version.rc2arset = [2000 12 30 20 0 0];
  81. ASAcontrol.req_version.cov2arset = [2000 12 30 20 0 0];
  82. ASAcontrol.req_version.armafilter = [2000 12 12 14 0 0];
  83. ASAcontrol.req_version.convol = [2000 12 6 12 17 20];
  84. ASAcontrol.req_version.convolrev = [2000 12 6 12 17 20];
  85. %Checks
  86. %------
  87. if ~any(strcmp(fieldnames(ASAcontrol),'error_chk')) | ASAcontrol.error_chk
  88.    %Perform standard error checks
  89.    %Input argument format checks
  90.    ASAcontrol.error_chk = 1;
  91.    if ~isnum(sig)
  92.       error(ASAerr(11,'sig'))
  93.    end
  94.    if ~isvector(sig)
  95.       error([ASAerr(14) ASAerr(15,'sig')])
  96.    elseif size(sig,2)>1
  97.       sig = sig(:);
  98.       warning(ASAwarn(25,{'row';'sig';'column'},ASAcontrol))
  99.    end
  100.    if ~isempty(cand_order)
  101.       if ~isnum(cand_order) | ~isintvector(cand_order) |...
  102.             cand_order(1)<0 | ~isascending(cand_order)
  103.          error(ASAerr(12,{'candidate';'cand_order'}))
  104.       elseif size(cand_order,1)>1
  105.          cand_order = cand_order';
  106.          warning(ASAwarn(25,{'column';'cand_order';'row'},ASAcontrol))
  107.       end
  108.    end
  109.    
  110.    %Input argument value checks
  111.    if ~isreal(sig)
  112.       error(ASAerr(13))
  113.    end
  114.    if max(cand_order) > length(sig)-1
  115.       error(ASAerr(21))
  116.    end
  117. end
  118. if ~any(strcmp(fieldnames(ASAcontrol),'version_chk')) | ASAcontrol.version_chk
  119.       %Perform version check
  120.    ASAcontrol.version_chk = 1;
  121.       
  122.    %Make sure the requested version of this function
  123.    %complies with its actual version
  124.    ASAversionchk(ASAcontrol);
  125.    
  126.    %Make sure the requested versions of the called
  127.    %functions comply with their actual versions
  128.    burg(ASAcontrol);
  129.    cic(ASAcontrol);
  130.    rc2arset(ASAcontrol);
  131.    cov2arset(ASAcontrol);
  132.    armafilter(ASAcontrol);
  133.    convol(ASAcontrol);
  134.    convolrev(ASAcontrol);
  135. end
  136. if ~any(strcmp(fieldnames(ASAcontrol),'run')) | ASAcontrol.run
  137.       %Run the computational kernel
  138.    ASAcontrol.run = 1;
  139.    ASAcontrol.version_chk = 0;
  140.    ASAcontrol.error_chk = 0;
  141.    ASAtime = clock;
  142.    ASAdate = now;
  143. %Main   
  144. %================================================================================================
  145.   
  146. %Initialization of variables
  147. %---------------------------
  148. if isempty(ASAglob_subtr_mean) | ASAglob_subtr_mean
  149.    sig = sig-mean(sig);
  150.    if isempty(ASAglob_mean_adj)
  151.       ASAglob_mean_adj = 1;
  152.    end
  153. elseif isempty(ASAglob_mean_adj)
  154.    ASAglob_mean_adj = 0;
  155. end
  156. n_obs = length(sig);
  157. ar_stack = cell(4,1);
  158. ar_entry = ones(1,4);
  159. rc = [];
  160. ma_sel = 1;
  161. %Combined determination of the maximum candidate MA
  162. %order and the max. candidate sliding AR order 
  163. %--------------------------------------------------
  164. def_max_ma_order = min(fix(n_obs/5),fix(80*log10(n_obs)));
  165. if def_max_ma_order > 400; 
  166.    def_max_ma_order = 400;
  167. end
  168. if isempty(cand_order)
  169.    cand_order = 0:def_max_ma_order;
  170. end
  171. max_ma_order = cand_order(end);
  172. if max_ma_order <= def_max_ma_order
  173.    max_slid_ar_order = fix(2.5*def_max_ma_order);
  174. else
  175.    max_slid_ar_order = fix(2.5*max_ma_order);
  176.    if max_slid_ar_order > n_obs-1
  177.       max_slid_ar_order = n_obs-1;
  178.    end
  179. end
  180. %Preparations for the estimation procedure
  181. %-----------------------------------------
  182. l_cand_order = length(cand_order);
  183. ma = zeros(1,max_ma_order);
  184. var = sig'*sig/n_obs;
  185. if cand_order(1)==0
  186.    zero_incl = 1;
  187.    gic3 = zeros(1,l_cand_order);
  188.    gic3(1) = log(var)+3/n_obs;
  189.    pe_est = zeros(1,l_cand_order);
  190.    if ASAglob_mean_adj
  191.       pe_est(1) = var*(n_obs+1)/(n_obs-1);
  192.    else
  193.       pe_est(1) = var;
  194.    end
  195. else
  196.    zero_incl = 0;
  197.    cand_order = [0 cand_order];
  198.    gic3 = zeros(1,l_cand_order+1);
  199.    gic3(1) = inf;
  200.    pe_est = zeros(1,l_cand_order+1);
  201. end
  202. if max_ma_order > 0
  203.    %Conditioning AR orders to the previously selected AR model
  204.    if isequal(ASAglob_ar_cond,1) & ~isempty(ASAglob_ar)
  205.       ar = ASAglob_ar;
  206.       sel_ar_order = length(ar)-1;
  207.       if  2*sel_ar_order+max_ma_order < max_slid_ar_order
  208.          max_slid_ar_order = 2*sel_ar_order+max_ma_order;
  209.       end
  210.    end 
  211.    
  212.    %AR model estimation
  213.    l_rc = length(ASAglob_rc);
  214.    if l_rc>1
  215.       rc = ASAglob_rc;
  216.       if (l_rc < max_slid_ar_order+1)
  217.          if isempty(ASAglob_final_f)
  218.             ar_det = rc2arset(ASAglob_rc(1:end-1),ASAcontrol);
  219.             ASAglob_final_f = convol(sig,ar_det,l_rc-1,n_obs,ASAcontrol);
  220.             ASAglob_final_b = convolrev(ar_det,sig,l_rc-1,n_obs,ASAcontrol);
  221.          end
  222.          rc = [ASAglob_rc burg(ASAglob_final_f, ...
  223.                ASAglob_final_b,max_slid_ar_order+1-l_rc,ASAcontrol)];
  224.       end
  225.    else
  226.       rc = burg(sig,max_slid_ar_order,ASAcontrol);
  227.    end
  228.    %AR model order selection
  229.    if ~isequal(ASAglob_ar_cond,1) | isempty(ASAglob_ar)
  230.       rc(1) = 0;
  231.       res = var*cumprod(1-rc(1:max_slid_ar_order+1).^2);
  232.       rc(1) = 1;
  233.       [min_value,sel_location] = min(cic(res,n_obs,ASAcontrol));
  234.       sel_ar_order = sel_location-1;
  235.    end
  236.    
  237.    min_ma_order = max(1,cand_order(1));
  238.    
  239.    slid_ar_order = 2*sel_ar_order+min_ma_order;
  240.    if slid_ar_order > max_slid_ar_order
  241.       slid_ar_order = max_slid_ar_order;
  242.    elseif slid_ar_order < 3
  243.       slid_ar_order = min(3,max_slid_ar_order);
  244.    end
  245.    
  246.    pred_ar_order = min(3*sel_ar_order+min(9,1+fix(n_obs/10)),max_slid_ar_order);
  247.    
  248.    %Determine a minimum set of AR parameter vectors, as needed for the preparations
  249.    [cand_ar_order,ar_entry] = sort([sel_ar_order pred_ar_order slid_ar_order max_slid_ar_order]);
  250.    equal_entry = zeros(1,4);
  251.    [dummy,redirect] = sort(ar_entry);
  252.    equal_counter = 0;
  253.    for i = 2:4
  254.       if isequal(cand_ar_order(i),cand_ar_order(i-1));
  255.          equal_counter = equal_counter+1;
  256.          equal_entry(i) = i;
  257.          index = find(max(0,redirect-i+equal_counter));
  258.          redirect(index) = redirect(index)-1;
  259.       end
  260.    end
  261.    cand_ar_order(find(equal_entry)) = [];
  262.    ar_entry = redirect;
  263.    ar_stack = rc2arset(rc,cand_ar_order,ASAcontrol);
  264.    
  265.    ar_pred = ar_stack{ar_entry(2)};
  266.    l_ar_pred = length(ar_pred);
  267.    l_pred_sig = fix(n_obs/2);
  268.    pred_sig_rev = armafilter(zeros(l_pred_sig,1),ar_pred,1,...
  269.       sig(end:-1:1),convolrev(sig,ar_pred,1,l_ar_pred,ASAcontrol),ASAcontrol);
  270.    pred_sig = pred_sig_rev(end:-1:1);
  271.    
  272.    counter = 2;
  273.    req_counter = 2;
  274.    sel_index = 1;
  275.    ar_slid = zeros(1,max_slid_ar_order+1); 
  276.    ar_slid(1:slid_ar_order+1) = ar_stack{ar_entry(3)};
  277.    
  278.    %Estimation procedure and model order selection
  279.    %----------------------------------------------
  280.    
  281.    for order = min_ma_order:max_ma_order
  282.       if cand_order(req_counter)==order
  283.          ar_corr = convolrev(ar_slid(1:slid_ar_order+1),order,ASAcontrol);
  284.          ma = cov2arset(ar_corr,ASAcontrol);
  285.          
  286.          e = armafilter(sig,ma,1,filter(1,ma,pred_sig),pred_sig,ASAcontrol);
  287.          res = e'*e/n_obs;
  288.          gic3_temp = log(res)+3*(order+1)/n_obs;
  289.          gic3(req_counter) = gic3_temp;
  290.          if gic3_temp < gic3(sel_index)
  291.            sel_index = req_counter;
  292.            ma_sel = ma;
  293.          end
  294.          if ASAglob_mean_adj
  295.             pe_est(req_counter) = res*(n_obs+order+1)/(n_obs-order-1);
  296.          else
  297.             pe_est(req_counter) = res*(n_obs+order)/(n_obs-order);
  298.          end
  299.          req_counter = req_counter+1;            
  300.       end
  301.       
  302.       if slid_ar_order < max_slid_ar_order
  303.          slid_ar_order = slid_ar_order+1;
  304.          rc_temp = rc(slid_ar_order+1);
  305.          ar_slid(2:slid_ar_order) = ar_slid(2:slid_ar_order)+rc_temp*ar_slid(slid_ar_order:-1:2);
  306.          ar_slid(slid_ar_order+1) = rc_temp;
  307.       end
  308.       
  309.       counter = counter+1;
  310.    end
  311. end
  312. %Arranging output arguments
  313. %--------------------------
  314. ma = ma_sel;
  315. if ~zero_incl
  316.    gic3(1) = [];
  317.    pe_est(1) = [];
  318.    cand_order(1) =[];
  319. end
  320. if ~isempty(rc)
  321.    ASAglob_rc = rc;
  322. end
  323. if nargout>1
  324.    ASAsellog.funct_name = mfilename;
  325.    ASAsellog.funct_version = ASAcontrol.is_version;
  326.    ASAsellog.date_time = [datestr(ASAdate,8) 32 datestr(ASAdate,0)];
  327.    ASAsellog.comp_time = etime(clock,ASAtime);
  328.    ASAsellog.ma = ma_sel;
  329.    ASAsellog.ar_sel = ar_stack{ar_entry(1)};
  330.    ASAsellog.mean_adj = ASAglob_mean_adj;
  331.    ASAsellog.cand_order = cand_order;
  332.    ASAsellog.gic3 = gic3;
  333.    ASAsellog.pe_est = pe_est;
  334. end
  335. %Footer
  336. %=====================================================
  337. else %Skip the computational kernel
  338.    %Return ASAcontrol as the first output argument
  339.    if nargout>1
  340.       warning(ASAwarn(9,mfilename,ASAcontrol))
  341.    end
  342.    ma = ASAcontrol;
  343.    ASAcontrol = [];
  344. end
  345. %Program history
  346. %======================================================================
  347. %
  348. % Version                Programmer(s)          E-mail address
  349. % -------                -------------          --------------
  350. % former versions        P.M.T. Broersen        broersen@tn.tudelft.nl
  351. % [2000 12 30 20 0 0]    W. Wunderink           wwunderink01@freeler.nl