
function [] = MRCC(
Rx,
Awgn_en,
Rayleigh_en,
webdemo_en,
svg_en,
filename)


% default parameters 
switch nargin
    case 0
        Rx = 2;
	Awgn_en = 1;
        Rayleigh_en = 1;
        webdemo_en = 0;        
        svg_en = 0;     
       filename = 'MRCC';
   otherwise
end
  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BEGIN, set up graphical output
    
% Create an invisible figure.

if (webdemo_en)
    fig = figure(1); 
	set(fig, 'visible', 'off');
else
    fig = figure(1); 
	set(fig, 'visible', 'on');
end

landscape = '-S930,350';
portrait  = '-S640,480';
output_format = portrait; %portrait;landscape; 

set(0, 'defaultlinelinewidth', 2);

if (webdemo_en)
    set(0, 'defaultaxesfontsize', 6);
    set(0, 'defaultaxesfontname', 'Helvetica');
    set(0, 'defaulttextfontsize', 6);
    set(0, 'defaulttextfontname', 'Helvetica');
    const_lw = 2;
elseif (svg_en)
    set(0, 'defaultaxesfontsize', 6);
    set(0, 'defaultaxesfontname', 'Helvetica');
    set(0, 'defaulttextfontsize', 6);
    set(0, 'defaulttextfontname', 'Helvetica');
    const_lw = 1;
else
    set(0, 'defaultaxesfontsize', 9);
    set(0, 'defaultaxesfontname', 'Helvetica');
    set(0, 'defaulttextfontsize', 8);
    set(0, 'defaulttextfontname', 'Helvetica');
    const_lw = 2;
end

% RGB color definitions, http://www.rapidtables.com/web/color/RGB_Color.htm
gray 	   = [[0,0,0]; [32,32,32]; [64,64,64]; [96,96,96]; [128,128,128]; [160,160,160]; [192,192,192]; [224,224,224]; [255,255,255]]/255;
violett    = [[51,0,25]; [102,0,51]; [153,0,76]; [204,0,102]; [255,0,127]; [255,51,153]; [255,102,178]; [255,153,204]; [255,204,229]]/255;
magenta    = [[51,0,51]; [102,0,102]; [153,0,153]; [204,0,204]; [255,0,255]; [255,51,255]; [255,102,255]; [255,153,255]; [255,204,255]]/255;
lilac      = [[25,0,51]; [51,0,102]; [76,0,153]; [102,0,204]; [127,0,255]; [153,51,255]; [178,102,255]; [204,153,255]; [229,204,255]]/255;
blue       = [[0,0,51]; [0,0,102]; [0,0,153]; [0,0,204]; [0,0,255]; [51,51,255]; [102,102,255]; [153,153,255]; [204,204,255]]/255;
seablue    = [[0,25,51]; [0,51,102]; [0,76,153]; [0,102,204]; [0,128,255]; [51,153,255]; [102,178,255]; [153,204,255]; [204,229,255]]/255;
cyan       = [[0,51,51]; [0,102,102]; [0,153,153]; [0,204,204]; [0,255,255]; [51,255,255]; [102,255,255]; [153,255,255]; [204,255,255]]/255;
seagreen   = [[0,51,25]; [0,102,51]; [0,153,76]; [0,204,102]; [0,255,128]; [51,255,153]; [102,255,178]; [153,255,204]; [204,255,229]]/255;
green      = [[0,51,0]; [0,102,0]; [0,153,0]; [0,204,0]; [0,255,0]; [51,255,51]; [102,255,102]; [153,255,153]; [204,255,204]]/255;
lightgreen = [[25,51,0]; [51,102,0]; [76,153,0]; [102,204,0]; [128,255,0]; [153,255,51]; [178,255,102]; [204,255,153]; [229,255,204]]/255;
yellow     = [[51,51,0]; [102,102,0]; [153,153,0]; [204,204,0]; [255,255,0]; [255,255,51]; [255,255,102]; [255,255,153]; [255,255,204]]/255;
amber      = [[51,25,0]; [102,51,0]; [153,76,0]; [204,102,0]; [255,128,0]; [255,153,51]; [255,178,102]; [255,204,153]; [255,229,204]]/255;
red        = [[51,0,0]; [102,0,0]; [153,0,0]; [204,0,0]; [255,0,0]; [255,51,51]; [255,102,102]; [255,153,153]; [255,204,204]]/255;


% END, set up graphical output
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BEGIN check input parameters for consistency


% END check input parameters for consistency
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clf
% Constants:
SNR_dB = [-10:2:30];

nBits = 10^5;		%Number of Bits

Source =round(rand(1,nBits));   % Source of Information    
Mod_Signal = 1- 2*Source;       % BPSK Modulation 1 -> -1, 0 -> 1 ;

no_Errors = zeros(1,length(SNR_dB)); 
nErrors = zeros(1,length(SNR_dB)); 


for i=1:length(SNR_dB) 

      sigma = 1/sqrt(2*10^(SNR_dB(i)/10));
      awg_Noise = sigma*[randn(Rx,nBits) + sqrt(-1)*randn(Rx,nBits)]; 
      Source_replica = repmat(Mod_Signal,Rx,1);

   if(Awgn_en) 

    Rx_Signal = Source_replica + awg_Noise ;   % Signal Recieved at Recievers 
            
    eq_Rx_Signal= sum(Rx_Signal,1);            % Equalization of Rx. signal
    
    noFading_Rx = (eq_Rx_Signal)/ Rx; 
    
    Rx_data_gaussian = real(noFading_Rx ) < 0;      % Detection of the Rx. Signal
     
    nErrors(i) = sum(Source ~= Rx_data_gaussian);   % Counting the number of errors

   endif

    if (Rayleigh_en)

    	Channel_Coeff = 1/sqrt(2)*[randn(Rx,nBits)+ sqrt(-1)*randn(Rx,nBits)];    % Rayleigh fading channel coefficients with zero mean and sigma^2 variance
    
    	Rx_Signal = Channel_Coeff.*Source_replica + awg_Noise ;  		  % Signal Recieved at Recievers
    
    	channel_weights = conj(Channel_Coeff);                                    % Optimal channel weights
    
    	Rx_Signal_Detected =sum(channel_weights.*Rx_Signal,1);                    % Rx. Signal Multiplied with the optimal weights of different channels    
    
    	eq_MRC_Signal = Rx_Signal_Detected./sum(Channel_Coeff.*channel_weights,1);  	% Equalization of Rx. signal    
    
    	Rx_data = real(eq_MRC_Signal) < 0; 						% Detection of the Rx. Signal     
 
    	no_Errors(i) = sum(Source~=Rx_data);             				% Counting Number of total Errors.
    endif

endfor

SNR_Lin = 10.^(SNR_dB/10);

noFading_BER = nErrors/nBits;
Fading_BER = no_Errors/nBits;

str = ['N_R = ', num2str(Rx)'];

if(Awgn_en)
noFading_Theory= 0.5*erfc(sqrt(SNR_Lin));
light_red = [0.9 0.5 0.4];
semilogy(SNR_dB,noFading_Theory,'--',"LineWidth",const_lw, "color",light_red);
hold on
noFading_Theory= 0.5*erfc(sqrt(SNR_Lin*Rx));              % Theoratical BER for no Fading channel
semilogy(SNR_dB,noFading_Theory,'--',"LineWidth",const_lw, "color","r");
semilogy(SNR_dB,noFading_BER,'v-',"LineWidth",const_lw, "color","b");
legend('AWGN: Theoretical(N_R=1)', 'Theoretical', 'Simulated');
str = ['Maximum Ratio Combining, ', 'N_R = ' num2str(Rx)];
title(str);
endif

if(Rayleigh_en)

minus_lambda = 1/2* (1-(sqrt(SNR_Lin./(1+SNR_Lin))));
plus_lambda  = 1/2* (1+(sqrt(SNR_Lin./(1+SNR_Lin))));
gray = [0.5 0.5 0.5];
semilogy(SNR_dB,minus_lambda,'m--',"LineWidth",const_lw, "color",gray);
hold on

prob= 0;
for l = 0:Rx-1
    prob = prob +(((factorial(Rx-1+l)) / (factorial(Rx-1)*factorial(l)))* (plus_lambda).^l);
end
Fading_Theory= ((minus_lambda).^Rx.* prob);
semilogy(SNR_dB,Fading_Theory,'m--',"LineWidth",const_lw, "color","k");
hold on
semilogy(SNR_dB,Fading_BER,'bp-',"LineWidth",const_lw, "color","m");
legend('Rayleigh Fading: Theoretical (N_R=1)', 'Theoretical', 'Simulated');
str = ['Maximum Ratio Combining, ', 'N_R = ' num2str(Rx)];
title(str);
endif


if (Awgn_en) && (Rayleigh_en)

noFading_Theory= 0.5*erfc(sqrt(SNR_Lin));
light_red = [0.9 0.5 0.4];
semilogy(SNR_dB,noFading_Theory,'--',"LineWidth",const_lw, "color",light_red);
hold on

noFading_Theory= 0.5*erfc(sqrt(SNR_Lin.* Rx));    % Theoratical BER for no Fading channel
           		
semilogy(SNR_dB,noFading_Theory,'--',"LineWidth",const_lw, "color","r");
hold on
semilogy(SNR_dB,noFading_BER,'v-',"LineWidth",const_lw, "color","b");


minus_lambda = 1/2* (1-(sqrt(SNR_Lin./(1+SNR_Lin))));
plus_lambda  = 1/2* (1+(sqrt(SNR_Lin./(1+SNR_Lin))));
gray = [0.5 0.5 0.5]
semilogy(SNR_dB,minus_lambda,'m--',"LineWidth",const_lw, "color",gray);
hold on

prob= 0;
for l = 0:Rx-1
    prob = prob +(((factorial(Rx-1+l)) / (factorial(Rx-1)*factorial(l)))* (plus_lambda).^l);
end
Fading_Theory= ((minus_lambda).^Rx.* prob);

semilogy(SNR_dB,Fading_Theory,'m--',"LineWidth",const_lw, "color","k");

semilogy(SNR_dB,Fading_BER,'bp-',"LineWidth",const_lw, "color","m");

legend('AWGN: Theoretical (N_R=1)', 'Theoretical', 'Simulated', 'Rayleigh Fading: Theoretical (N_R=1)', 'Theoretical', 'Simulated');
str = ['Maximum Ratio Combining, ', 'N_R = ' num2str(Rx)];
title(str);
endif

axis([-10 30 10^-5 0.5])
grid on

xlabel('Es/N0 [dB]');
ylabel('Bit Error Rate (BER)');
output_format = portrait;
    
  
% Create an image file. This image is displayed on the webpage.

if (webdemo_en)
    print(filename, '-dpng',output_format);
endif
if (svg_en)
    print(['fig_',filename], '-dsvg',output_format);
endif

endfunction

% END, main program (control)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%






%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BEGIN, function definitions



% END, function definitions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
