/branches/R2-1-x/octave-forge/main/signal/cplxreal.m

# · MATLAB · 73 lines · 23 code · 4 blank · 46 comment · 4 complexity · 46977254a8890cad65d361ea4c508004 MD5 · raw file

  1. %% Copyright (C) 2005 Julius O. Smith III
  2. %%
  3. %% This program is free software; you can redistribute it and/or modify
  4. %% it under the terms of the GNU General Public License as published by
  5. %% the Free Software Foundation; either version 2 of the License, or
  6. %% (at your option) any later version.
  7. %%
  8. %% This program is distributed in the hope that it will be useful,
  9. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. %% GNU General Public License for more details.
  12. %%
  13. %% You should have received a copy of the GNU General Public License
  14. %% along with this program; if not, write to the Free Software
  15. %% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. %% -*- texinfo -*-
  17. %% @deftypefn {Function File} {[@var{zc}, @var{zr}] =} cplxreal (@var{z}, @var{thresh})
  18. %% Split the vector z into its complex (@var{zc}) and real (@var{zr}) elements,
  19. %% eliminating one of each complex-conjugate pair.
  20. %%
  21. %% INPUTS:@*
  22. %% @itemize
  23. %% @item
  24. %% @var{z} = row- or column-vector of complex numbers@*
  25. %% @item
  26. %% @var{thresh} = tolerance threshold for numerical comparisons (default = 100*eps)
  27. %% @end itemize
  28. %%
  29. %% RETURNED:@*
  30. %% @itemize
  31. %% @item
  32. %% @var{zc} = elements of @var{z} having positive imaginary parts@*
  33. %% @item
  34. %% @var{zr} = elements of @var{z} having zero imaginary part@*
  35. %% @end itemize
  36. %%
  37. %% Each complex element of @var{z} is assumed to have a complex-conjugate
  38. %% counterpart elsewhere in @var{z} as well. Elements are declared real
  39. %% if their imaginary parts have magnitude less than @var{thresh}.
  40. %%
  41. %% @seealso{cplxpair}
  42. %% @end deftypefn
  43. function [zc,zr] = cplxreal(z,thresh)
  44. if nargin<2, thresh=100*eps; end
  45. % interesting for testing: if nargin<2, thresh=1E-3; end
  46. if isempty(z)
  47. zc=[];
  48. zr=[];
  49. else
  50. zcp = cplxpair(z); % sort complex pairs, real roots at end
  51. nz = length(z);
  52. nzrsec = 0;
  53. i=nz;
  54. while i && abs(imag(zcp(i)))<thresh % determine no. of real values
  55. zcp(i) = real(zcp(i));
  56. nzrsec = nzrsec+1;
  57. i=i-1;
  58. end
  59. nzsect2 = nz-nzrsec;
  60. if mod(nzsect2,2)~=0
  61. error('cplxreal: Odd number of complex values!');
  62. end
  63. nzsec = nzsect2/2;
  64. zc = zcp(2:2:nzsect2);
  65. zr = zcp(nzsect2+1:nz);
  66. end
  67. %!test
  68. %! [zc,zr] = cplxreal(roots([1 0 0 1]));
  69. %! assert({zc,zr},{0.5+i*sin(pi/3),-1},10*eps);