function[] = runCapacityFun(ch_lim, con, plotm, fn)
% clear all
% close all
% clc

% define style
set(0, "defaultlinelinewidth", 2);
set(0, "defaultaxesfontsize", 8);
set(0, "defaultaxesfontname", "Helvetica");
set(0, "defaulttextfontsize", 8);
set(0, "defaulttextfontname", "Helvetica");


% input variables
switch ch_lim
	case 0
		channel_lim = 'bipolar';
	case 1	
		channel_lim = 'unipolar';
end

switch con
	case 0
		constr = 'pwr';
	case 1 
		constr = 'avg';
	case 2
		constr = 'amp';
end

switch plotm
	case 0
		plot_mode = 'single';
	case 1
		plot_mode = 'double_2d';
	case 2
		plot_mode = 'single_2d';
	case 3
		plot_mode = 'single_3d';
end

%channel_lim = 'unipolar'; % = {'bipolar','unipolar'}
%constr = 'pwr'; % = {'pwr','avg','amp'}
%plot_mode = 'single_2d'; % = {single, single_2d, single_3d, double_2d}
filename = fn;

% load corresponding file
switch channel_lim
    case 'bipolar'
        load capacity_bipolar.mat
    case 'unipolar'
        load capacity_unipolar.mat
    otherwise
        error('Channel limitation unknown!')
end

chart_notpossible = false;

switch constr
    case 'pwr'
        x = x_pwr;
        log_px_set = log(px_set_pwr);
    case 'amp'
        x = x_amp;
        log_px_set = log(px_set_amp);
    case 'avg'
        switch channel_lim
            case 'unipolar'
                log_px_set = log(px_set_mean);
                x = x_mean;
            case 'bipolar'
                log_px_set = log(px_set_amp);
                chart_notpossible = true;
        end
end

% CAPACITY VS. NOISE POWER CHART

% compute Shannon Capacity limit for real channels
C_real = 0.5.*log2( 1 + 1./(sigma_range.^2));
C_uni = 0.5.*log2(1+exp(1)./(2.*pi.*sigma_range.^2))
figure(1);

switch plot_mode
    case {'single','double_2d'}
        
        switch channel_lim
            case 'bipolar'
                if strcmp(plot_mode,'double_2d')
                    subplot(2,1,1);
                end
                plot(-20.*log10(sigma_range),C_set_pwr,'LineWidth',2,'Color',[0.85 0.16 0])
                hold on
                plot(-20.*log10(sigma_range),C_set_amp,'LineWidth',2,'Color',[0 0.65 0])
                plot(-20.*log10(sigma_range),C_real,'LineWidth',2,'Color',[0 0 0],'LineStyle','--')
                hold off
                xlabel('-20*log_{10}(\sigma_n)')
                ylabel('Capacity [bit/channel use]')
                grid on
                legend('E[x^2]=1', 'max{|x|}=1','C_{bip}','Location','NorthWest')
%                 title('Bipolar AWGN channel')
                if strcmp(plot_mode,'double_2d')
                    set(gca,'Position',[0.13 0.59140365448505 0.675 0.33359634551495]);
                end
            case 'unipolar'
                if strcmp(plot_mode,'double_2d')
                    subplot(2,1,1);
                end
                plot(-20.*log10(sigma_range),C_set_pwr,'LineWidth',2,'Color',[0.85 0.16 0])
                hold on
                plot(-20.*log10(sigma_range),C_set_mean,'LineWidth',2,'Color',[0.043 0.518 0.780])
                plot(-20.*log10(sigma_range),C_set_amp,'LineWidth',2,'Color',[0 0.65 0])
                plot(-20.*log10(sigma_range),C_real,'LineWidth',2,'Color',[0 0 0],'LineStyle','--')
                plot(-20.*log10(sigma_range),C_uni,'LineWidth',2,'Color',[0.043 0.518 0.780],'LineStyle','--')
                hold off
                xlabel('-20*log_{10}(\sigma_n)')
                ylabel('Capacity [bit/channel use]')
                grid on
                legend('E[x^2]=1','E[x]=1','max{|x|}=1','C_{bip}','C_{unip}','Location','NorthWest')
%                 title('Unipolar AWGN channel')
                if strcmp(plot_mode,'double_2d')
                    set(gca,'Position',[0.13 0.59140365448505 0.675 0.33359634551495]);
                end
            otherwise
                error('Channel limitation unknown!')
        end
end

switch plot_mode
    case {'double_2d','single_2d'}
        
        if strcmp(plot_mode,'double_2d')
            subplot(2,1,2);
        end;
        
        % 2-D PDF CHART
        if chart_notpossible
            text(0.075,0.5,'The selected chart is not available','Color',[0.043 0.518 0.780],'FontSize',10);
            grid on
        else
            imagesc(-20.*log10(sigma_range),x,log_px_set')
            grid on
            cb = colorbar;
            xlabel('-20*log_{10}(\sigma_n)')
            ylabel('x')
            zlab = get(cb,'ylabel');
            set(zlab,'String','ln(p_x(x))');
            set(gca,'YDir','normal')
            [a,~] = caxis;
            caxis([a,0])
            if strcmp(plot_mode,'double_2d')
                set(gca,'Position',[0.13 0.1100 0.675 0.3412]);
            end; 
        end
        
    case 'single_3d'
        
        % 3-D PDF CHART
        if chart_notpossible
            text(0.075,0.5,'The selected chart is not available','Color',[0.043 0.518 0.780],'FontSize',10);
            grid on
        else
            surf(-20.*log10(sigma_range),x,log_px_set','EdgeColor','none')
            cb = colorbar;
            xlabel('-20*log_{10}(\sigma_n)')
            ylabel('x')
            zlab = get(cb,'ylabel');
            set(zlab,'String','ln(p_x(x))');
            [a,~] = caxis;
            caxis([a,0])
            
            % set appropriate viewing angle
            switch channel_lim
                case 'bipolar'
                    switch constr
                        case 'pwr'
                            view(70,15);
                        case 'amp'
                            view(45,60);
                    end
                case 'unipolar'
                    switch constr
                        case 'pwr'
                            view(125,25);
                        case 'amp'
                            view(60,45);
                        case 'avg'
                            view(125,25);
                    end
            end
            grid on
        end
end

print(filename, '-dpng', '-S640,480', '-tight');

