% OFDM Channel snapshot, Institute of Telecommunications, University of Stuttgart, Germany

% www.inue.uni-stuttgart.de

% author: Maximilian Arnold, maximilian.wolfgang.arnold@gmail.com

% date: 7.7.2015

% Written for MATLAB

%

% EXAMPLE:  OFDM_snapshot(0,10,0,0,0,0,1,filename)

% PARAMETERS:

%  channel_model                  % Channel Modus

%  L,		                  % number of Taps of Channel

%  keepPlot,                      % if 1, then the results of the previous plot are shown in the following plot as well.

%  svg_en,                        % if 1, then also output svg-file

%  webdemo_en,                    % if 0, then test-mode in Octave/Matlab commandline, no need for webserver running

%  filename                       % output file for webdemo





% BEGIN, main function (control)



function OFDM_snapshot(channel_model, L, betrag,averaged,ftdomain,Nsub,plotNsub,complex_channel_coef,keepPlot,svg_en,webdemo_en,filename)



% default parameters

switch nargin
    
    case 0
        
        channel_model =0;
        
        L=10;
        
        betrag = 0;
        
        averaged =1;
        
        ftdomain = 2;
        
        Nsub = 8;
        plotNsub=1;
        complex_channel_coef =1;
        hideLegend = 1;
        
        keepPlot = 0;
        
        svg_en = 0;
        
        webdemo_en = 0;
        
        filename = 'OFDM_runN';
        
    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', 1);



if (webdemo_en)
    
    set(0, 'defaultaxesfontsize', 8);
    
    set(0, 'defaultaxesfontname', 'Helvetica');
    
    set(0, 'defaulttextfontsize', 8);
    
    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', 10);
    
    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;

clr  ={red(2,:), blue(2,:), green(2,:), magenta(4,:), cyan(4,:),  violett(4,:), seagreen(6,:), yellow(4,:) amber(4,:) ,lightgreen(4,:), seagreen(4,:), red(5,:), blue(5,:), green(5,:), magenta(8,:), cyan(8,:),  violett(8,:), seagreen(5,:), yellow(8,:) amber(8,:) ,lightgreen(8,:), seagreen(8,:),red(2,:), blue(2,:), green(2,:), magenta(4,:), cyan(4,:),  violett(4,:), seagreen(6,:), yellow(4,:) amber(4,:) ,lightgreen(4,:), seagreen(4,:), red(5,:), blue(5,:), green(5,:), magenta(8,:), cyan(8,:),  violett(8,:), seagreen(5,:), yellow(8,:) amber(8,:) ,lightgreen(8,:), seagreen(8,:),red(2,:), blue(2,:), green(2,:), magenta(4,:), cyan(4,:),  violett(4,:), seagreen(6,:), yellow(4,:) amber(4,:) ,lightgreen(4,:), seagreen(4,:), red(5,:), blue(5,:), green(5,:), magenta(8,:), cyan(8,:),  violett(8,:), seagreen(5,:), yellow(8,:) amber(8,:) ,lightgreen(8,:), seagreen(8,:)};
markers ={'x', 'o','+','.','s','^','v','h','p','d'};

% END, set up graphical output

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% BEGIN check input parameters for consistency




if(L<1 && L>50)
    
    warning('Order of channel needs to be in range 1-50')
    
end

if(Nsub>64)
    warning('Number of subcarriers too high')
end


% END check input parameters for consistency

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Parameter definitions:
number_of_channels = 1000;

if(channel_model==1)
    if(complex_channel_coef==0)
        alpha = - (1-sqrt(10))/L;
        k = 0:1:L;
        g_k = 1./(alpha*k+1).^2;
        for u=1:number_of_channels
            h_Channel = normrnd(0,1/sqrt(L+1),[1 L+1]);
            h_Channel_Energy(u,:) = h_Channel.*sqrt(g_k);
        end
        beta = 1/u*sum(sum(abs(h_Channel_Energy).^2));
    else
        alpha = - (1-sqrt(10))/L;
        k = 0:1:L;
        g_k = 1./(alpha*k+1).^2;
        for u=1:number_of_channels
            h_Channel = 1/sqrt(2)*(normrnd(0,1/sqrt(L+1),[1 L+1])+i*normrnd(0,1/sqrt(L+1),[1 L+1]));
            h_Channel_Energy(u,:) = h_Channel.*sqrt(g_k);
        end
        beta = 1/u*sum(sum(abs(h_Channel_Energy).^2));
    end
end

if(channel_model==2)
    if(complex_channel_coef==0)
        alpha = - (1-10)/L;
        k = 0:1:L;
        g_k = 1./(alpha*k+1).^2;
        for u=1:number_of_channels
            h_Channel = normrnd(0,1/sqrt(L+1),[1 L+1]);
            h_Channel_Energy(u,:) = h_Channel.*sqrt(g_k);
        end
        beta = 1/u*sum(sum(abs(h_Channel_Energy).^2));
    else
        alpha = - (1-10)/L;
        k = 0:1:L;
        g_k = 1./(alpha*k+1).^2;
        for u=1:number_of_channels
            h_Channel = 1/sqrt(2)*(normrnd(0,1/sqrt(L+1),[1 L+1])+i*normrnd(0,1/sqrt(L+1),[1 L+1]));
            h_Channel_Energy(u,:) = h_Channel.*sqrt(g_k);
        end
        beta = 1/u*sum(sum(abs(h_Channel_Energy).^2));
    end
end

clear h_GivenChannel;

% Gaussian channel model
if(averaged==0)
    number_of_channels=1;
end

if(averaged==1)
    number_of_channels=10;
end
if(averaged ==2 && ftdomain>0)
    number_of_channels=300;
end
data = zeros(20000,1);
data(1,1) = 1;

for x=1:number_of_channels
    if(channel_model==0)
        if(complex_channel_coef==0)
            h_Channel(x,:) = normrnd(0,1/sqrt(L+1),[1 L+1]);
        else
            h_Channel(x,:) = 1/2*(normrnd(0,1/sqrt(L+1),[1 L+1])+i*normrnd(0,1/sqrt(L+1),[1 L+1]));
        end
    end
    % decay 0.1 model
    if(channel_model==1)
        if(complex_channel_coef==0)
            h_Channel(x,:) = normrnd(0,1/sqrt(L+1),[1 L+1]);
            h_Channel(x,:) = 1/sqrt(beta)*h_Channel(x,:).*sqrt(g_k);
        else
            h_Channel(x,:) = 1/2*(normrnd(0,1/sqrt(L+1),[1 L+1])+i*normrnd(0,1/sqrt(L+1),[1 L+1]));
            h_Channel(x,:) = 1/sqrt(beta)*h_Channel(x,:).*sqrt(g_k);
        end
    end
    
    % decay 0.01  model
    
    if(channel_model==2)
        if(complex_channel_coef==0)
            h_Channel(x,:) = normrnd(0,1/sqrt(L+1),[1 L+1]);
            h_Channel(x,:) = 1/sqrt(beta)*h_Channel(x,:).*sqrt(g_k);
        else
            h_Channel(x,:) = 1/2*(normrnd(0,1/sqrt(L+1),[1 L+1])+i*normrnd(0,1/sqrt(L+1),[1 L+1]));
            h_Channel(x,:) = 1/sqrt(beta)*h_Channel(x,:).*sqrt(g_k);
        end
    end
    if(ftdomain>0)
        bandwitdh(x,:) = abs(fft(((filter(h_Channel(x,:),1,data)))))';
        bandwitdh(x,:) = length(bandwitdh(x,:))*real((bandwitdh(x,:)))./(sum(abs(real(bandwitdh(x,:)))));
    end
end



%% Average
if(number_of_channels>1 && averaged==2&& betrag ==0)
    h_Channel = sum(h_Channel)/number_of_channels;
end
if(number_of_channels>1 && averaged==2&& betrag ==1)
    h_Channel = sum(abs(h_Channel).^2)/number_of_channels;
end

%% Plotting

if (keepPlot)
    
    hold on;
    
else
    
    hold off;
    
end

if(ftdomain==0)
    if(complex_channel_coef==0)
        k=0:L;
        for z=1:length(h_Channel(:,1))
            if(betrag==0 ||averaged ==2)
                stem(k,h_Channel(z,:),'color',clr{z},'marker',markers{z},'Linewidth',2);
            end
            if(betrag==1 && averaged<2)
                stem(k,abs(h_Channel(z,:)),'color',clr{z},'marker',markers{z},'Linewidth',2);
            end
            hold on;
        end
        grid on;
        % Limits and Labels
        
        xlim([-1 L+1]);
        xlabel('m=t/T_m, B=1/T_m');
        if(betrag==0 )
            ylabel('h_m');
            ylim([-1 1]);
        else
            if(averaged==0)
                ylabel('|h_m|^2');
                ylim([0 1]);
            else
                ylabel('h_m');
                ylim([0 1]);
            end
        end
    else
        k=0:L;
        for z=1:length(h_Channel(:,1))
            if(betrag==0)
                subplot(2,2,1:2);
                stem(k,real(h_Channel(z,:)),'color',clr{z},'marker',markers{z},'Linewidth',2);
                hold on
                grid on;
                subplot(2,2,3:4);
                stem(k,imag(h_Channel(z,:)),'color',clr{z},'marker',markers{z},'LineStyle','-.','Linewidth',2);
                hold on;
            end
            if(betrag==1 && averaged ==2)
                 stem(k,h_Channel(z,:),'color',clr{z},'marker',markers{z},'Linewidth',2);
            end
            if(betrag==1 && averaged<2)
                stem(k,abs(h_Channel(z,:)),'color',clr{z},'marker',markers{z},'Linewidth',2);
            end
            hold on;
        end
        grid on;
        % Limits and Labels
        if(betrag==0)
            subplot(2,2,1:2);
            xlim([-1 L+1]);
            xlabel('m=t/T_m, B=1/T_m');
            subplot(2,2,3:4);
            xlim([-1 L+1]);
            xlabel('m=t/T_m, B=1/T_m');
            subplot(2,2,1:2);
            ylabel('Re\{h_m\}');
            ylim([-1 1]);
            subplot(2,2,3:4);
            ylabel('Im\{h_m\}');
            ylim([-1 1]);
        else
            if(averaged==0)
                xlim([-1 L+1]);
                xlabel('m=t/T_m, B=1/T_m');
                ylabel('|h_m|^2');
                ylim([0 1]);
            else
                xlim([-1 L+1]);
                xlabel('m=t/T_m, B=1/T_m');
                ylabel('h_m');
                ylim([0 1]);
            end
        end
    end
end
if(ftdomain==1)
    k=1:length(data);
    data_f=10*log10(real(bandwitdh).^2);
    data_f(:,1) = data_f(:,2);
    if(averaged==2)
        data_f = sum(data_f)/number_of_channels;
    end
    for z=1:length(data_f(:,1))
        plot(k-length(data)/(2*(Nsub+2)),data_f(z,:),'color',clr{z},'Linewidth',2);
        hold on;
    end
    if(averaged==1)
        data_k = data_f;
        data_f = 40*ones(1,length(data));
    end
    if(plotNsub==1)
        Nsub=Nsub+2;
        z = -40*ones(1,length(data));
        for p=1:Nsub-2
            hold on;
            X = [k(ceil(p*length(data)/Nsub):ceil((p+1)*length(data)/Nsub)), fliplr(k(ceil(p*length(data)/Nsub):ceil((p+1)*length(data)/Nsub)))];
            Y = [z(ceil(p*length(data)/Nsub+1+length(data)/(2*Nsub)):ceil((p+1)*length(data)/Nsub) +length(data)/(2*Nsub)),fliplr(data_f(ceil(p*length(data)/Nsub+1 +length(data)/(2*Nsub)):ceil((p+1)*length(data)/Nsub+length(data)/(2*Nsub) )))];
            if(length(X)>length(Y))
                X = X(1:length(Y));
            else
                Y= Y(1:length(X));
            end
            coloring = fill(X(1:length(Y)),Y,clr{p+1});
            set(coloring,'faceAlpha',.3);
            grid on;
        end
        
        X = [k(ceil(1):ceil((1*length(data)/Nsub))), fliplr(k(ceil(1):ceil((1*length(data)/Nsub))))];
        k=p-1;
        p=0;
        Y = [z(ceil(p*length(data)/Nsub+1+length(data)/(2*Nsub)):ceil((p+1)*length(data)/Nsub) +length(data)/(2*Nsub)),fliplr(data_f(ceil(p*length(data)/Nsub+1 +length(data)/(2*Nsub)):ceil((p+1)*length(data)/Nsub+length(data)/(2*Nsub) )))];
        coloring = fill(X(1:length(Y)),Y,clr{k+2});
        set(coloring,'faceAlpha',.3);
    end
    if(averaged==1)
        for z=1:length(data_k(:,1))
            plot(k-length(data)/(2*(Nsub+2)),data_k(z,:),'color',clr{z},'Linewidth',2);
            hold on;
        end
    end
    xlim([length(data)/(Nsub*2) 20000-3*length(data)/(2*Nsub)]);
    ylabel('|H(f)|^2 in dB');
    ylim([-40 40]);
    set (gca, 'xtick', [length(data)/(Nsub*2) 10000-length(data)/(2*Nsub) 20000-3*length(data)/(2*Nsub) ])
    grid on;
    set(gca,'xtickLabel',{'-B/2','f=0', 'B/2'});
end
if(ftdomain==2)
    subplot(2,2,1:2);
    if(complex_channel_coef==0)
        k=0:L;
        for z=1:length(h_Channel(:,1))
            if(betrag==0 ||averaged ==2)
                stem(k,h_Channel(z,:),'color',clr{z},'marker',markers{z},'Linewidth',2);
            end
            if(betrag==1 && averaged<2)
                stem(k,abs(h_Channel(z,:)),'color',clr{z},'marker',markers{z},'Linewidth',2);
            end
            hold on;
        end
        grid on;
        % Limits and Labels
        
        xlim([-1 L+1]);
        xlabel('m=t/T_m, B=1/T_m');
        if(betrag==0 )
            ylabel('h_m');
            ylim([-1 1]);
        else
            if(averaged==0)
                ylabel('|h_m|^2');
                ylim([0 1]);
            else
                ylabel('h_m');
                ylim([0 1]);
            end
        end
    else
        k=0:L;
        for z=1:length(h_Channel(:,1))
            if(betrag==0)
                subplot(2,2,1);
                stem(k,real(h_Channel(z,:)),'color',clr{z},'marker',markers{z},'Linewidth',2);
                hold on
                grid on;
                subplot(2,2,2);
                stem(k,imag(h_Channel(z,:)),'color',clr{z},'marker',markers{z},'LineStyle','-.','Linewidth',2);
                hold on;
            end
            if(betrag==1 && averaged ==2)
                 stem(k,h_Channel(z,:),'color',clr{z},'marker',markers{z},'Linewidth',2);
            end
            if(betrag==1 && averaged<2)
                stem(k,abs(h_Channel(z,:)),'color',clr{z},'marker',markers{z},'Linewidth',2);
            end
            hold on;
        end
        grid on;
        % Limits and Labels
        if(betrag==0)
            subplot(2,2,1);
            xlim([-1 L+1]);
            xlabel('m=t/T_m, B=1/T_m');
            subplot(2,2,2);
            xlim([-1 L+1]);
            xlabel('m=t/T_m, B=1/T_m');
            subplot(2,2,1);
            text(0.2,0.8,'Re\{h_m{\}');
            ylim([-1 1]);
            subplot(2,2,2);
            text(0.2,0.8,'Im\{h_m{\}');
            ylim([-1 1]);
        else
            if(averaged==0)
                xlim([-1 L+1]);
                xlabel('m=t/T_m, B=1/T_m');
                ylabel('|h_m|^2');
                ylim([0 1]);
            else
                xlim([-1 L+1]);
                xlabel('m=t/T_m, B=1/T_m');
                ylabel('h_m');
                ylim([0 1]);
            end
        end
    end
    subplot(2,2,3:4);
    k=1:length(data);
    data_f=10*log10(real(bandwitdh).^2);
    data_f(:,1) = data_f(:,2);
    if(averaged==2)
        data_f = sum(data_f)/number_of_channels;
    end
    for z=1:length(data_f(:,1))
        plot(k-length(data)/(2*(Nsub+2)),data_f(z,:),'color',clr{z},'Linewidth',2);
        hold on;
    end
    if(averaged==1)
        data_k=data_f;
        data_f = 40*ones(1,length(data));
    end
    if(plotNsub==1)
        Nsub=Nsub+2;
        z = -40*ones(1,length(data));
        for p=1:Nsub-2
            hold on;
            X = [k(ceil(p*length(data)/Nsub):ceil((p+1)*length(data)/Nsub)), fliplr(k(ceil(p*length(data)/Nsub):ceil((p+1)*length(data)/Nsub)))];
            Y = [z(ceil(p*length(data)/Nsub+1+length(data)/(2*Nsub)):ceil((p+1)*length(data)/Nsub) +length(data)/(2*Nsub)),fliplr(data_f(ceil(p*length(data)/Nsub+1 +length(data)/(2*Nsub)):ceil((p+1)*length(data)/Nsub+length(data)/(2*Nsub) )))];
            if(length(X)>length(Y))
                X = X(1:length(Y));
            else
                Y= Y(1:length(X));
            end
            coloring = fill(X(1:length(Y)),Y,clr{p+1});
            set(coloring,'faceAlpha',.3);
            grid on;
        end
        
        X = [k(ceil(1):ceil((1*length(data)/Nsub))), fliplr(k(ceil(1):ceil((1*length(data)/Nsub))))];
        k=p-1;
        p=0;
        Y = [z(ceil(p*length(data)/Nsub+1+length(data)/(2*Nsub)):ceil((p+1)*length(data)/Nsub) +length(data)/(2*Nsub)),fliplr(data_f(ceil(p*length(data)/Nsub+1 +length(data)/(2*Nsub)):ceil((p+1)*length(data)/Nsub+length(data)/(2*Nsub) )))];
        coloring = fill(X(1:length(Y)),Y,clr{k+2});
        set(coloring,'faceAlpha',.3);
    end
    if(averaged==1)
        for z=1:length(data_k(:,1))
            plot(k-length(data)/(2*(Nsub+2)),data_k(z,:),'color',clr{z},'Linewidth',2);
            hold on;
        end
    end
    xlim([length(data)/(Nsub*2) 20000-3*length(data)/(2*Nsub)]);
    ylabel('|H(f)|^2 in dB');
    ylim([-40 40]);
    set (gca, 'xtick', [length(data)/(Nsub*2) 10000-length(data)/(2*Nsub) 20000-3*length(data)/(2*Nsub) ])
    grid on;
    set(gca,'xtickLabel',{'-B/2','f=0', 'B/2'});
end
% Create an image file. This image is displayed on the webpage.

if (webdemo_en)
    
    print(filename, '-dpng',output_format);
    
end

if (svg_en)
    
    print(['fig_',filename], '-dsvg',output_format);
    
end



end



% END, main program (control)

%%%%%   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%












