%***********************************************************
%        
%        This mfunction generates faded envelope and phase 
%        corresponding to Rayleigh or Rician fading
%        AUTHOR: Balu Santhanam
%        DATE  : 09/10/00 
%        FUNCTION SYNOPSIS: 
%        [env,phi] = fade(L,para,type)
% 
%        Parameter Description: 
%        L          :   number of samples needed  
%        para       :   parameter vector containing 
%                       [m1,m2,var] 
%        type       :   string containing the type 
%                       of fading with the options
%        'Rayleigh' : Rayleigh fading
%        'Rice'     : Rician fading
%**********************************************************
function [env,phi] = fade(L,para,type)
%  Error check
if strcmp(type,'Rayleigh') == 1 
   if para(1) ~= 0 & para(2) ~= 0
      error('Fade type and means do not match')
   end
   mu = zeros(1,2);
elseif para(3) <= 0
   error('Positive variance needed')
elseif size(para) ~= 3
   error('Insufficient input parameters')
elseif strcmp(type,'Rayleigh') == 0 & strcmp(type,'Rice') == 0
   error('Invalid Option Type')
end
%  Generate bivariate Gaussian uncorrelated 
%  random variables
mu = [para(1) ; para(2)]; 
C = para(3)*eye(2,2);
r = mvnrnd(mu,C,L);
%  Convert to polar coordinates and compute 
%  magnitude and phase 
z = r(:,1) + j*r(:,2);
env = abs(z); phi = angle(z);
%**********************************************************



