%% sim_main_single.m
% file with main simulation loops (no parallel processing)

for i_snr = 1:length(SNR) % smycka SNR
    disp([' SNR = ',num2str(SNR(i_snr)),' dB | ',num2str(i_snr),'/',num2str(length(SNR)),' SNR values']);
    
    for i_loop = 1:N_frames; % vnitrni smycka - pocet behu
        if mod(i_loop,1000) == 0
            disp(['Part ',num2str(i_loop/1000),'/',num2str(N_frames/1000)]);
        end
        
%         switch mode
%             case g
%                 
%             case n
%                 
%             case ac
        txObj = WIFI_TX(wifi_params,txObj,i_mcs); % perform transmitter operations
%         
%             otherwise
%                 
%         end
        LENGTH
        size(txObj.output.x_s)
        wifi_y_tx = txObj.output.x_s;
        save(['WIFI_',wifi_standard,'_LENGTH_',num2str(LENGTH),'.mat'],'wifi_y_tx')
        
        clear wifi_y_tx
        
        % sigma_v = 10^(-EbNo/20);
        
        % upconverter option %% NENI POTREBA -> dodelat pozdeji, viz
        % load_wifi_params.m, radek ~90
        %         txObj = WIFI_upconverter(txObj, wifi_params, agOut, agParams, useGPU);
        %         plotSpectrum(txObj.output.txUpSignal,txObj.output.genie.fs);
        % or usrp
        
        %% interferring signal
        % load - nahrat signal LTE dle parametru (BW = 1.4 -- 20 MHz)
        
        %% channel model and noise
        %         %% Prevzorkovani rusiveho signalu (LTE) a jeho upkonverze - NENI POTREBA, viz vyse
        %         if ((i_snr == 1) && (i_loop == 1))
        %             K = gcd(agParams.Fs,wifi_params.Fs);
        %             L = wifi_params.Fs/K;
        %             M = agParams.Fs/K;
        %             agOut.y_tx = resample(agOut.y_tx,L,M);
        %
        %             [agOut] = LTE_upconverter(agOut, agParams, wifi_params, useGPU, txObj);
        %             save('InterfSignalTemp.mat','agOut');
        %             %             pwelch(agOut.txUpSignal)
        %         else
        %             load('InterfSignalTemp.mat');
        %         end
        %
        %         P_LTE = (norm(agOut.txUpSignal)^2)/(length(agOut.txUpSignal));
        %         P_WiFi = (norm(txObj.output.txUpSignal)^2)/(length(txObj.output.txUpSignal));
        %
        %         W_WiFi = 1;
        %         W_LTE = ((10^(-(SNR(i_snr))/10))/W_WiFi);
        %         txObj.output.txUpSignal = W_LTE*agOut.txUpSignal+W_WiFi*txObj.output.txUpSignal;
        % %         plotSpectrum(txObj.output.txUpSignal,txObj.output.genie.fs)
        
        channelObj = channel(wifi_params,txObj.nTXant,rxObj.nRXant); % mozna presunout mimo tuto smycku - parametry kanalu jsou znamy jiz pred simulaci
        
        channelObj.x_h = conv(txObj.output.x_s,channelObj.h);
        channelObj.SNR = SNR(i_snr);
        %         channelObj.x_h = conv(txObj.output.txUpSignal,channelObj.h);
        % noise
        n = 10^(-SNR(i_snr)/20)*((randn(size(channelObj.x_h))+1j*(randn(size(channelObj.x_h))))/sqrt(2));
        v = sqrt(wifi_params.mapping.n_fft/wifi_params.mapping.n_tot) * n;
        
        % awgn or fading channel + white noise
        channelObj.x_hn = channelObj.x_h+v;
        
        %% downconverter option -> NENI POTREBA, viz vyse
        % or usrp
        
        %         [ChanMod_output, txObj] = WIFI_downconverter(channelObj.x_hn, txObj, wifi_params, useGPU);
        
        % receiver
        
        %         rxObj = WIFI_RX(wifi_params,rxObj,channelObj, ChanMod_output);
        rxObj = WIFI_RX(wifi_params,rxObj,channelObj, channelObj.x_hn,i_mcs);
        results = result_calculation(results,txObj,rxObj,i_snr,i_loop,N_frames);
    end % inner loop
    % results evaluation % using OOP
end % outer loop