function demo_mimo(
	env_sel,		% environment selection
	SpacingRx,		% antenna spacing of uniform linear antenna array
	SpacingTx,
	mimo_sel,		% 2x2 or 4x4 MIMO
	tap,			% tap to plot
	filename		% output file for webdemo 
)

% create figure and set style 
fig = figure(1); 
set(fig, "visible", "on"); 
landscape = "-S930,350"; 
portrait  = "-S640,480"; 
output_format = portrait; 
set(0, "defaultlinelinewidth", 2); 
set(0, "defaultaxesfontsize", 8); 
set(0, "defaultaxesfontname", "Helvetica"); 
set(0, "defaulttextfontsize", 8); 
set(0, "defaulttextfontname", "Helvetica"); 
const_lw = 2; 
const_msize = 2; 

% Parameters
Connection = 'downlink';

switch(env_sel)
	case 1
		ID = 'A';
	case 2
		ID = 'B';
	case 3 
		ID = 'C';
	case 4 
		ID = 'D';
	case 5
		ID = 'E';
	otherwise
		ID = 'F';
endswitch

CorrelationCoefficientType = 'complex';

if (mimo_sel == 1)
	NumberOfXXAntennas = 2;
else
	NumberOfXXAntennas = 4;
endif

CarrierFrequency_Hz = 2.4*10^9;

distance_Tx_Rx_m = 10;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The following functions are taken from L. Schumacher, "WLAN MIMO channel matlab program", Online: http://www.info.fundp.ac.be/∼lsc/Research/IEEE_80211_HTSG_CMSC/distributionterms.html, 2003.


[d_bp, sigma_S, tap_delay, tap_power, cl_AoA_rAS_AoD_tAS, cl_power, cl_9, cl_1113, cl_15, cl_1012, cl_13, cl_14, rtp] = gep(env_sel)
[PDP_linear, RTx, RRx, Fading_Type, RiceMatrix, K_factor_dB, ...
        PathLoss_dB, ShadowFading_dB, DirectionOfMovement_rad, ...
        AngleAtMobile_rad, ASAtMobile_rad] = ...
        IEEE_802_11_Cases(Connection, ID, CorrelationCoefficientType, ...
        CarrierFrequency_Hz, NumberOfXXAntennas, SpacingTx, ...
        NumberOfXXAntennas, SpacingRx, distance_Tx_Rx_m)
[C, R] = init_MIMO_channel(RTx,RRx, 'complex')

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

ax = 1:NumberOfXXAntennas;



if (tap > length(tap_power))
	tap = length(tap_power);
endif

subplot(2,2,1)
RTx_plot = zeros(size(RTx, 2), size(RTx, 3));
for ii = 1:size(RTx, 2)
	for kk = 1:size(RTx, 3)
		RTx_plot(ii,kk) = RTx(tap,ii,kk)
	endfor
endfor
imagesc(abs(RTx_plot));
title(["TX correlation matrix"]);
nb_cols=256;
      newmap= jet(nb_cols);
      % generate smooth transition of jet() from "black to white" brightness
      for (k=1:nb_cols)
        newmap(k,1:3) = (newmap(k,1:3)*(k-1) + [nb_cols-k, nb_cols-k, nb_cols-k])/(nb_cols-1);
      end
      jet_light_inue=newmap;
      colormap( jet_light_inue );
caxis([0 1]);
colorbar
if(mimo_sel==1)
	set(gca,'XTick',[1:2]);
	set(gca,'YTick',[1:2]);
	set(gca,'XTickLabel',[])%'Tx_1'; 'Tx_2']);
	set(gca,'YTickLabel',[])%'Tx_1'; 'Tx_2']);
	for ii = 1:2
		for kk = 1:2
			text(ii-0.2,kk, ["Tx_{" num2str(kk) num2str(ii)]);
		endfor
	endfor
else
	set(gca,'XTick',[1:4]);
	set(gca,'YTick',[1:4]);
	set(gca,'XTickLabel',[])%'Tx_1'; 'Tx_2'; 'Tx_3'; 'Tx_4']);
	set(gca,'YTickLabel',[])%'Tx_1'; 'Tx_2'; 'Tx_3'; 'Tx_4']);
	for ii = 1:4
		for kk = 1:4
			text(ii-0.4,kk, ["Tx_{" num2str(kk) num2str(ii)]);
		endfor
	endfor
endif

subplot(2,2,2)
RRx_plot = zeros(size(RRx, 2), size(RRx, 3));
for ii = 1:size(RRx, 2)
	for kk = 1:size(RRx, 3)
		RRx_plot(ii,kk) = RRx(tap,ii,kk)
	endfor
endfor
imagesc(abs(RRx_plot));
title(["RX correlation matrix"]);
nb_cols=256;
      newmap= jet(nb_cols);
      % generate smooth transition of jet() from "black to white" brightness
      for (k=1:nb_cols)
        newmap(k,1:3) = (newmap(k,1:3)*(k-1) + [nb_cols-k, nb_cols-k, nb_cols-k])/(nb_cols-1);
      end
      jet_light_inue=newmap;
      colormap( jet_light_inue );
caxis([0 1]);
colorbar
if(mimo_sel==1)
	set(gca,'XTick',[1:2]);
	set(gca,'YTick',[1:2]);
	set(gca,'XTickLabel',[])%'Tx_1'; 'Tx_2']);
	set(gca,'YTickLabel',[])%'Tx_1'; 'Tx_2']);
	for ii = 1:2
		for kk = 1:2
			text(ii-0.2,kk, ["Rx_{" num2str(kk) num2str(ii)]);
		endfor
	endfor
else
	set(gca,'XTick',[1:4]);
	set(gca,'YTick',[1:4]);
	set(gca,'XTickLabel',[])%'Tx_1'; 'Tx_2'; 'Tx_3'; 'Tx_4']);
	set(gca,'YTickLabel',[])%'Tx_1'; 'Tx_2'; 'Tx_3'; 'Tx_4']);
	for ii = 1:4
		for kk = 1:4
			text(ii-0.4,kk, ["Rx_{" num2str(kk) num2str(ii)]);
		endfor
	endfor
endif

%ax = subplot(2, 2, 3);
%xlim([0 4]);
%ylim([0 4]);
%set(gca,'YDir', 'reverse');
%countstr = '1';
%text(1,1,countstr);
%xx = 0;
%yy = 0;
%for ii = 1:size(RRx, 2)
%	for kk = 1:size(RRx, 3)
%		text(xx,yy, [num2str(round(abs(RRx_plot(ii,kk))*10)/10)]);
%		xx = xx + 2;		
%	endfor
%	xx = 0;
%	yy = yy + 2;
%endfor
%set (ax, 'visible', 'on');

ax = subplot(2,2,3);
text(0, 0.5, ["Kronecker product of TX and RX"; "correlation matrices =>"; "check the description slide"; "for more information"]);
set (ax, 'visible', 'off');
subplot(2,2,4)
if (mimo_sel == 1)
	imagesc(abs(R(tap*4-3:tap*4, tap*4-3:tap*4,:)));
	set(gca,'XTick',[]);
	set(gca,'YTick',[]);
	nb_cols=256;
      newmap= jet(nb_cols);
      % generate smooth transition of jet() from "black to white" brightness
      for (k=1:nb_cols)
        newmap(k,1:3) = (newmap(k,1:3)*(k-1) + [nb_cols-k, nb_cols-k, nb_cols-k])/(nb_cols-1);
      end
      jet_light_inue=newmap;
      colormap( jet_light_inue );
	caxis([0 1]);
	colorbar
else
	imagesc(abs(R(tap*16-15:tap*16, tap*16-15:tap*16,:)));
	set(gca,'XTick',[]);
	set(gca,'YTick',[]);
	nb_cols=256;
      newmap= jet(nb_cols);
      % generate smooth transition of jet() from "black to white" brightness
      for (k=1:nb_cols)
        newmap(k,1:3) = (newmap(k,1:3)*(k-1) + [nb_cols-k, nb_cols-k, nb_cols-k])/(nb_cols-1);
      end
      jet_light_inue=newmap;
      colormap( jet_light_inue );
	caxis([0 1]);
	colorbar
endif
title(["channel correlation matrix"]);

% Create an image file 
print(filename, "-dpng",output_format); 
endfunction
