mode(0); 		// echo off
funcprot(0); 	// function warnings off
lines(0, 120); 	// show 1000 lines before asking
clc;			// clear command window


// Function definitions ========================================================

// Call after plot command to set mark color
function setMarkColor(c)
	ge = gce();
	pline = ge.children(1);
	pline.mark_background = c;
endfunction;

// Draw some text to the points
function printPositionIndex(V, fmt_text)
	for i = 1:size(V, 2)
		xstring(V(1, i), V(2, i), sprintf(fmt_text, i));
	end;
endfunction;

// Vectorized elementwise vector norm
function X = vecNorm(A, B)
	X = zeros(1, size(A, 2));
	for i = 1:size(A, 1) // dimensions
		X = X + (A(i, :) - B(i, :)).^2;
	end;
	X = sqrt(X);
endfunction;

//Linear interpolation (vectorized): f(t) = A + t * (B - A)
function X = linearInterpolation(A, B, h)
	X = A * ones(1, h) + (ones(p, 1) * linspace(0, 1, h)) .* ((B - A) * ones(1, h));
endfunction;

// Calculate signal strength according to formula from lecture
function X = signalStrength(d, mu, eta)
	h = size(d, 1);
	X = mu - eta * log(d) + rand(h, 1);
endfunction;

// Main routine ================================================================

// Use normal distribution N(0, 1)
rand("normal");

p = 2; // dimension
n = 3; // base station count
m = 7; // points count
steps = 40;

mu  = 30.0;
eta = 10.0;

// Base station positions M, cell phone points S
M = [-0.5, -0.6; 0.6, 0.15; -0.1, 0.9]';
S = [-1.0, 0.1; -0.3, 0.6; 0.2, 0.7; 0.8, 0.1; 0.2, -0.4; -0.5, 0.0; -0.7, 0.7]';


// Plot base stations
scf(0);
clf;
f = 1;
d_rect = [-f, -f, f, f];
plot2d(M(1, :), M(2, :), -5, frameflag = 3, rect = d_rect);
setMarkColor(13);
printPositionIndex(M, "  Base station %d");

// Plot cell phone path
plot2d(S(1, :), S(2, :), -9);
setMarkColor(5);
printPositionIndex(S, "  %d");
for i = 2:m
	plot2d([S(1, i - 1), S(1, i)], [S(2, i - 1), S(2, i)], 2);
end;
xtitle("Cell phone city (from above)","x (km)","y (km)");

// Loop over all segments of cell phone path (and over all base stations)
R = zeros((m - 1) * steps, n);
for i = 1:n
	for j = 2:m
		// Linear interpolation (vectorized), X is path of cell phone positions
		X = linearInterpolation(S(:, j-1), S(:, j), steps);
		
		// Distances D = || X - M || (vectorized)
		D = vecNorm(X, M(:, i) * ones(1, steps))';
		R(steps*(j-2)+1:steps*(j-1), i) = signalStrength(D, mu, eta);
		//printf("Set positions %d-%d, %d, set %d array\n", steps*(j-2)+1, steps*(j-1), i, size(R, 1));
	end;
end;

// Calculate total path length through city
x_line = []; 
total_path = 0;
for i = 2:m
	seg_length = norm(S(:, j-1) - S(:, j));
	x_part = total_path + linspace(0, seg_length, steps);
	x_line = [x_line, x_part];
	total_path = total_path + seg_length;
end;

// Plot signal strengths
scf(1);
clf;
for i = 1:n
	plot2d(x_line, R(:, i)', i+1);
	[max_val, max_ind] = maxi(R(:, i));
	xstring(x_line(max_ind), R(max_ind, i), sprintf("Base station %d", i));
end;
xtitle("Signal strength diagram","path through city (km)","signal strength (dB)");



	
