%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% .m script of web demo "Insert title here" 
% Written for GNU Octave
% 
% INSTITUTE OF TELECOMMUNICATIONS  
% University of Stuttgart 
% www.inue-uni-stuttgart.de 
% author: Your Name   
% date: DD.MM.YYYY   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% This function creats animationNumberOfPictures frames and prints the values
% of the parameters that have been passed as well as an increasing frame number.
% Make sure that the function prototype matches the prototype defined
% in the XML config file. (Parameter names are arbitrary but should be the same
% for better readability) 

function [] = demo_LOS(plot_constellation_diagram, plot_impulse_response, colorize, percent_of_drawing, from_range, to_range, nr_of_frames, filename)
if (nargin == 0)
  plot_constellation_diagram = 1;
  plot_impulse_response = 1;
  colorize = 1;
  percent_of_drawing = 0.25;
  from_range = 1;
  to_range = 52;
  nr_of_frames = 1;
  filename = 'testoutput_of_frame';
end

warning('off','all');

% define formats
landscape = "-S930,350";
portrait  = "-S640,480";

% select format
output_format = portrait;

  load('101-127.mat');


if (from_range > to_range)
  temp_val=from_range;
  from_range=to_range;
  to_range=temp_val;
end

from_range=from_range+29;
to_range=to_range+29;



%which_data_plot = [1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1];
%
%nr_data_to_plot=sum(which_data_plot(from_range:to_range));



if (from_range>8 && from_range <23)
from_range=from_range-1;
elseif(from_range>22 && from_range <30)
from_range=from_range-2;
elseif(from_range>29 && from_range <37)
from_range=from_range-3;
elseif(from_range>36 && from_range <51)
from_range=from_range-4;
elseif(from_range>50)
from_range=from_range-5;
end

if (to_range>7 && to_range <22)
to_range=to_range-1;
elseif(to_range>21 && to_range <29)
to_range=to_range-2;
elseif(to_range>28 && to_range <36)
to_range=to_range-3;
elseif(to_range>35 && to_range <50)
to_range=to_range-4;
elseif(to_range>49)
to_range=to_range-5;
end
  

img_index = 0;
for k= 1:nr_of_frames
  % The frames must be numbered from 0 to animationNumberOfPictures - 1
  new_filename = [filename '_' num2str(img_index)];
  img_index = img_index + 1;

  % Create an invisible figure.
  fh = figure(1);
  set(fh, "visible", "off");
  
  % define style
  set(0, "defaultlinelinewidth", 2);
  set(0, "defaultaxesfontsize", 8);
  set(0, "defaultaxesfontname", "Helvetica");
  set(0, "defaulttextfontsize", 8);
  set(0, "defaulttextfontname", "Helvetica");

  % Plot into the figure
  clf reset
  hold on

  
  main_p=semilogy(LTF_Norm(k,:),'color','blue');
  if (colorize == 1)
  semilogy(5:11,LTF_Norm(k,5:11),'color','yellow');          
  semilogy(13:25,LTF_Norm(k,13:25),'color','cyan');     
  semilogy(27:32,LTF_Norm(k,27:32),'color','black');              
  semilogy(34:39,LTF_Norm(k,34:39),'color','magenta');      
  semilogy(41:53,LTF_Norm(k,41:53),'color','green');    
  semilogy(55:61,LTF_Norm(k,55:61),'color','red');      
  end

  XTick = 0:8:64;
  XTick(1)=1;
  XTickLabel = -32:8:32;
  XTickLabel(end)=31;
  YTickLabel = [-50 -40 -30 -20 -10 0 10];

  set(gca,'XTick',XTick,'XTickLabel',XTickLabel,'YTickLabel',YTickLabel,'XLim',[XTick(1) XTick(end)],'FontSize',7)
  
  ylim([0.00001 10]);
  xlabel("Subarrier-Nr.");
  ylabel("Normalized Subcarrier-magnitude in dB");
  %if (plot_constellation_diagram ~= 1)
    title(['Distance= ' num2str((k-1)*2) ' meter, EVM = ' num2str(EVM(k)) ' dB']);
  %else
  %  title(['Distance = ' num2str((k-1)*2) ' meter']);
  %end
  
  noise_floor=mean(LTF_Norm(k,[1 2 3 4 62 63 64]));
  %noise_floor=mean(LTF_Norm(k,[1 2 3 63 64]));
  plot([-100 100],[noise_floor noise_floor],'color','red');
%        text(24, noise_floor*2,['Noise floor = ' num2str(10*log10(noise_floor)) 'dB'], "color", "red");
  text(27, noise_floor*2,'Noise floor', "color", "red");
  
  if (plot_impulse_response == 1)
    text(11.5,1.7e-5,'microseconds');
  end 
  
  
  if (plot_constellation_diagram == 1)
  
  
    sub_ax = axes('Position', [0.65 0.20 0.25 0.25]);
    %set(sub_ax,'color','red');
    
    for length_of = 1:size(DATA,3)
      if (DATA(k,10,length_of) == 0)
        length_of = length_of-1;
        break;
      end
    end
    
    nr_symbols=length_of;
    nr_symbols_to_draw=ceil(nr_symbols*percent_of_drawing);
    
    hold on
    if (colorize == 1)
      for sub_carrier = from_range:to_range
          const_plot = plot((squeeze(DATA(k,sub_carrier,1:ceil(nr_symbols_to_draw*0.95)))),'*','MarkerSize',0.1); axis equal;
          if (sub_carrier >=1 && sub_carrier <=7)
            set(const_plot,'color','yellow');
          elseif(sub_carrier >=8 && sub_carrier <=20)
            set(const_plot,'color','cyan');
          elseif(sub_carrier >=21 && sub_carrier <=26)
            set(const_plot,'color','black');
          elseif(sub_carrier >=27 && sub_carrier <=32)
            set(const_plot,'color','magenta');
          elseif(sub_carrier >=33 && sub_carrier <=45)
            set(const_plot,'color','green');
          elseif(sub_carrier >=46 && sub_carrier <=52)
            set(const_plot,'color','red');
          end
        end
        for ofdm_symbol = ceil(nr_symbols_to_draw*0.95):nr_symbols_to_draw
          for sub_carrier = from_range:to_range
            const_plot = plot((squeeze(DATA(k,sub_carrier,ofdm_symbol))),'*','MarkerSize',0.1); axis equal;
	    if (sub_carrier >=1 && sub_carrier <=7)
	      set(const_plot,'color','yellow');
	    elseif(sub_carrier >=8 && sub_carrier <=20)
	      set(const_plot,'color','cyan');
	    elseif(sub_carrier >=21 && sub_carrier <=26)
	      set(const_plot,'color','black');
	    elseif(sub_carrier >=27 && sub_carrier <=32)
	      set(const_plot,'color','magenta');
	    elseif(sub_carrier >=33 && sub_carrier <=45)
	      set(const_plot,'color','green');
	    elseif(sub_carrier >=46 && sub_carrier <=52)
	      set(const_plot,'color','red');
	    end
          end
        end

    else
      plot(((squeeze(DATA(k,:,1:nr_symbols_to_draw)))),'*','MarkerSize',0.1,'color','red'); axis equal;
    end
  
  %xlabel('RE');
  %ylabel('IM');
  %set(sub_ax, 'Title', ['EVM = ' num2str(EVM(k)) 'dB']);
  xlim([-1.5 1.5]);
  ylim([-1.5 1.5]);

  end
  

  if (plot_impulse_response == 1)


    sub_ax_2 = axes('Position', [0.23 0.20 0.2 0.25]);
    

    length_impulse=9;
    %figure;plot(abs(ifft(LTF_Norm(k,:)))(1:length_impulse))
    calc_ifft=LTF_Norm(k,:);
    calc_ifft(1:4)=0;
    calc_ifft(62:64)=0;
    calc_ifft(33)=(calc_ifft(32)+calc_ifft(34))/2;
    %calc_ifft(33)=0;
    
    impulse_response = abs(ifft(calc_ifft))(1:length_impulse);
    impulse_response_norm = impulse_response/impulse_response(1);
    %impulse_response_norm_db = 10*log10(impulse_response_norm);

    %all_echos = abs(ifft(calc_ifft))(1:33);
    %all_echos_power = sum(abs(all_echos).^2);
    %viewd_echos = abs(ifft(calc_ifft))(1:length_impulse);
    %viewd_echos_power = sum(abs(viewd_echos).^2);

    %figure;stem(impulse_response)
    stem(impulse_response_norm)
    %figure;stem(impulse_response_norm_db)

    XTick = 1:4:length_impulse;
    XTickLabel=0:4:length_impulse;
    XTickLabel=XTickLabel/10;
    set(gca,'XTick',XTick,'XTickLabel',XTickLabel);
    %xlabel("time in microseconds");
    %ylabel("amplitude");
    %ylabel("normalized amplitude ");
    %percent_of_viewed_power=(viewd_echos_power/all_echos_power)*100;
    %title(['viewed impuls response equals ' num2str(percent_of_viewed_power)  '% of whole impulse response']);
  end
  
  
  
  hold off


  % Create an image file. This png image is displayed on the webpage. Its size 
  % must be 640x480.
  print(new_filename, "-dpng", output_format);
  clf;
end
end





    
%      plot(((squeeze(DATA(k,1:7,ofdm_sym)))),'*','MarkerSize',0.1,'color','red');       axis equal;
%      plot(((squeeze(DATA(k,8:20,ofdm_sym)))),'*','MarkerSize',0.1,'color','yellow');  axis equal;
%      plot(((squeeze(DATA(k,21:26,ofdm_sym)))),'*','MarkerSize',0.1,'color','cyan');    axis equal;
%      plot(((squeeze(DATA(k,27:32,ofdm_sym)))),'*','MarkerSize',0.1,'color','green');   axis equal;
%      plot(((squeeze(DATA(k,33:45,ofdm_sym)))),'*','MarkerSize',0.1,'color','magenta'); axis equal;
%      plot(((squeeze(DATA(k,46:52,ofdm_sym)))),'*','MarkerSize',0.1,'color','black');   axis equal;
