 %BER ISI of OFDM over different Subcarrier, Institute of Telecommunications, University of Stuttgart, Germany

% www.inue.uni-stuttgart.de

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

% date: 27.6.2015

% update: 27.6.2015

%

% Written for MATLAB

%

% EXAMPLE:  OFDM_ISI(64, 10,0,0,0,1,filename)

% PARAMETERS:

%  number_of_channels,			  % number of Channels to average over

%  L,		                      % number of Taps of Channel

%  Betrag                         % Plot squared magnitude

%  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_ISI(number_of_subcarrier, L,betrag,keepPlot,svg_en,webdemo_en,filename)

% default parameters

switch nargin

    case 0

        number_of_subcarrier =64;

        L=10;

        hideLegend = 1;
        
        betrag = 0;

        keepPlot = 0;

        svg_en = 0;

        webdemo_en = 0;

        filename = 'OFDM_ISI';

    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;





% END, set up graphical output

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





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

% BEGIN check input parameters for consistency
if(number_of_subcarrier<0 && number_of_subcarrier>256)
    warning('Out of range');
end
if(L<1 && L>1024)
    warning('Order of channel needs to be in range 1-1024')
end

if(betrag<0 && betrag>1)
    warning('Only checkbox options, sorry pal')
end



% END check input parameters for consistency

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





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
       
        %% Given Channel
                

       % Gaussian channel model

        h_GivenChannel = normrnd(0,1,[1 L+1]);
 
        % Normalize Channels
     
        h_GivenChannel = 1/sqrt(L+1)*h_GivenChannel;
        d(2*number_of_subcarrier)=0;
        x(2*number_of_subcarrier)=0;
        x(1) =1;
        x(number_of_subcarrier)=1;
        i = (1:(L));
        j = (L+1:number_of_subcarrier);
        u = (number_of_subcarrier+1:number_of_subcarrier+L);
        v = (number_of_subcarrier+L+1:2*number_of_subcarrier);
        y = filter(h_GivenChannel,1,x);
   
  

        
%% Plotting

if (keepPlot)

    hold on;

else

    hold off;

end

if(betrag==0)
    stem(i,y(1:L),'r');
    hold on;
    stem(j,d(L+1:number_of_subcarrier),'.b');
    hold on;
    stem(u,y(1:L),'.r');
    hold on;
    stem(v,d(L+1:number_of_subcarrier),'.b');
    hold on;
    ylim([-1 1]);
else
    stem(i,y(1:L).^2,'.r');
    hold on;
    stem(j,d(L+1:number_of_subcarrier).^2,'.b');
    hold on;
    stem(u,y(1:L).^2,'.r');
    hold on;
    stem(v,d(L+1:number_of_subcarrier).^2,'.b');
    hold on;
    ylim([0 max(abs(y))]);
end
grid minor;



% Labels
xlabel('k=t/T_s');
ylabel('ISI between OFDM symbols');
set(gca, 'xTick',[0 number_of_subcarrier number_of_subcarrier*2]);
set(gca, 'xTickLabel',{ '0', '1', '2'});
xlim([0 2*(number_of_subcarrier)]);
% 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)

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







