%% load_wifi_params file for load parameters of simulation
global wifi_params

% definovat randstreams seed!

wifi_params.general.standard = wifi_standard; 


%% PHY layer - timing-related constants
switch wifi_standard
    case '802dot11g'
        wifi_params.mapping.bandwidth = 20e6;
        wifi_params.mapping.n_fft = 64;
        wifi_params.mapping.n_data = 48;
        wifi_params.mapping.n_pilot = 4;
        wifi_params.mapping.ir_highest_subc = 26;
        wifi_params.mapping.i_data = [1:5, 7:19, 21:26, 28:33, 35:47, 49:53];
        wifi_params.mapping.i_DC = 27;
        wifi_params.mapping.i_pilots = [6 20 34 48];
        wifi_params.mapping.ir_phase_rotated_subc = [];
        wifi_params.mapping.phase_rotation_factor = 1; % 0°
    case '802dot11n'
        switch CH_BANDWIDTH
            case 'HT_CBW20'
                wifi_params.mapping.bandwidth = 20e6;
                wifi_params.mapping.n_fft = 64;
                wifi_params.mapping.n_data = 52;
                wifi_params.mapping.n_pilot = 4;
                wifi_params.mapping.ir_highest_subc = 28;
                wifi_params.mapping.i_data = [1:7, 9:21, 23:28, 30:35, 37:49, 51:57];
                wifi_params.mapping.i_DC = 29;
                wifi_params.mapping.i_pilots = [8 22 36 50];
                wifi_params.mapping.ir_phase_rotated_subc = [];
                wifi_params.mapping.phase_rotation_factor = 1; % 0°
            case 'NON_HT_CBW20'
                wifi_params.mapping.bandwidth = 20e6;
                wifi_params.mapping.n_fft = 64;
                wifi_params.mapping.n_data = 48;
                wifi_params.mapping.n_pilot = 4;
                wifi_params.mapping.ir_highest_subc = 26;
                wifi_params.mapping.i_data = [1:5, 7:19, 21:26, 28:33, 35:47, 49:53];
                wifi_params.mapping.i_DC = 27;
                wifi_params.mapping.i_pilots = [6 20 34 48];
                wifi_params.mapping.ir_phase_rotated_subc = [];
                wifi_params.mapping.phase_rotation_factor = 1; % 0°
            case 'HT_CBW40'
                wifi_params.mapping.bandwidth = 40e6;
                wifi_params.mapping.n_fft = 128;
                wifi_params.mapping.n_data = 108;
                wifi_params.mapping.n_pilot = 6;
                wifi_params.mapping.ir_highest_subc = 58;
                wifi_params.mapping.i_data = [1:5, 7:33, 35:47, 49:57, 61:69, 71:83, 85:111, 113:117];
                wifi_params.mapping.i_DC = 58:60; % zero DC carrier including two isolating zero carriers
                wifi_params.mapping.i_pilots = [6 34 48 70 84 112];
                wifi_params.mapping.ir_phase_rotated_subc = 0:wifi_params.mapping.ir_highest_subc; % >= 0
                wifi_params.mapping.phase_rotation_factor = 1j; % 90°
            case 'NON_HT_CBW40' % MCS 32 and non-HT duplicate
                warning('NON_HT_CBW40 is not implemented correctly');
                wifi_params.mapping.bandwidth = 40e6;
                wifi_params.mapping.n_fft = 128;
                wifi_params.mapping.n_data = 48;
                wifi_params.mapping.n_pilot = 6;
                wifi_params.mapping.ir_highest_subc = 58;
                wifi_params.mapping.i_data = [1:5, 7:33, 35:47, 49:57, 61:69, 71:83, 85:111, 113:117];
                wifi_params.mapping.i_DC = 58:60; % zero DC carrier including two isolating zero carriers
                wifi_params.mapping.i_pilots = [6 34 48 70 84 112];
                wifi_params.mapping.ir_phase_rotated_subc = [];
                wifi_params.mapping.phase_rotation_factor = 1; % 0°
                error('NON_HT_CBW40 mode is not implemented correctly');
            otherwise
                error('Wrong Wi-Fi timing-related constants');
        end
    case '802dot11ac'
        switch CH_BANDWIDTH
            case 'CBW20'
                wifi_params.mapping.bandwidth = 20e6;
                wifi_params.mapping.n_fft = 64;
                wifi_params.mapping.n_data = 52;
                wifi_params.mapping.n_pilot = 4;
                wifi_params.mapping.ir_highest_subc = 28;
                wifi_params.mapping.i_data = [1:7, 9:21, 23:28, 30:35, 37:49, 51:57];
                wifi_params.mapping.i_DC = 29;
                wifi_params.mapping.i_pilots = [8 22 36 50];
                wifi_params.mapping.ir_phase_rotated_subc = [];
                wifi_params.mapping.phase_rotation_factor = 1; % 0°
                wifi_params.mapping.n_segment = 1;
            case 'CBW40'
                wifi_params.mapping.bandwidth = 40e6;
                wifi_params.mapping.n_fft = 128;
                wifi_params.mapping.n_data = 108;
                wifi_params.mapping.n_pilot = 6;
                wifi_params.mapping.ir_highest_subc = 58;
                wifi_params.mapping.i_data = [1:5, 7:33, 35:47, 49:57, 61:69, 71:83, 85:111, 113:117];
                wifi_params.mapping.i_DC = 58:60;
                wifi_params.mapping.i_pilots = [6 34 48 70 84 112];
                wifi_params.mapping.ir_phase_rotated_subc = 0:wifi_params.mapping.ir_highest_subc;
                wifi_params.mapping.phase_rotation_factor = 1j; % 90°
                wifi_params.mapping.n_segment = 1;
            case 'CBW80'
                error([CH_BANDWIDTH,' mode is not implemented correctly']);
                wifi_params.mapping.bandwidth = 80e6;
                wifi_params.mapping.n_fft = 256;
                wifi_params.mapping.n_data = 234;
                wifi_params.mapping.n_pilot = 8;
                wifi_params.mapping.ir_highest_subc = 122;
                wifi_params.mapping.i_data = [1:7, 9:21, 23:28, 30:35, 37:49, 51:57];
                wifi_params.mapping.i_DC = 29;
                wifi_params.mapping.i_pilots = [8 22 36 50];
                wifi_params.mapping.ir_phase_rotated_subc = -64:wifi_params.mapping.ir_highest_subc;
                wifi_params.mapping.phase_rotation_factor = -1; % 0°
                wifi_params.mapping.n_segment = 1;
            case 'CBW80+80'
                error([CH_BANDWIDTH,' mode is not implemented correctly']);
                wifi_params.mapping.bandwidth = 80e6;
                wifi_params.mapping.n_fft = 256;
                wifi_params.mapping.n_data = 234;
                wifi_params.mapping.n_pilot = 8;
                wifi_params.mapping.ir_highest_subc = 122;
                wifi_params.mapping.i_data = [1:7, 9:21, 23:28, 30:35, 37:49, 51:57];
                wifi_params.mapping.i_DC = 29;
                wifi_params.mapping.i_pilots = [8 22 36 50];
                wifi_params.mapping.ir_phase_rotated_subc = -64:wifi_params.mapping.ir_highest_subc;
                wifi_params.mapping.phase_rotation_factor = -1; % 0°
                wifi_params.mapping.n_segment = 2;
            case 'CBW160'
                error([CH_BANDWIDTH,' mode is not implemented correctly']);
                wifi_params.mapping.bandwidth = 160e6;
                wifi_params.mapping.n_fft = 512;
                wifi_params.mapping.n_data = 468;
                wifi_params.mapping.n_pilot = 16;
                wifi_params.mapping.ir_highest_subc = 250;
                wifi_params.mapping.i_data = [1:7, 9:21, 23:28, 30:35, 37:49, 51:57];
                wifi_params.mapping.i_DC = 29;
                wifi_params.mapping.i_pilots = [8 22 36 50];
                wifi_params.mapping.ir_phase_rotated_subc = [-192:-1, 64:wifi_params.mapping.ir_highest_subc];
                wifi_params.mapping.phase_rotation_factor = -1; % 0°
                wifi_params.mapping.n_segment = 1;
            otherwise
                error('Wrong Wi-Fi timing-related constants');
        end
        
    case '802dot11af'
        
    case '802dot11ah'
        
    otherwise
        error('Unknown type of IEEE 802.11')
end
% parameters dependent
wifi_params.mapping.n_tot = wifi_params.mapping.n_data+wifi_params.mapping.n_pilot;
wifi_params.mapping.df = wifi_params.mapping.bandwidth/wifi_params.mapping.n_fft;
wifi_params.mapping.bw_real = wifi_params.mapping.n_tot*wifi_params.mapping.df;
wifi_params.mapping.ir_all = -wifi_params.mapping.ir_highest_subc:wifi_params.mapping.ir_highest_subc;


% ramce jsou hezky popsany napr. zde:
% http://www.cnrood.com/public/docs/WiFi_Physical_Layer_and_Transm_Meas.pdf

wifi_params.general.LENGTH = LENGTH;

wifi_params.mapping.service_length = 16;

% Bude predefinovano parametrem LENGTH
%% MCS
mcs_definition;


%% MODEM common params
% BPSK
wifi_params.modulation(1).M = 1; % BPSK
wifi_params.modulation(1).k = 2.^wifi_params.modulation(1).M; % number of constellation points
wifi_params.modulation(1).hMod = comm.PSKModulator(...
    'ModulationOrder',wifi_params.modulation(1).k,...
    'PhaseOffset',pi,...
    'BitInput',true);
wifi_params.modulation(1).hDemod = comm.PSKDemodulator(...
    'ModulationOrder',wifi_params.modulation(1).k,...
    'PhaseOffset',pi,...
    'BitOutput',true,...
    'DecisionMethod','Hard decision');
wifi_params.modulation(1).hDemod_LLR = comm.PSKDemodulator(...
    'ModulationOrder',wifi_params.modulation(1).k,...
    'PhaseOffset',pi,...
    'BitOutput',true,...
    'DecisionMethod','Log-likelihood ratio');
wifi_params.modulation(1).hMod_factor = 1;
wifi_params.modulation(1).hScalQuant = dsp.ScalarQuantizerEncoder('Partitioning','Unbounded');
wifi_params.modulation(1).bound_pts_wo_noiseVar = (-1.5:0.5:1.5)*wifi_params.modulation(1).hMod_factor;
% QPSK
wifi_params.modulation(2).M = 2; % QPSK
wifi_params.modulation(2).k = 2.^wifi_params.modulation(2).M; % number of constellation points
wifi_params.modulation(2).hMod = comm.PSKModulator(...
    'ModulationOrder',wifi_params.modulation(2).k,...
    'PhaseOffset',pi/4,...
    'BitInput',true,...
    'SymbolMapping','Custom',...
    'CustomSymbolMapping',[3 1 0 2]);
wifi_params.modulation(2).hDemod = comm.PSKDemodulator(...
    'ModulationOrder',wifi_params.modulation(2).k,...
    'PhaseOffset',pi/4,...
    'BitOutput',true,...
    'SymbolMapping','Custom',...
    'CustomSymbolMapping',[3 1 0 2],...
    'DecisionMethod','Hard decision');
wifi_params.modulation(2).hDemod_LLR = comm.PSKDemodulator(...
    'ModulationOrder',wifi_params.modulation(2).k,...
    'PhaseOffset',pi/4,...
    'BitOutput',true,...
    'SymbolMapping','Custom',...
    'CustomSymbolMapping',[3 1 0 2],...
    'DecisionMethod','Log-likelihood ratio'); %'Log-likelihood ratio');
wifi_params.modulation(2).hMod_factor = 1;
wifi_params.modulation(2).hScalQuant = dsp.ScalarQuantizerEncoder('Partitioning','Unbounded');
wifi_params.modulation(2).bound_pts_wo_noiseVar = (-1.5:0.5:1.5)*wifi_params.modulation(2).hMod_factor;
% 16QAM
wifi_params.modulation(4).M = 4; % 16QAM
wifi_params.modulation(4).k = 2.^wifi_params.modulation(4).M; % number of constellation points
wifi_params.modulation(4).hMod = comm.RectangularQAMModulator(...
    'ModulationOrder',wifi_params.modulation(4).k,...
    'BitInput',true,...
    'SymbolMapping','Custom',...
    'CustomSymbolMapping',...
    [2 3 1 0 6 7 5 4 ...
    14 15 13 12 10 11 9 8]);
wifi_params.modulation(4).hDemod = comm.RectangularQAMDemodulator(...
    'ModulationOrder',wifi_params.modulation(4).k,...
    'BitOutput',true,...
    'SymbolMapping','Custom',...
    'CustomSymbolMapping',...
    [2 3 1 0 6 7 5 4 ...
    14 15 13 12 10 11 9 8],...
    'DecisionMethod','Hard decision');
wifi_params.modulation(4).hDemod_LLR = comm.RectangularQAMDemodulator(...
    'ModulationOrder',wifi_params.modulation(4).k,...
    'BitOutput',true,...
    'SymbolMapping','Custom',...
    'CustomSymbolMapping',...
    [2 3 1 0 6 7 5 4 ...
    14 15 13 12 10 11 9 8],...
    'DecisionMethod','Log-likelihood ratio');
wifi_params.modulation(4).hMod_factor = 1/sqrt(10);
wifi_params.modulation(4).hScalQuant = dsp.ScalarQuantizerEncoder('Partitioning','Unbounded');
wifi_params.modulation(4).bound_pts_wo_noiseVar = (-3:1:3)*wifi_params.modulation(4).hMod_factor;
% 64QAM
wifi_params.modulation(6).M = 6; % 64QAM
wifi_params.modulation(6).k = 2.^wifi_params.modulation(6).M; % number of constellation points
wifi_params.modulation(6).hMod = comm.RectangularQAMModulator(...
    'ModulationOrder',wifi_params.modulation(6).k,...
    'BitInput',true,...
    'SymbolMapping','Custom',...
    'CustomSymbolMapping',...
    [4 5 7 6 2 3 1 0 ...
    12 13 15 14 10 11 9 8 ...
    28 29 31 30 26 27 25 24 ...
    20 21 23 22 18 19 17 16 ...
    52 53 55 54 50 51 49 48 ...
    60 61 63 62 58 59 57 56 ...
    44 45 47 46 42 43 41 40 ...
    36 37 39 38 34 35 33 32]);
wifi_params.modulation(6).hDemod = comm.RectangularQAMDemodulator(...
    'ModulationOrder',wifi_params.modulation(6).k,...
    'BitOutput',true,...
    'SymbolMapping','Custom',...
    'CustomSymbolMapping',...
    [4 5 7 6 2 3 1 0 ...
    12 13 15 14 10 11 9 8 ...
    28 29 31 30 26 27 25 24 ...
    20 21 23 22 18 19 17 16 ...
    52 53 55 54 50 51 49 48 ...
    60 61 63 62 58 59 57 56 ...
    44 45 47 46 42 43 41 40 ...
    36 37 39 38 34 35 33 32],...
    'DecisionMethod','Hard decision');
wifi_params.modulation(6).hDemod_LLR = comm.RectangularQAMDemodulator(...
    'ModulationOrder',wifi_params.modulation(6).k,...
    'BitOutput',true,...
    'SymbolMapping','Custom',...
    'CustomSymbolMapping',...
    [4 5 7 6 2 3 1 0 ...
    12 13 15 14 10 11 9 8 ...
    28 29 31 30 26 27 25 24 ...
    20 21 23 22 18 19 17 16 ...
    52 53 55 54 50 51 49 48 ...
    60 61 63 62 58 59 57 56 ...
    44 45 47 46 42 43 41 40 ...
    36 37 39 38 34 35 33 32],...
    'DecisionMethod','Log-likelihood ratio');
wifi_params.modulation(6).hMod_factor = 1/sqrt(42);
wifi_params.modulation(6).hScalQuant = dsp.ScalarQuantizerEncoder('Partitioning','Unbounded');
wifi_params.modulation(6).bound_pts_wo_noiseVar = (-7:1:7)*wifi_params.modulation(6).hMod_factor;
% 256QAM -- for 802dot11ac only -- not defined correctly yet
wifi_params.modulation(8).M = 8; % 
wifi_params.modulation(8).k = 2.^wifi_params.modulation(8).M; % number of constellation points
wifi_params.modulation(8).hMod = comm.RectangularQAMModulator(...
    'ModulationOrder',wifi_params.modulation(8).k,...
    'BitInput',true);
%             'SymbolMapping','Custom',...
%             'CustomSymbolMapping',...
%             [4 5 7 6 2 3 1 0 ...
%             12 13 15 14 10 11 9 8 ...
%             28 29 31 30 26 27 25 24 ...
%             20 21 23 22 18 19 17 16 ...
%             52 53 55 54 50 51 49 48 ...
%             60 61 63 62 58 59 57 56 ...
%             44 45 47 46 42 43 41 40 ...
%             36 37 39 38 34 35 33 32]);
wifi_params.modulation(8).hDemod = comm.RectangularQAMDemodulator(...
    'ModulationOrder',wifi_params.modulation(8).k,...
    'BitOutput',true,...
    'DecisionMethod','Hard decision');
wifi_params.modulation(8).hDemod_LLR = comm.RectangularQAMDemodulator(...
    'ModulationOrder',wifi_params.modulation(8).k,...
    'BitOutput',true,...
    'DecisionMethod','Log-likelihood ratio');
%             'SymbolMapping','Custom',...
%             'CustomSymbolMapping',...
%             [4 5 7 6 2 3 1 0 ...
%             12 13 15 14 10 11 9 8 ...
%             28 29 31 30 26 27 25 24 ...
%             20 21 23 22 18 19 17 16 ...
%             52 53 55 54 50 51 49 48 ...
%             60 61 63 62 58 59 57 56 ...
%             44 45 47 46 42 43 41 40 ...
%             36 37 39 38 34 35 33 32],...
wifi_params.modulation(8).hMod_factor = 1/sqrt(170);
wifi_params.modulation(8).hScalQuant = dsp.ScalarQuantizerEncoder('Partitioning','Unbounded');
wifi_params.modulation(8).bound_pts_wo_noiseVar = (-15:1:15)*wifi_params.modulation(8).hMod_factor;
%% Channel coding parameters
m_stbc = 1; % no STBC is used
wifi_params.coding = [];
switch coding_type
    case 'BCC'
        wifi_params.coding = BCC_params(wifi_params, m_stbc, i_mcs);
    case 'LDPC'
        wifi_params.coding = LDPC_params(wifi_params, m_stbc, i_mcs);
end
wifi_params.coding.decision_type = decision_type;
wifi_params.mapping.n_data_symbols_per_frame = wifi_params.coding.N_sym; % N_SYM
%% Scrambling parameters
wifi_params.scrambling.scr_seed = randi([0 1],1,7);
%% Interleaving parameters
wifi_params.interleaving = Interleaving_params(coding_type,wifi_params,i_mcs);

%% Mapping
% it will be necessary in case of change pilot subcarriers position
% logical map of data
wifi_params.mapping.map_data = false(wifi_params.mapping.n_tot+numel(wifi_params.mapping.i_DC),1);
wifi_params.mapping.map_data(wifi_params.mapping.i_data) = true;
wifi_params.mapping.map_data = repmat(wifi_params.mapping.map_data,1,wifi_params.mapping.n_data_symbols_per_frame);
% logical map of pilots
wifi_params.mapping.map_pilots = false(wifi_params.mapping.n_tot+numel(wifi_params.mapping.i_DC),1);
wifi_params.mapping.map_pilots(wifi_params.mapping.i_pilots) = true;
wifi_params.mapping.map_pilots = repmat(wifi_params.mapping.map_pilots,1,wifi_params.mapping.n_data_symbols_per_frame);
% logical map of DC
wifi_params.mapping.map_DC = false(wifi_params.mapping.n_tot+numel(wifi_params.mapping.i_DC),1);
wifi_params.mapping.map_DC(wifi_params.mapping.i_DC) = true;
wifi_params.mapping.map_DC = repmat(wifi_params.mapping.map_DC,1,wifi_params.mapping.n_data_symbols_per_frame);

% logical phase shift mapping
wifi_params.mapping.map_phase_rotation = false(size(wifi_params.mapping.map_data));
wifi_params.mapping.map_phase_rotation(wifi_params.mapping.ir_phase_rotated_subc+wifi_params.mapping.ir_highest_subc+1,:) = true;

wifi_params.Fs = wifi_params.mapping.n_fft*wifi_params.mapping.df;

%% MAPPING TEST
if showMapping
    % data:
    mDATA = double(wifi_params.mapping.map_data(:,1));
    % pilots:
    mPILOTS = double(wifi_params.mapping.map_pilots(:,1));
    % DC subc:
    mDC = double(wifi_params.mapping.map_DC(:,1));
    % phase rotation:
    mPR = double(wifi_params.mapping.map_phase_rotation(:,1));
    
    ir_all = wifi_params.mapping.ir_all.';
    
    table(ir_all,mDATA,mPILOTS,mDC,mPR)
    
    figure; image(0:2,ir_all,[mDATA,mPILOTS,mDC,mPR]*100)
    xlabel(['DATA x PILOTS x DC x PHASE ROTATION *(',num2str(wifi_params.mapping.phase_rotation_factor),')']), ylabel('subcarrier index')
    
end
% % % % % % % % wifi_params.coexistence.fc = fc; % neni zatim potreba, bude
% delano pouze jako IF pasmo, pouze vykus z pasma 2400 -- 2483,5 MHz ==> 0 -- 83,5 MHz -> uspora casu, mozna filtrace apod.

switch GuardInterval
    case 'short'
        wifi_params.cprefix.t_cp = 400e-9;
        if strcmp(CH_BANDWIDTH,'NON_HT_CBW20')||strcmp(CH_BANDWIDTH,'NON_HT_CBW40')
            warning('Short cyclic prefix is not usable in Non-HT mode (see load_wifi_params)');
            wifi_params.cprefix.t_cp = 800e-9;
            warning('Cyclic prefix length was set to extended (800ns)');
        end
    case 'normal'
        wifi_params.cprefix.t_cp = 800e-9;
    case 'doubled'
        wifi_params.cprefix.t_cp = 1600e-9;
    otherwise
        error('Wrong cyclic prefix');
end
wifi_params.cprefix.n_cp = (wifi_params.cprefix.t_cp*wifi_params.Fs);
wifi_params.cprefix.indices = wifi_params.mapping.n_fft-wifi_params.cprefix.n_cp+1:wifi_params.mapping.n_fft;

%% Framing

% disp(mat2str(wifi_params.cprefix.indices))
%% Colission detections:
if strcmp(CH_BANDWIDTH,'NON_HT_CBW20') && (wifi_params.cprefix.t_cp == 400e-9)
    warning('Short cyclic prefix is not usable in Mixed (Non-HT) mode (see load_wifi_params)');
    warning('Cyclic prefix length was set to extended (800ns)');
    
    wifi_params.cprefix.t_cp = 800e-9;
    wifi_params.cprefix.n_cp = (wifi_params.cprefix.t_cp*wifi_params.Fs);
end


%%
%wifi_params.coexistence.T_common = 1e-3;
% wifi_params.coexistence.N_wifi_all_symbols = wifi_params.Fs*wifi_params.coexistence.T_common;
% wifi_params.coexistence.N_samples_per_symbol = wifi_params.mapping.n_fft+wifi_params.cprefix.n_cp;
% wifi_params.coexistence.N_available_symbols = floor(wifi_params.coexistence.N_wifi_all_symbols/wifi_params.coexistence.N_samples_per_symbol);
% wifi_params.coexistence.N_available_REs = wifi_params.coexistence.N_available_symbols*wifi_params.mapping.n_data;
%

% %% definovano dle coexistence, lze prepracovat
% if strcmp(cp_length,'normal')
%     wifi_params.mapping.n_sf = 23*12;
%     wifi_params.coexistence.n_x12_syms = 23;
%     wifi_params.coexistence.n_rem_bits = rem(20000,wifi_params.mapping.n_sf*72);
% else
%     wifi_params.mapping.n_sf = 20*12;
%     wifi_params.coexistence.n_x12_syms = 20;
%     wifi_params.coexistence.n_rem_bits = rem(20000,wifi_params.mapping.n_sf*80);
% end
%
% wifi_params.coexistence.T_common = 1e-3;
% wifi_params.coexistence.N_wifi_all_symbols = wifi_params.Fs*wifi_params.coexistence.T_common;
% wifi_params.coexistence.N_samples_per_symbol = wifi_params.mapping.n_fft+wifi_params.cprefix.n_cp;
% wifi_params.coexistence.N_available_symbols = floor(wifi_params.coexistence.N_wifi_all_symbols/wifi_params.coexistence.N_samples_per_symbol);
% wifi_params.coexistence.N_available_REs = wifi_params.coexistence.N_available_symbols*wifi_params.mapping.n_data;



%% MCS definition
%
% if strcmp(decType,'llr') || strcmp(decType,'approximate llr')
%     disp('The demodulator needs a noise varience level setup');
% end

% N_scps = n_data



% % set actual MCS
% wifi_params.MCS = wifi_params.MCS(MCSvec(i_mcs)+1);

wifi_params.data_rate = (wifi_params.MCS(i_mcs).N_ss*wifi_params.MCS(i_mcs).CR*wifi_params.mapping.n_data*log2(wifi_params.MCS(i_mcs).M))/((wifi_params.mapping.n_fft/wifi_params.Fs)+(wifi_params.cprefix.t_cp));
wifi_params.data_rate_PSDU = ((LENGTH*8))/(wifi_params.coding.N_sym*((wifi_params.mapping.n_fft/wifi_params.Fs)+(wifi_params.cprefix.t_cp)));

str1 = sprintf('\n User data rate is %2.2f Mbps \r', wifi_params.data_rate/1e6);
str2 = sprintf(' PSDU data rate is %2.2f Mbps \n\r', wifi_params.data_rate_PSDU/1e6);
disp(str1)
disp(str2)

%% Create TX and RX object
txObj = transmitter(1,1,2412,wifi_params.MCS); % create transmitter object
rxObj = receiver(1,1,2412,wifi_params.MCS); % create receiver object

% n_users = [3 2]; % for test
% fcee = [2412 2420 2428]; % for test
% wifi_tx = transmitter(n_users(1),1,fcee,wifi_params.MCS(1));
%
% wifi_rx = receiver(n_users(2),2,fcee,wifi_params.MCS(1));
%
% snr_vec = 1:10;
% n_symbols = 10*12;
% %
% %
% sr = sim_results(n_users(1),n_users(2),snr_vec,n_symbols);
%
%
% for i_snr = 1:length(snr_vec)
%     for i_loop = 1:n_symbols
%
% %         for ii_tx = 1:n_users(1)
% %             for ii_rx = 1:n_users(2)
%
%                 TXout = randsrc(1,10,[0 1]);
%                 RXout = randsrc(1,10,[0 1]);
%
%                 save_loop_results(sr,TXout,RXout,i_loop,i_snr);
%
% %             end
% %         end
%
%     end
% end
clear decType

%% channel definition
wifi_params.channel.type = channelType;
fadingType = 'block';
wifi_params.channel.fading = fadingType;
wifi_params.channel.powerdB = [0 -9.7]; % -19.2 -22.8];
wifi_params.channel.delayTime = [0 110]*10^(-9); % 190 410]*10^(-9); % channel delay sample
% pridat switch pro definici kanalu dle normy

% clear channelType fadingType