//******************************************************//
//      TFC: Integración redes telemáticas		//
//							//
//	Pedro Sánchez Torres				//       
//							//
//	Simulador de la capcacidad de redes 		//
//	WLAN 802.11e transportando VoIP			//
//							//
//							//
//	Junio 2012					//
//******************************************************//


function principal()
	
	//**********************************************//
 	//					        //
	//	VARIABLES GLOBALES 			// 
	//					        //
	//**********************************************//

	global margin_x;
	global margin_y;
	
	global padding_x;
	global padding_y;
	
	global frame_w;
	global frame_h;
	
	global plot_w;
	global plot_h;	
		
	frame_w            = 300;     // Frame width
	frame_h            = 550;     // Frame height
	
	plot_w             = 600;     // Plot width
	plot_h             = frame_h; // Plot height
	
	margin_x           = 15;      // Horizontal margin between each elements
	margin_y           = 15;      // Vertical margin between each elements
	
	padding_x          = 10;      // Horizontal padding between each elements
	padding_y          = 10;      // Vertical padding between each elements
	
	button_w           = 100;     // Button width
	button_h           = 30;      // Button height
	
	defaultfont        = "arial"; // Default Font
	subframe_font_size = 12;      // Title font size (Rotation angle, Colormap, ...)
	text_font_size     = 11;      // Text font size

	global ('my_slider_tasa', 'cfpef', 'sifs', 'bfs', 'plh', 'bi', 'pho', 'text0', 'my_plot_axes');	
	
	//**********************************************//
 	//					        //
	//	CREACION DEL SIMULADOR. MEDIDAS	Y	//
	//	CONFIGURACION				// 
	//					        //
	//**********************************************//
	
	axes_w       = 3*margin_x + frame_w + plot_w; // axes width
	axes_h       = 2*margin_y + frame_h;          // axes height (100 => toolbar height)
	
	simu_80211e = scf(100001);
	
	// Remove Scilab graphics menus & toolbar
	
	delmenu(simu_80211e.figure_id, gettext("&File"));
	delmenu(simu_80211e.figure_id, gettext("&Tools"));
	delmenu(simu_80211e.figure_id, gettext("&Edit"));
	delmenu(simu_80211e.figure_id, gettext("&?"));
	toolbar(simu_80211e.figure_id, "off");
	
	simu_80211e.background      = -2;
	simu_80211e.color_map       = jetcolormap(128);
	simu_80211e.figure_position = [0 0];
	simu_80211e.figure_name     = gettext("Simulador VoIP 802.11e");
	
	// New menu
	
	h = uimenu( "parent", simu_80211e,       ..
				"label" , gettext("File"));
	
	uimenu( "parent"    , h,                 ..
			"label"     , gettext("Close"),  ..
			"callback"  , "simu_80211e=get_figure_handle(100001);delete(simu_80211e);", ..
			"tag"       , "close_menu");
	
	sleep(500);
	simu_80211e.axes_size       = [axes_w axes_h];
	
	//**********************************************//
 	//					        //
	//	FRAME PRINCPAL. PANEL DE CONTROL	// 
	//					        //
	//**********************************************//
	
	my_frame = uicontrol( ...
		"parent"              , simu_80211e,...
		"relief"              , "groove",...
		"style"               , "frame",...
		"units"               , "pixels",...
		"position"            , [ margin_x margin_y frame_w frame_h],...
		"horizontalalignment" , "center", ...
		"background"          , [1 1 1], ...
		"tag"                 , "frame_control" ...
	);
	
	// Frame title
	my_frame_title = uicontrol( ...
		"parent"              , simu_80211e,...
		"style"               , "text",...
		"string"              , "Panel de Control",...
		"units"               , "pixels",...
		"position"            , [ 30+margin_x margin_y+frame_h-10 frame_w-60 20 ],...
		"fontname"            , defaultfont,...
		"fontunits"           , "points",...
		"fontsize"            , 16,...
		"horizontalalignment" , "center", ...
		"background"          , [1 1 1], ...
		"tag"                 , "title_frame_control" ...
	);
	
	
	//*************************************************//
 	//					           //
	//	FRAME TASA DE TRANSFERENCIA	           // 
	//					           //
	//*************************************************//
	
	// Unidades:
	
	my_ra_frame_w     = frame_w-2*padding_x
	my_ra_frame_h     = 140;
	my_ra_frame_pos_x = padding_x+margin_x;
	my_ra_frame_pos_y = (margin_y+frame_h) - 4*padding_y - my_ra_frame_h;
	
	my_slider_width      = my_ra_frame_w - 20;
	my_slider_height     = 20;
	my_slider_position_x = my_ra_frame_pos_x + 10;
	my_slider_position_y = my_ra_frame_pos_y + 10;
	
	// Frame Tasa de transferencia: medidas	

	my_ra_frame = uicontrol( ...
		"parent"              , simu_80211e,...
		"relief"              , "groove",...
		"style"               , "frame",...
		"units"               , "pixels",...
		"position"            , [ my_ra_frame_pos_x my_ra_frame_pos_y my_ra_frame_w my_ra_frame_h],...
		"horizontalalignment" , "center", ...
		"background"          , [1 1 1], ...
		"tag"                 , "frame_control" ...
	);
	
	// Frame Tasa de transferencia: titulo y medidas
	
	my_ra_frame_title = uicontrol( ...
		"parent"              , simu_80211e,...
		"style"               , "text",...
		"string"              , "Variación VoIP",...
		"units"               , "pixels",...
		"position"            , [ 80+my_ra_frame_pos_x my_ra_frame_pos_y+my_ra_frame_h-9 my_ra_frame_w-180 18 ],...
		"fontname"            , defaultfont,...
		"fontunits"           , "points",...
		"fontsize"            , subframe_font_size,...
		"horizontalalignment" , "center", ...
		"background"          , [1 1 1], ...
		"tag"                 , "title_frame_control" ...
	);
	
	// Frame Tasa de transferencia: Deslizador, medidas:

	my_slider_tasa = uicontrol( ...
		"parent"              , simu_80211e,...
		"style"               , "slider",...
		"Min"                 , 1,...
		"Max"                 , 54,...
		"Value"               , 11,...
		"SliderStep"          , [1 30],...
		"units"               , "pixels",...
		"position"            , [ my_slider_position_x my_slider_position_y+80 my_slider_width my_slider_height ],...
		"tag"                 , "my_slider_tasa", ...
		"callback"            , "actualizar_tasa" ...
	);

	// Frame Tasa de transferencia: Texto debajo del deslizador:

	text0 = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "text",...
		"position"           , [my_slider_position_x my_slider_position_y+55 my_slider_position_x+20 my_slider_height],...
		"horizontalalignment", "left",...
		"string"             , "11", ...
		"tag"                , "aaaa");

	textMbps = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "text",...
		"position"           , [my_slider_position_x+35 my_slider_position_y+55 my_slider_position_x+70 my_slider_height],...
		"horizontalalignment", "left",...
		"string"             , "Mbps", ...
		"tag"                , "aaaa");

	// Frame Tasa de transferencia: Radiobutton supertrama

	global ('supertrama_radio','cp_radio', 'maximoVoip_radio', 'retraso_radio', 'otro_radio');
	
	supertrama_radio = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "radiobutton",...
		"string"             , "Super trama (SF)",...
		"position"           , [ my_slider_position_x+10 my_slider_position_y+25 my_slider_position_x+70 my_slider_height],...
		"horizontalalignment", "left",...
		"fontname"           , defaultfont,...
		"fontunits"          , "points",...
		"fontsize"           , text_font_size,...
		"min"                , 0, ...
		"max"                , 1, ...
		"value"              , 1, ...
		"background"          , [1 1 1], ...
		"callback"           , "update_radiobuttons1",...
		"tag"                , "supertrama_radio");
	
	// Frame Tasa de transferencia: Radiobutton Periodo de contención
	
	cp_radio = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "radiobutton",...
		"string"             , "Periodo de Contención (CP)",...
		"position"           , [ my_slider_position_x+10 my_slider_position_y+5 my_slider_position_x+170 my_slider_height],...
		"horizontalalignment", "left",...
		"fontname"           , defaultfont,...
		"fontunits"          , "points",...
		"fontsize"           , text_font_size,...
		"min"                , 0, ...
		"max"                , 1, ...
		"value"              , 0, ...
		"background"          , [1 1 1], ...
		"callback"           , "update_radiobuttons2",...
		"tag"                , "cp_radio");
	
	//*************************************************//
 	//					           //
	//	FRAME FUNCION			           // 
	//					           //
	//*************************************************//
 
	// Unidades:

	my_cmap_frame_w     = frame_w-2*padding_x
	my_cmap_frame_h     = 80;
	my_cmap_frame_pos_x = padding_x+margin_x;
	my_cmap_frame_pos_y = my_ra_frame_pos_y - 2*padding_y - my_cmap_frame_h;
	
	// Frame función: medidas
	
	my_funcion_frame = uicontrol( ...
		"parent"              , simu_80211e,...
		"relief"              , "groove",...
		"style"               , "frame",...
		"units"               , "pixels",...
		"position"            , [ my_cmap_frame_pos_x my_cmap_frame_pos_y my_cmap_frame_w my_cmap_frame_h],...
		"horizontalalignment" , "center", ...
		"background"          , [1 1 1], ...
		"tag"                 , "frame_control" ...
	);
	
	// Frame función:  texto y medidas del título
	
	my_funcion_frame_title = uicontrol( ...
		"parent"              , simu_80211e,...
		"style"               , "text",...
		"string"              , "Totales",...
		"units"               , "pixels",...
		"position"            , [ 70+my_cmap_frame_pos_x my_cmap_frame_pos_y+my_cmap_frame_h-9 my_cmap_frame_w-160 18 ],...
		"fontname"            , defaultfont,...
		"fontunits"           , "points",...
		"fontsize"            , subframe_font_size,...
		"horizontalalignment" , "center", ...
		"background"          , [1 1 1], ...
		"tag"                 , "title_frame_control" ...
	);
		
	// Radiobutton Funcion :  maximoVoip
	
	maximoVoip_radio = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "radiobutton",...
		"string"             , "Tasa de Transferencia",...
		"position"           , [ my_cmap_frame_pos_x+20 my_cmap_frame_pos_y+my_cmap_frame_h-30 my_cmap_frame_w-25 20],...
		"horizontalalignment", "left",...
		"fontname"           , defaultfont,...
		"fontsize"           , text_font_size,...
		"fontunits"          , "points",...
		"min"                , 0, ...
		"max"                , 1, ...
		"value"              , 0, ...
		"background"          , [1 1 1], ...
		"callback"           , "update_radiobuttons3",...
		"tag"                , "maximoVoip_radio");
		
	// Radiobutton Funcion : Retraso
	
	retraso_radio = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "radiobutton",...
		"string"             , "Retraso",...
		"position"           , [ my_cmap_frame_pos_x+20 my_cmap_frame_pos_y+my_cmap_frame_h-50 my_cmap_frame_w-25 20],...
		"horizontalalignment", "left",...
		"fontname"           , defaultfont,...
		"fontsize"           , text_font_size,...
		"fontunits"          , "points",...
		"min"                , 0, ...
		"max"                , 1, ...
		"value"              , 0, ...
		"background"          , [1 1 1], ...
		"callback"           , "update_radiobuttons4",...
		"tag"                , "retraso_radio");

	// Radiobutton Funcion : Otro

	otro_radio = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "radiobutton",...
		"string"             , "MOS",...
		"position"           , [ my_cmap_frame_pos_x+20 my_cmap_frame_pos_y+my_cmap_frame_h-70 my_cmap_frame_w-25 20],...
		"horizontalalignment", "left",...
		"fontname"           , defaultfont,...
		"fontsize"           , text_font_size,...
		"fontunits"          , "points",...
		"min"                , 0, ...
		"max"                , 1, ...
		"value"              , 0, ...
		"background"          , [1 1 1], ...
		"callback"           , "update_radiobuttons5",...
		"tag"                , "otro_radio");
				
	//*************************************************//
 	//					           //
	//	FRAME PARAMETROS			   // 
	//					           //
	//*************************************************//
	
	// Unidades:

	my_param_frame_w     = frame_w-2*padding_x
	my_param_frame_h     = 210;
	my_param_frame_pos_x = padding_x+margin_x;
	my_param_frame_pos_y = my_cmap_frame_pos_y - 2*padding_y - my_param_frame_h;
	
	// Frame parametros: medidas
	
	my_param_frame = uicontrol( ...
		"parent"              , simu_80211e,...
		"relief"              , "groove",...
		"style"               , "frame",...
		"units"               , "pixels",...
		"position"            , [ my_param_frame_pos_x my_param_frame_pos_y my_param_frame_w my_param_frame_h],...
		"horizontalalignment" , "center", ...
		"background"          , [1 1 1], ...
		"tag"                 , "frame_param" ...
	);
	
	// Frame parametros: texto y medidas
	
	my_param_frame_title = uicontrol( ...
		"parent"              , simu_80211e,...
		"style"               , "text",...
		"string"              , "Parámetros",...
		"units"               , "pixels",...
		"position"            , [ 100+my_param_frame_pos_x my_param_frame_pos_y+my_param_frame_h-9 my_param_frame_w-200 18 ],...
		"fontname"            , defaultfont,...
		"fontunits"           , "points",...
		"fontsize"            , subframe_font_size,...
		"horizontalalignment" , "center", ...
		"background"          , [1 1 1], ...
		"tag"                 , "title_frame_param" ...
	);
	
	// Definicion de los label de parametros
	
	text1 = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "text",...
		"position"           , [ my_param_frame_pos_x+10 my_param_frame_pos_y+my_param_frame_h-50 my_param_frame_w-110 25],...
		"horizontalalignment", "left",...
		"string"             , "Beacon Frame Size (bits)", ...
		"tag"                , "aaaa");
	
	bfs = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "edit",...
		"position"           , [ my_param_frame_pos_x+190 my_param_frame_pos_y+my_param_frame_h-50 my_param_frame_w-200 25],...
		"horizontalalignment", "left",...
		"string"             , "320", ...
		"tag"                , "bfs");

	text2 = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "text",...
		"position"           , [ my_param_frame_pos_x+10 my_param_frame_pos_y+my_param_frame_h-80 my_param_frame_w-110 25],...
		"horizontalalignment", "left",...
		"string"             , "Protocol header overhead (bits)", ...
		"tag"                , "aaaa");
	
	pho = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "edit",...
		"position"           , [ my_param_frame_pos_x+190 my_param_frame_pos_y+my_param_frame_h-80 my_param_frame_w-200 25],...
		"horizontalalignment", "left",...
		"string"             , "456", ...
		"tag"                , "pho");

	text3 = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "text",...
		"position"           , [ my_param_frame_pos_x+10 my_param_frame_pos_y+my_param_frame_h-110 my_param_frame_w-110 25],...
		"horizontalalignment", "left",...
		"string"             , "Physical layer header (bits)", ...
		"tag"                , "aaaa");
	
	plh = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "edit",...
		"position"           , [ my_param_frame_pos_x+190 my_param_frame_pos_y+my_param_frame_h-110 my_param_frame_w-200 25],...
		"horizontalalignment", "left",...
		"string"             , "192", ...
		"tag"                , "plh");

	text4 = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "text",...
		"position"           , [ my_param_frame_pos_x+10 my_param_frame_pos_y+my_param_frame_h-140 my_param_frame_w-110 25],...
		"horizontalalignment", "left",...
		"string"             , "Beacon interval (s)", ...
		"tag"                , "aaaa");
	
	bi = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "edit",...
		"position"           , [ my_param_frame_pos_x+190 my_param_frame_pos_y+my_param_frame_h-140 my_param_frame_w-200 25],...
		"horizontalalignment", "left",...
		"string"             , "0.01", ...
		"tag"                , "bi");

	text5 = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "text",...
		"position"           , [ my_param_frame_pos_x+10 my_param_frame_pos_y+my_param_frame_h-170 my_param_frame_w-110 25],...
		"horizontalalignment", "left",...
		"string"             , "SIFS time interval (s)", ...
		"tag"                , "aaaa");
	
	sifs = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "edit",...
		"position"           , [ my_param_frame_pos_x+190 my_param_frame_pos_y+my_param_frame_h-170 my_param_frame_w-200 25],...
		"horizontalalignment", "left",...
		"string"             , "0.000028", ...
		"tag"                , "sifs");

	text6 = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "text",...
		"position"           , [ my_param_frame_pos_x+10 my_param_frame_pos_y+my_param_frame_h-200 my_param_frame_w-110 25],...
		"horizontalalignment", "left",...
		"string"             , "CFP end frame (bits)", ...
		"tag"                , "aaaa");
	
	cfpef = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "edit",...
		"position"           , [ my_param_frame_pos_x+190 my_param_frame_pos_y+my_param_frame_h-200 my_param_frame_w-200 25],...
		"horizontalalignment", "left",...
		"string"             , "192", ...
		"tag"                , "cfpef");

	// Definicion botón actualizar:

	boton_actualizar = uicontrol( ...
		"parent"             , simu_80211e,...
		"style"              , "pushbutton",...
		"position"           , [ my_param_frame_pos_x+100 my_param_frame_pos_y+my_param_frame_h-240 my_param_frame_w-220 20],...
		"horizontalalignment", "center",...
		"fontname"           , defaultfont,...
		"fontunits"          , "points",...
		"fontsize"           , text_font_size,...
		"string"             , "Actualizar", ...
		"callback"           , "radiobttn_activo",...
		"tag"                , "actualizar");

	// Inicialización de la gráfica:

	update_grafica(1);

endfunction

//******************************************************//
//							//
//	Control de los radiobutton.			//       
//							//
//******************************************************//

function update_radiobuttons1()

	global ('supertrama_radio','cp_radio', 'maximoVoip_radio', 'retraso_radio', 'otro_radio');
	set(supertrama_radio,"Value", 1);
	set(cp_radio,"Value", 0);
	set(maximoVoip_radio,"Value", 0);
	set(retraso_radio,"Value", 0);
	set(otro_radio,"Value", 0);

endfunction

function update_radiobuttons2()

	global ('supertrama_radio','cp_radio', 'maximoVoip_radio', 'retraso_radio', 'otro_radio');
	set(supertrama_radio,"Value", 0);
	set(cp_radio,"Value", 1);
	set(maximoVoip_radio,"Value", 0);
	set(retraso_radio,"Value", 0);
	set(otro_radio,"Value", 0);

endfunction

function update_radiobuttons3()

	global ('supertrama_radio','cp_radio', 'maximoVoip_radio', 'retraso_radio', 'otro_radio');
	set(supertrama_radio,"Value", 0);
	set(cp_radio,"Value", 0);
	set(maximoVoip_radio,"Value", 1);
	set(retraso_radio,"Value", 0);
	set(otro_radio,"Value", 0);

endfunction

function update_radiobuttons4()

	global ('supertrama_radio','cp_radio', 'maximoVoip_radio', 'retraso_radio', 'otro_radio');
	set(supertrama_radio,"Value", 0);
	set(cp_radio,"Value", 0);
	set(maximoVoip_radio,"Value", 0);
	set(retraso_radio,"Value", 1);
	set(otro_radio,"Value", 0);

endfunction

function update_radiobuttons5()

	global ('supertrama_radio','cp_radio', 'maximoVoip_radio', 'retraso_radio', 'otro_radio');
	set(supertrama_radio,"Value", 0);
	set(cp_radio,"Value", 0);
	set(maximoVoip_radio,"Value", 0);
	set(retraso_radio,"Value", 0);
	set(otro_radio,"Value", 1);

endfunction

//******************************************************//
//							//
//	Radiobutton activo				//       
//							//
//******************************************************//

function radiobttn_activo()

	global ('supertrama_radio','cp_radio', 'maximoVoip_radio', 'retraso_radio', 'otro_radio');

	if get(supertrama_radio,"Value") == 1 then
		update_grafica(1);

	elseif get(cp_radio,"Value") == 1 then
		update_grafica(2);

	elseif get(maximoVoip_radio,"Value") == 1 then
		update_grafica(3);

	elseif get(retraso_radio,"Value") == 1 then
		update_grafica(4);

	elseif get(otro_radio,"Value") == 1 then
		update_grafica(5);
  	end

endfunction

//******************************************************//
//							//
//	Actualizar Tasa					//       
//							//
//******************************************************//

function actualizar_tasa()

	global ('my_slider_tasa', 'text0');

	set(text0,"string", string(get(my_slider_tasa,"Value")));

endfunction

//******************************************************//
//							//
//	Update Gráfica					//       
//							//
//******************************************************//

function update_grafica(tipo)

	//Variables
	a =  tipo
	global ('my_slider_tasa', 'tipo');

	//Tasa de Bit de cada Codec
	CBR_711 = 64*1000
	CBR_729 = 8*1000
	CBR_723 = 6.3*1000

	//Retraso de paquetización de cada Codec en segundos
	Tvoice_711 = 0.02;
	Tvoice_729 = 0.02;
	Tvoice_723 = 0.03;

	R_seleccionada = get(my_slider_tasa,"Value")*1000000;

	if a == 1 then
		
		drawlater();  
		delete(gca());

		// Configuración gráfica
		my_plot_axes2 = gca();
		my_plot_axes2.axes_bounds        = [1/3,0,2/3,1];
		my_plot_axes2.grid=[2,2];
		my_plot_axes2.data_bounds        = [0.03,0;0.12,1200];
		my_plot_axes2.title.font_size    = 4;
		my_plot_axes2.x_label.text       = "SF (segundos)";
		my_plot_axes2.y_label.text       = "Numero de llamadas VoIP";
		my_plot_axes2.title.text         = "Nº de llamadas soportadas en modo EDCA/HCCA.";
		

		// Supertrama
		Tsf = (0.03:0.01:0.12);
		tcfp = Tsf ./ 3;
		tcp  = Tsf- tcfp;	
                                           	
		//Número de llamadas soportadas por cada codec		
		codec_711 = N_total(CBR_711, Tvoice_711, R_seleccionada, Tsf, tcfp, tcp);
		codec_729 = N_total(CBR_729, Tvoice_729, R_seleccionada, Tsf, tcfp, tcp);
		codec_723 = N_total(CBR_723, Tvoice_723, R_seleccionada, Tsf, tcfp, tcp);

		//Gráfica
		plot(Tsf,codec_711,'r',Tsf,codec_729,'b',Tsf,codec_723,'g');

		drawnow();
		hl=legend(['G.711';'G.723';'G.729'], 2);

	elseif a == 2

		drawlater();  
		delete(gca());

		// Configuración gráfica
		my_plot_axes2 = gca();
		my_plot_axes2.axes_bounds        = [1/3,0,2/3,1];
		my_plot_axes2.grid=[2,2];
		my_plot_axes2.data_bounds        = [0.03,0;0.12,1200];
		my_plot_axes2.title.font_size    = 4;
		my_plot_axes2.x_label.text       = "CP (segundos)";
		my_plot_axes2.y_label.text       = "Numero de llamadas VoIP";
		my_plot_axes2.title.text         = "Nº de llamadas soportadas en modo EDCA/HCCA.";
                 
		// Supertrama                          			
		T_sf2 = 0.12;
		t_cp2 = (0.03:0.01:0.12);
		t_cfp2 = T_sf2 - t_cp2;

		//Número de llamadas soportadas por cada codec	
		codec_711 = N_total(CBR_711, Tvoice_711, R_seleccionada, T_sf2, t_cp2, t_cfp2);
		codec_729 = N_total(CBR_729, Tvoice_729, R_seleccionada, T_sf2, t_cp2, t_cfp2);
		codec_723 = N_total(CBR_723, Tvoice_723, R_seleccionada, T_sf2, t_cp2, t_cfp2);

		//Gráfica
		plot(t_cp2, codec_711,'r',t_cp2, codec_729,'b',t_cp2, codec_723,'g')

		drawnow();
		hl=legend(['G.711';'G.723';'G.729'], 2);
		
	elseif a == 3 

		drawlater();  
		delete(gca());

		// Configuración gráfica
		my_plot_axes2 = gca();
		my_plot_axes2.axes_bounds       = [1/3,0,2/3,1];
		my_plot_axes2.grid		= [2,2];
		my_plot_axes2.data_bounds       = [2,0;54,1200];
		my_plot_axes2.title.font_size   = 4;
		my_plot_axes2.x_label.text      = "Tasa de transmisión (Mbps)";
		my_plot_axes2.y_label.text      = "Numero de llamadas VoIP";
		my_plot_axes2.title.text        = "Nº de llamadas soportadas en modo EDCA/HCCA.";
                                
		//Tasa de transferencia           			
		Tasa = (1:4:54);

		//Número de llamadas soportadas por cada codec	
		codec_711 = N_total(CBR_711, Tvoice_711, Tasa*10^6, 0.12, 0.04, 0.08);
		codec_729 = N_total(CBR_729, Tvoice_729, Tasa*10^6, 0.12, 0.03, 0.09);
		codec_723 = N_total(CBR_723, Tvoice_723, Tasa*10^6, 0.12, 0.03, 0.09);

		//Gráfica
		plot(Tasa, codec_711,'r',Tasa, codec_729,'b',Tasa, codec_723,'g');

		drawnow();
		hl=legend(['G.711';'G.723';'G.729'], 2);
		
	elseif a == 4 

		drawlater();  
		delete(gca());

		// Configuración gráfica
		my_plot_axes2 = gca();
		my_plot_axes2.axes_bounds        = [1/3,0,2/3,1];
		my_plot_axes2.grid=[2,2];
		my_plot_axes2.data_bounds        = [0,0;1200,1];
		my_plot_axes2.title.font_size    = 4;
		my_plot_axes2.x_label.text       = "Número de llamadas VoIP simultaneas";
		my_plot_axes2.y_label.text       = "Retraso (s)";
		my_plot_axes2.title.text         = "Retraso en función de la cantidad de llamadas VoIP.";
		
		// Supertrama  
		Tsf = (0.01:0.001:1);
		tcfp = Tsf ./ 3;
		tcp  = Tsf- tcfp;	
    
		//Número de llamadas soportadas por cada codec	
		codec_711 = N_total(CBR_711, Tvoice_711, 10*10^6, Tsf, tcfp, tcp);
		codec_729 = N_total(CBR_729, Tvoice_729, 10*10^6, Tsf, tcfp, tcp);
		codec_723 = N_total(CBR_723, Tvoice_723, 10*10^6, Tsf, tcfp, tcp);

		//Retraso total de cada códec
		retraso_711 = Retraso_VoIP(Tvoice_711, Tsf);
		retraso_729 = Retraso_VoIP(Tvoice_729, Tsf);
		retraso_723 = Retraso_VoIP(Tvoice_723, Tsf);

		//Gráfica
		plot(codec_711, retraso_711,'r', codec_729, retraso_729,'b', codec_723, retraso_723, 'g');

		drawnow();  
		hl=legend(['G.711';'G.723';'G.729'], 2);

	elseif a == 5 

		drawlater();  
		delete(gca());

		// Configuración gráfica
		my_plot_axes2 = gca();
		my_plot_axes2.axes_bounds        = [1/3,0,2/3,1];
		my_plot_axes2.grid=[2,2];
		my_plot_axes2.data_bounds        = [0,0;800,5];
		my_plot_axes2.title.font_size    = 4;
		my_plot_axes2.x_label.text       = "Número de llamadas VoIP simultaneas";
		my_plot_axes2.y_label.text       = "MOS";
		my_plot_axes2.title.text         = "MOS en función de la cantidad de llamadas VoIP.";
	
		// Degradación Intrínseca
		ie_711 = 0;
		ie_729 = 10;
		ie_723 = 15;

		//Definición de los vectores.
		//	      1	     2	    3	   4	   5	  6     7      8      9      10     11     12
		Tsf_R711 = [0.1773 1.1773 1.1773 2.1773 10.773 15.773 70.088 150.99 280.73 340.00 380.83 450.73];
		aux      = [0.1773 0.1773 0.1773 0.1773 0.1773 0.1773 0.1773 0.1773 0.1773 0.1773 0.1773 0.1773];
		uno      = [1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000];
		sesenta  = [60.000 60.000 60.000 60.000 60.000 60.000 60.000 60.000 60.000 60.000 60.000 60.000];
		cien     = [100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00];
	     	Tsf_R729 = (0.01:55:650);
		Tsf_R723 = (0.01:34:400);
		Tasa_711 = (1:0.8:10);
		Tasa_72x = (1:5:60);

		//Número de llamadas soportadas por cada codec	
		codec_711 = N_total(CBR_711, Tvoice_711, Tasa_711*10^6, 0.12, 0.04, 0.08);
		codec_729 = N_total(CBR_729, Tvoice_729, Tasa_72x*10^6, 0.12, 0.03, 0.09);
		codec_723 = N_total(CBR_723, Tvoice_723, Tasa_72x*10^6, 0.12, 0.03, 0.09);
		
		//Retraso total de cada códec
		retraso_711 = Retraso_VoIP(Tvoice_711, Tsf_R711);
		retraso_729 = Retraso_VoIP(Tvoice_729, Tsf_R729);
		retraso_723 = Retraso_VoIP(Tvoice_723, Tsf_R723);

		// Impairment de cada codec
		id_711 = 0.024 .* retraso_711 + 0.11 .* (retraso_711-aux).*evaluar(retraso_711-aux);
		id_729 = 0.024 .* retraso_729 + 0.11 .* (retraso_729-aux).*evaluar(retraso_729-aux);
		id_723 = 0.024 .* retraso_723 + 0.11 .* (retraso_723-aux).*evaluar(retraso_723-aux);

		// Factor R de cada códec
		R_711 = 94.2 - ie_711 - id_711; 
		R_729 = 94.2 - ie_729 - id_729; 
		R_723 = 94.2 - ie_723 - id_723;

		//Gráfica
		plot (codec_711, mos(R_711),'r', codec_723, mos(R_723),'b', codec_729, mos(R_729), 'g');

		drawnow();  
		hl=legend(['G.711';'G.723';'G.729'], 1);

	end

endfunction

//******************************************************//
//							//
//	Cálculo del tiempo overhead			//
//							//
//	Entrada: Tasa de transmisión, supertrama y	// 
//		 periodo de  CP				//
//							//
//	Salida: Tiempo consumido por el overhead	//       
//							//
//******************************************************//

function Toverhead=overhead(R, T_sf, T_cp)

	global ('cfpef', 'sifs', 'bfs', 'plh', 'bi');

	v_cfpef = get(cfpef,"string");
	v_sifs = get(sifs,"string");
	v_bfs =  get(bfs,"string");
	v_plh =  get(plh,"string");
	v_bi =  get(bi,"string");
	
	vble1 = evstr(v_sifs) + ((evstr(v_bfs)+evstr(v_plh)) ./ R);
	vble2 = (T_sf-T_cp) ./ evstr(v_bi);
	vble3 = (evstr(v_cfpef)+evstr(v_plh)) ./ R;
	
	Toverhead = vble1 .* vble2 + vble3
	
endfunction

//******************************************************//
//							//
//	Calculo del tiempo de polling durante		//
//	el periodo libre de contención			//
//							//
//	Entrada: Tasa de bits, tiempo de		//
//		 paquetización y CBR del códec y 	//
//		 periodo de CFP				//
//							//
//	Salida:	Periodo de sondeo durante CFP		//
//							//
//******************************************************//

function Tpollcfp=Tpoll_cfp(CBR, Tvoice, R, T_cfp)

	global ('sifs', 'plh', 'pho');

	v_sifs = get(sifs,"string");
	v_plh =  get(plh,"string");
	v_pho =  get(pho,"string");

	vble4 = CBR .* (Tvoice+T_cfp);
	vble5 = vble4 + evstr(v_pho) + evstr(v_plh);
	vble6 = (vble5 .* 2) ./ R;

	Tpollcfp = vble6 + 2.*evstr(v_sifs);

endfunction

//******************************************************//
//							//
//	Calculo del tiempo de polling durante		//
//	el periodo de contención (CAPs)			//
//							//
//	Entrada: Tasa de bits, tiempo de		//
//		 paquetización y CBR del códec y 	//
//		 periodo de CP				//
//							//
//	Salida:	Periodo de sondeo durante CP		//
//							//
//******************************************************//

function Tpollcp=Tpoll_cp(CBR, Tvoice, R, T_cp)

	global ('sifs', 'plh', 'pho');

	v_sifs = get(sifs,"string");
	v_plh =  get(plh,"string");
	v_pho =  get(pho,"string");

	vble7 = CBR .* (Tvoice+T_cp);
	vble8 = vble7 + evstr(v_pho) + evstr(v_plh);
	vble9 = (vble8.*2)./ R;
	
	Tpollcp = vble9 + 2.*evstr(v_sifs);

endfunction

//******************************************************//
//							//
//	Calculo del número de llamadas durante		//
//	el periodo libre de contención			//
//							//
//	Entrada: Tasa de bits, tiempo de		//
//		 paquetización y CBR del códec,  	//
//		 periodo de CP-CFP y supertrama		//
//							//
//	Salida:	llamadas VoIP durante CP		//
//							//
//******************************************************//
function Npollcfp=Npoll_cfp(CBR, Tvoice, R, T_sf, T_cfp, T_cp)

//	vble10 = T_cfp - overhead(R, T_sf, T_cp);
	vble10 = T_cfp - overhead(R, T_sf, T_cp) + 0.0725.*T_cp;
//	vble10 = T_cfp - overhead(R, T_sf, T_cp) - 0.2.*T_cp;

	Npollcfp = vble10 ./ Tpoll_cfp(CBR, Tvoice, R, T_cfp);


endfunction

//******************************************************//
//							//
//	Calculo del número de llamadas durante		//
//	el periodo de contención (CAPs)			//
//							//
//	Entrada: Tasa de bits, tiempo de		//
//		 paquetización y CBR del códec y 	//
//		 periodo de CP				//
//							//
//	Salida:	llamadas VoIP durante CP		//
//							//
//******************************************************//

function Npollcp=Npoll_cp(CBR, Tvoice, R, t_cp)

//	Npollcp = t_cp ./ Tpoll_cp(CBR, Tvoice, R, t_cp);
	Npollcp = (t_cp-0.2.*t_cp) ./ Tpoll_cp(CBR, Tvoice, R, t_cp);

endfunction


//******************************************************//
//							//
//	Calculo del número total de llamadas		//
//	soportadas en 802.11e				//
//							//
//	Entrada: Tasa de bits, tiempo de		//
//		 paquetización y CBR del códec,  	//
//		 periodo de CP-CFP y supertrama		//
//							//
//	Salida:	llamadas VoIP totales			//
//							//
//******************************************************//


function Ntotal=N_total(CBR, Tvoice, R, T_sf, T_cfp, T_cp)

	Ntotal = Npoll_cfp(CBR, Tvoice, R, T_sf, T_cfp, T_cp) + Npoll_cp(CBR, Tvoice, R, T_cp)

endfunction

//******************************************************//
//							//
//	Retraso de las llamadas VoIP			//
//							//
//	Entrada: Tiempo de paquetización y supertrama	//
//							//
//	Salida:	Retraso.				//
//							//
//******************************************************//

function RetrasoVoIP=Retraso_VoIP(Tvoice, T_sf)

	RetrasoVoIP = Tvoice + T_sf;

endfunction

//******************************************************//
//							//
//	Funcion Heavyside				//
//							//
//	H(x) = 0 if x < 0 and 1 if x ≥ 0		//
//							//
//******************************************************//

function evaluado = evaluar(var)

	for i=1 : length(var) 
		if var(i) < 0 then var(i) = 0;
		elseif var(i) >= 0 var(i) = 1;
		end
	end
	evaluado = var;

endfunction

//******************************************************//
//							//
//	Funcion MOS					//
//							//
//	Entrada: Factor R				//
//							//
//	Salida: MOS equivalente				//
//							//
//******************************************************//
function MOS = mos(R)

	if R < 0 then 
		MOS = 1
	elseif R < 100 then 
		MOS =  uno + 0.035 .* R + 7 .* R .* (R - sesenta) .* (cien - R) .* 10^-6;
	elseif R >= 100 then
		MOS = 4.5;
	end

endfunction

//******************************************************//
//							//
//	Main						//
//							//
//******************************************************//
principal();

