home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 9.ddi / IDENT.DI$ / CRA.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  2.5 KB  |  74 lines

  1. function [ir,r,cl]=cra(z,M,n)
  2. %CRA    Performs correlation analysis
  3. %
  4. %    IR = cra(Z)
  5. %
  6. %    Z: The data, entered with two columns Z = [y u]
  7. %     IR: The estimated impulse response (IR(1) corresponds to g(0))
  8. %
  9. %    [IR,R,CL] = cra(Z,M,NA) gives access to
  10. %    M: The number of lags for which the functions are computed (def 20)
  11. %    NA: The order of the whitening filter.(Def 10). With NA=0, no prewhite-
  12. %        ning is performed. Then the covariance functions of the original
  13. %        data are obtained.
  14. %    R: The covariance/correlation information
  15. %       R(:,1) contains the lag indices
  16. %       R(:,2) contains the covariance function of y (poss. prewhitened)
  17. %       R(:,3) contains the covariance function of u (poss. prewhitened)
  18. %       R(:,4) contains the correlation function between (poss prewhitened)
  19. %         u and y (positive lags correponds to an influence from u to y)
  20. %       CL is the 99 % significance level for the impulse response
  21. %       These functions are also plotted by the cra. The plots can be 
  22. %       redisplayed by cra(R);
  23. %
  24.  
  25.  
  26. %    L. Ljung 10-2-90,1-25-92
  27. %    Copyright (c) 1990-92 by the MathWorks, Inc.
  28. %    All Rights Reserved.
  29.  
  30. [Ncap,nz]=size(z);redisp=0;
  31. if nz==4, if z((Ncap+1)/2,1)==0, redisp=1;end,end
  32. if nz>2 & ~redisp,error('This routine is for two data records only. To check correlations between more signals, use cra for two signals at a time.'),end
  33. if redisp, r=z; n=0;end
  34. clf reset
  35. if ~redisp
  36. if nargin<3, n=[];end
  37. if nargin<2, M=[];end
  38. if isempty(n),n=10;end
  39. if isempty(M),M=20;end
  40. if nz==1, error('For a single signal, use covf instead!'),end
  41. if n>0
  42.     a=th2poly(ar(z(:,2),n,'fb0'));
  43.     z(:,1)=filter(a,1,z(:,1));
  44.     z(:,2)=filter(a,1,z(:,2));
  45. end
  46. M=M-1;
  47. Rcap=covf(z,M+1);
  48. r(:,1)=[-M:1:M]';
  49. r(M+1:2*M+1,2:3)=Rcap([1 4],:)';
  50. r(1:M,2:3)=Rcap([1 4],M+1:-1:2)';
  51. scir=Rcap(4,1); sccf=sqrt(Rcap(1,1)*Rcap(4,1));
  52. r(M+1:2*M+1,4)=Rcap(2,:)'/sccf;
  53. r(1:M,4)=Rcap(3,M+1:-1:2)'/sccf;
  54. ir=r(M+1:2*M+1,4)*sccf/scir;
  55. end
  56. subplot(221)
  57. plot(r(:,1),r(:,2)),
  58. if n>0,title('Covf for filtered y'),else title('Covf for y'),end
  59. subplot(222)
  60. plot(r(:,1),r(:,3)),
  61. if n>0,title('Covf for prewhitened u'),else title('Covf for u'),end
  62. subplot(223)
  63. plot(r(:,1),r(:,4))
  64. if n>0,title('Correlation from u to y (prewh)'),else title('Correlation from u to y'),end
  65. if ~redisp,
  66.     sdreu=2.58*sqrt(r(:,3)'*r(:,2))/scir/sqrt(Ncap)*ones(2*M+1,1);
  67.     subplot(224),plot(r(:,1),r(:,4)*sccf/scir)
  68.     hold on,plot(r(:,1),sdreu,'g-.',r(:,1),-sdreu,'g-.'),hold off
  69.     cl=sdreu(1);
  70.     title('Impulse response estimate')
  71. end
  72. set(gcf,'NextPlot','replace');
  73.  
  74.