function y = minmax3d(u, S)
	%% sliding min-max normalisation filter 
	% y = minmax3d(u, size)
	% implements a sliding min-max normalisation filter for a 3D signal u(i, j, k)
	% 
	% y(i,j,k) = (u(i,j,k) - min(u))/(max(u) - min(u))
	%
	% the minimum/maximum value of u is taken locally around i, j, k
	% size determines how big this local region is
	% the algorithm will search a region specified by:
	%    [i j k]-size/2 <= [i, j, k] <= [i, j, k]+size/2, 
		
	assert(ndims(u) == 3);
	assert(numel(S) == 3);
	
	y			= zeros(size(u), class(u));
	Ni			= size(u, 1);
	Nj			= size(u, 2);
	Nk			= size(u, 3);
	
	ivec		= -floor(S(1)/2):floor(S(1)/2);
	jvec		= -floor(S(2)/2):floor(S(2)/2);
	kvec		= -floor(S(3)/2):floor(S(3)/2);
	
	for i = 1:Ni
		is		= i + ivec;
		is		= is(is >= 1 & is <= Ni);
		uinear= u(is,:,:);

		for j = 1:Nj
			js			= j + jvec;
			js			= js(js >= 1 & js <= Nj);
			uijnear	= uinear(:,js,:);
			
			for k = 1:Nk
				ks = k + kvec;
				ks = ks(ks >= 1 & ks <= Nk);
				
				uijknear = uijnear(:,:,ks);
				minu		= min(uijknear(:));
				maxu		= max(uijknear(:));
				y(i,j,k) = (u(i,j,k)-minu)/max(maxu-minu,1);
			end
		end
	end
end
