PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/scilab-master-1333395999/modules/signal_processing/macros/pspect.sci

#
Unknown | 126 lines | 116 code | 10 blank | 0 comment | 0 complexity | 11ff6842bb9d0c7a811022e7efb677a3 MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause
  1. // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
  2. // Copyright (C) INRIA - 1988 - C. Bunks
  3. //
  4. // This file must be used under the terms of the CeCILL.
  5. // This source file is licensed as described in the file COPYING, which
  6. // you should have received as part of this distribution. The terms
  7. // are also available at
  8. // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
  9. function [sm,cwp]=pspect(sec_step,sec_leng,wtype,x,y,wpar)
  10. //[sm [,cwp] ]=pspect(sec_step,sec_leng,wtype,x [,y] [,wpar])
  11. //Cross-spectral estimate between x and y if both are given
  12. //and auto-spectral estimate of x otherwise.
  13. //Spectral estimate obtained using the modified periodogram method
  14. // x :
  15. // - if vector, the first signal data
  16. // - amount of input data if scalar. In this case the first signal
  17. // is read by batch by a user defined function xbatch=getx(n,offset).)
  18. // y : optional, if present cross correlation is made else auto correlation
  19. // - if vector, the second signal data .It must have the same number of element than x
  20. // - if scalar, the second signal
  21. // is read by batch by a user defined function ybatch=gety(n,offset).)
  22. // sec_step : offset of each data window.
  23. // if sec_step==sec_leng/2 50% overlap is made
  24. // sec_leng : Number of points of the window
  25. // wtype : window type ('re','tr','hm','hn','kr','ch')
  26. // wpar : optional window parameters
  27. // : for wtype='kr', wpar>0
  28. // : for wtype='ch', 0<wpar(1)<.5, wpar(2)>0
  29. // sm : power spectral estimate in the interval [0,1]
  30. // cwp : unspecified Chebyshev window parameter
  31. //!
  32. [lhs,rhs]=argn(0);
  33. if sec_step>=sec_leng then
  34. error(msprintf(gettext("%s: Incompatible input arguments #%d and #%d: Argument #%d expected to be less than argument #%d.\n"),"pspect",1,2,1,2));
  35. end
  36. if and(wtype<>['re','tr','hm','hn','kr','ch']) then
  37. error(msprintf(gettext("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),..
  38. "pspect",3,"''re'',''tr'',''hm'',''hn'',''kr'',''ch''"));
  39. end
  40. //Analyze calling sequence and construct window
  41. if rhs==4 then,
  42. //pspect(sec_step,sec_leng,wtype,x)
  43. w=window(wtype,sec_leng);
  44. cross=%f; //autocorrelation
  45. elseif rhs==5 then,
  46. //pspect(sec_step,sec_leng,wtype,x,y) or pspect(sec_step,sec_leng,wtype,x,wpar)
  47. if wtype=='kr' then,//pspect(sec_step,sec_leng,'kr',x,wpar)
  48. wpar=y;
  49. w=window(wtype,sec_leng,wpar);cwp=[];
  50. cross=%f; //autocorrelation
  51. elseif wtype=='ch' then,//pspect(sec_step,sec_leng,'ch',x,wpar)
  52. wpar=y;
  53. [w,cwp]=window(wtype,sec_leng,wpar);
  54. cross=%f; //autocorrelation
  55. else,//pspect(sec_step,sec_leng,wtype,x,y)
  56. w=window(wtype,sec_leng);cwp=[];
  57. cross=%t; //cross-correlation
  58. end,
  59. else //pspect(sec_step,sec_leng,wtype,x,y,wpar)
  60. [w,cwp]=window(wtype,sec_leng,wpar);
  61. cross=%t; //cross-correlation
  62. end,
  63. wpower=w*w';//the window energy
  64. //Make x and y row vectors
  65. x=matrix(x,1,-1);
  66. if cross then
  67. y=matrix(y,1,-1);
  68. if size(x,'*')<>size(y,'*') then
  69. error(msprintf(gettext("%s: Arguments #%d and #%d must have the same sizes.\n"),"pspect",4,5));
  70. end
  71. end
  72. //Average periodograms
  73. sm=0*w;
  74. if size(x,'*')==1 then //Batch processing of x and y data
  75. //get number of sections to be used
  76. nsecs=int((x-sec_leng+sec_step)/sec_step);//x contains the number of points of the signals
  77. ovrlp=sec_leng-sec_step;
  78. xd=[0*ones(1,sec_step) getx(ovrlp,1)];
  79. if cross then
  80. yd=[0*ones(1,sec_step) gety(ovrlp,1)];
  81. end
  82. for k=1:nsecs
  83. xd(1:ovrlp)=xd(sec_step+1:sec_leng);
  84. xd(ovrlp+1:sec_leng)=getx(sec_step,sec_leng+(k-1)*sec_step+1);
  85. xw=w.*(xd-(sum(xd)/sec_leng));
  86. fx=fft(xw,-1);
  87. if cross then
  88. yd(1:ovrlp)=yd(sec_step+1:sec_leng);
  89. yd(ovrlp+1:sec_leng)=gety(sec_step,sec_leng+(k-1)*sec_step+1);
  90. yw=w.*(yd-(sum(yd)/sec_leng));
  91. sm=sm+fx.*conj(fft(yw,-1));
  92. else
  93. sm=sm+real(fx.*conj(fx));
  94. end
  95. end
  96. else // Signal data given in x and y if cross-correlation
  97. //get number of sections to be used
  98. nsecs=int((size(x,'*')-sec_leng+sec_step)/sec_step);
  99. ind=(1:sec_leng);
  100. for k=1:nsecs
  101. indd=ind+(k-1)*sec_step*ones(1:sec_leng);
  102. xd=x(indd);
  103. xe=w.*(xd-(sum(xd)/sec_leng));
  104. fx=fft(xe,-1);
  105. if cross then
  106. yd=y(indd);
  107. ye=w.*(yd-(sum(yd)/sec_leng));
  108. sm=sm+fx.*conj(fft(ye,-1));
  109. else
  110. sm=sm+real(fx.*conj(fx));
  111. end
  112. end
  113. end
  114. //Normalization
  115. sm=sm/(nsecs*wpower);
  116. endfunction