% showAndSavePlotsToPNG
%% plot all coded and uncoded BER results
tabMods = [0 1;... % i_mcs, k = log2(M)
    1 2;...
    2 2;...
    3 4;...
    4 4;...
    5 6;...
    6 6;...
    7 6];
plotMarkersList = ['o', 's', '*', 'd', 'v', '^', '.', 'p'];

if i_mcs == 1
    h1 = figure;
end
figure(1);
% Uncoded
subplot(121)
ber_uncoded = results.ber_uncoded_data;

if i_mcs == 1 || i_mcs == 2 || i_mcs == 4 || i_mcs == 8
    semilogy(SNR,ber_uncoded,['k-',plotMarkersList(i_mcs)]);
    hold on
end

if i_mcs == length(MCSvec)
    
    legend('BPSK ', 'QPSK ', '16QAM', '64QAM','Location','NorthEast');
    
    grid on
    axis([SNR(1) SNR(end)+10 10^(-6) 1])
    
    xlabel('CNR [dB]');
    ylabel('BER [-]');
    title(['uncoded|BW_{LTE}=',num2str(agBW/1e6),'MHz|f_c=',num2str(wifi_params.coexistence.fc/1e6),'MHz']);
end
% Coded
subplot(122)
ber_coded = results.ber_data;
semilogy(SNR,ber_coded,['k-',plotMarkersList(i_mcs)]);
hold on
if i_mcs == length(MCSvec)
    legTmp = repmat('MCS ',length(MCSvec),1);
    legTmp = [legTmp,num2str(MCSvec')];
    legend(legTmp,'Location','SouthWest')
    
    grid on
    axis([SNR(1) SNR(end)+10 10^(-6) 1])
    
    xlabel('CNR [dB]');
%     ylabel('BER [-]');
    title(['coded|BW_{LTE}=',num2str(agBW/1e6),'MHz|f_c=',num2str(wifi_params.coexistence.fc/1e6),'MHz']);
end

if i_mcs == length(MCSvec)
    % upravit pro ukladani orientacnich grafu v PNG
    plot_filename = sprintf('wifi_20MHz_lte_%2.2fMHz_koex_fc_%dMHz_from%dto%d_dB_CP%dns_%d_repeat_Mode_%s',...
        agParams.BW/1e6,...
        wifi_params.coexistence.fc/1e6,...
        SNR(1),...
        SNR(end),...
        wifi_params.cprefix.t_cp,...
        N,...
        wifiMode);
    print(h1,'-dpng',fullfile('./grafy',[plot_filename, '.png']));
    %      saveas(h,fullfile('./grafy',[plot_filename, '.png']));
    close
end
%
% close all
%% plot coded and uncoded results with relevant MCS
if 1
    if i_mcs == 1
        h2 = figure;
    end
    figure(2);
    
    % plot uncoded BER:
    if i_mcs == 1
        % BPSK
        subplot(141)
        semilogy(SNR,results.ber_uncoded_data,'r-')
        axis([SNR(1) SNR(end) 10^(-6) 1])
        grid on
        hold on
        xlabel('CNR [dB]');
        ylabel('BER [-]');
    elseif i_mcs == 2
        % QPSK
        subplot(142)
        semilogy(SNR,results.ber_uncoded_data,'r-')
        axis([SNR(1) SNR(end) 10^(-6) 1])
        grid on
        hold on
        xlabel('CNR [dB]');
%         ylabel('BER [-]');
    elseif i_mcs == 4
        % 16QAM
        subplot(143)
        semilogy(SNR,results.ber_uncoded_data,'r-')
        axis([SNR(1) SNR(end) 10^(-6) 1])
        grid on
        hold on
        xlabel('CNR [dB]');
%         ylabel('BER [-]');
    elseif i_mcs == 6
        % 64QAM
        subplot(144)
        semilogy(SNR,results.ber_uncoded_data,'r-')
        axis([SNR(1) SNR(end) 10^(-6) 1])
        grid on
        hold on
        xlabel('CNR [dB]');
%         ylabel('BER [-]');
    end
    
    % plot coded BER:
    if i_mcs == 1
        % BPSK
        subplot(141)
        semilogy(SNR,results.ber_data,['k-',plotMarkersList(i_mcs)])
        axis([SNR(1) SNR(end) 10^(-6) 1])
        grid on
        hold on
        xlabel('CNR [dB]');
        ylabel('BER [-]');
    elseif i_mcs == 2 || i_mcs == 3
        % QPSK
        subplot(142)
        semilogy(SNR,results.ber_data,['k-',plotMarkersList(i_mcs)])
        axis([SNR(1) SNR(end) 10^(-6) 1])
        grid on
        hold on
        xlabel('CNR [dB]');
%         ylabel('BER [-]');
    elseif i_mcs == 4 || i_mcs == 5
        % 16QAM
        subplot(143)
        semilogy(SNR,results.ber_data,['k-',plotMarkersList(i_mcs)])
        axis([SNR(1) SNR(end) 10^(-6) 1])
        grid on
        hold on
        xlabel('CNR [dB]');
%         ylabel('BER [-]');
    elseif i_mcs == 6 || i_mcs == 7 || i_mcs == 8
        % 64QAM
        subplot(144)
        semilogy(SNR,results.ber_data,['k-',plotMarkersList(i_mcs)])
        axis([SNR(1) SNR(end) 10^(-6) 1])
        grid on
        hold on
        xlabel('CNR [dB]');
%         ylabel('BER [-]');
    end
    
    if i_mcs == length(MCSvec)
    % upravit pro ukladani orientacnich grafu v PNG
    plot_filename = sprintf('wifi_20MHz_lte_%2.2fMHz_koex_fc_%dMHz_from%dto%d_dB_CP%dns_%d_repeat_Mode_%s_accToModulation',...
        agParams.BW/1e6,...
        wifi_params.coexistence.fc/1e6,...
        SNR(1),...
        SNR(end),...
        wifi_params.cprefix.t_cp,...
        N,...
        wifiMode);
    print(h2,'-dpng',fullfile('./grafy',[plot_filename, '.png']));
    %      saveas(h,fullfile('./grafy',[plot_filename, '.png']));
    close
    end
end