gwnoise.m
Upload User: loeagle
Upload Date: 2013-03-02
Package Size: 1236k
Code Size: 2k
Development Platform:

Matlab

  1. function [m, d, s] = gwnoise(m, d, s)
  2. %GWNOISE generate valid mean value, standard deviation and seeds for GWNOISE block.
  3. % [M, D, S] = GWNOISE(M, D, S) checks input mean M, standard deviation D, and
  4. % seed S. When they are not valid, converts them to be valid for GWNOISE
  5. % block, or gives out an error message.
  6. %
  7. %  Copyright 1996-2001 The MathWorks, Inc.
  8. %       $Revision: 1.17 $
  9. if nargin ~= 3
  10. error('Input variable must be 3 for GWNOISE.');
  11. end;
  12. m = m(:)';
  13. s = s(:)';
  14. l_m = length(m);
  15. l_s = length(s);
  16. [n_d, m_d] = size(d);
  17. if n_d ~= m_d
  18. if min(n_d, m_d) == 1
  19. if any(d < 0)
  20. error('Standard deviation cannot be negative number.');
  21. end
  22. d = diag(d);
  23. else
  24. error('Standard deviation for Gaussian noise block is not valid.');
  25. end;
  26. else
  27. [n_d, m_d] = chol(d);
  28. if m_d ~= 0
  29. error('Standard deviation for Gaussian noise must be positive definite.');
  30. end;
  31. end;
  32. l_d = length(d);
  33. if ((l_m < l_d) & (l_m ~= 1)) | ((l_m > l_d) & (l_d ~= 1))
  34. error('The dimensions of the mean and standard deviation for Gaussian noise block are not compatible.');
  35. end;
  36. max_l = max(l_m, l_d);
  37. if l_s < max_l
  38.     new_seed = 1;
  39.     while l_s < max_l
  40.         seed_pos = find(s == new_seed);
  41.         if (length(seed_pos) == 0)
  42.             s = [s, new_seed];
  43.             l_s = length(s);
  44.         end
  45.         new_seed = new_seed + 1;
  46.     end    
  47. elseif l_s > max_l
  48.     if max_l ~= 1
  49.         s = s(1 : max_l);
  50.     else
  51.         d = eye(l_s) * d;
  52.     end;
  53. end;
  54. [d, tmp] = chol(d);
  55. if tmp > 0
  56.     error('Covariance matrix in AWGN noise generator is not a positively defined matrix.');
  57. end;
  58. % end of gwnoise