
function [] = SC(
Rx,
%nBits,
webdemo_en,
svg_en,
filename)


% default parameters 
switch nargin
    case 0
        Rx = 2;	
        webdemo_en = 0;        
        svg_en = 0;     
       filename = 'SC';
   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;
Source =round(rand(1,nBits));    % Source of Information 
Mod_Signal = 1- 2*Source;       % BPSK Modulation 1 -> -1, 0 -> 1 ;

nErrors = zeros(1,length(SNR_dB));  
 
   
for i=1:length(SNR_dB) 

    sigma = 1/sqrt(2*10^(SNR_dB(i)/10));
    Channel_Coeff = 1/sqrt(2)*[randn(Rx,nBits)+ sqrt(-1)*randn(Rx,nBits)];    % Rayleigh fading channel coefficients with zero mean and sigma^2 variance

    awg_Noise = sigma*[randn(Rx,nBits) + sqrt(-1)*randn(Rx,nBits)];    
    
    Source_replica = repmat(Mod_Signal,Rx,1);

    Rx_Signal = Channel_Coeff.*Source_replica + awg_Noise;

    Channel_weights = conj(Channel_Coeff);   			      % Optimal channel weights
    

		Channel_Power = Channel_Coeff.*Channel_weights;   % Channel Power on each Receive Antenna     

                [maxPower_Sig] = max(Channel_Power,[],1);

         	maxPower_replica = kron(ones(Rx,1),maxPower_Sig);

         	isequal = eq(Channel_Power,maxPower_replica);     

         	Rx_Sel = Rx_Signal(isequal);  			% Selecting received signal on the basis of max. power channel  
       
         	Channel_Sel = Channel_Coeff(isequal);           % Selecting channel on the basis of max. power channel  
          
       		eq_Rx_Data = Rx_Sel./Channel_Sel;		% Equalization 

       		reshape_eq_data = reshape(eq_Rx_Data,1,nBits);  
       
       		Rx_data_sc = real(reshape_eq_data ) < 0;  
    	
                no_Errors_sc(i) = sum(Source ~= Rx_data_sc);        % Calculating the number of errors  

   
	                                      
    	    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_mrc = real(eq_MRC_Signal) < 0; 						% Detection of the Rx. Signal     
 
    	    no_Errors_mrc(i) = sum(Source~=Rx_data_mrc);       
endfor


BER = no_Errors_sc/nBits;
BER_mrc = no_Errors_mrc/nBits;
SNR_Lin = 10.^(SNR_dB/10);

Fading_Theory = 1/2* (1-(sqrt(SNR_Lin./(1+SNR_Lin))));   		% Theoratical BER 

semilogy(SNR_dB,Fading_Theory,'--',"LineWidth",const_lw, "color","k");
hold on
semilogy(SNR_dB,BER,'v-',"LineWidth",const_lw, "color","r",'markersize', 5);
semilogy(SNR_dB,BER_mrc,'d-',"LineWidth",const_lw, "color","b",'markersize', 5);


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

grid on



legend('Rayleigh fading: Theoretical (N_R = 1) ', ' Simulated (SC)', ' Simulated (MRC)');

xlabel('Es/N0 [dB]');
ylabel('Bit Error Rate (BER)');
str = ['Selection Combining, ', 'N_R = ' num2str(Rx)];
title(str);
output_format = portrait;
    
%str = ['L_A(x_1) = ' num2str(La1) ', P_{apriori}[x_1=-1] = ' num2str(1-Pa1), '  L_A(x_2) = ' num2str(La2), P_{apriori}[x_2=-1] = ' num2str(1-Pa2)];
%title(str);
% 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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
