function [x,k,h,w] = dLoadData( filename )
% dLoadData - loads file with Dickstein data and converts it into matrix form
% 	[x,k,h,w] = loadDicksteinData( filename )
%	filename  - string with the name of the file
%	x	  - matrix (k*h) rows and w columns
%	k	  - number of time samples
%	h	  - hight of the one sample matrix, h=15
%	w	  - width of the one sample matrix, w=15
% The input file is simply array of numbers, one number per row.
% There should be k*(15*16+1) numbers, where k = number of samples

%
%The format of Dickstein ultrasound data is:
%
%header
%loop:   15x16 matrix with diagonal equal 0
%        1 element ??? (signal similar to other signals)
%	goto loop:
%matrix/elements are 2 byte integers
%
%The output of this program is a matrix with k*15 rows and 15 columns. 
%Each 15x15 submatrix starting at 1st, 16h, 31th ...(15*n+1)  row is raw data 
%with the diagonal elements equal to zero.
%

%cd d:\laza\dickst~1
if ( exist(filename) ~= 2), 
	disp(['File ' filename ' does not exist']);
	return;
end;
eval([ 'load -ascii ' filename ]);
DotPosition = find(filename == '.');
if (isempty(DotPosition)), DotPosition = length(filename)+1;   end;
SlashPosition = max([ find(filename=='\') find(filename=='/')]);;
if (isempty(SlashPosition)), SlashPosition = 0;  end;
varname = filename( SlashPosition+1 : DotPosition -1 );
xxx = eval( varname );
eval(['clear ' varname]);

h = 15; w = 16;
y = xxx((h*w+1):(h*w+1):length(xxx));	%misterious element
N = length(xxx) - rem( length(xxx), h*w+1);
k = N / (h*w+1);	M = k*(h*w);

x = zeros( k*h, h );	% just taking 15x15
for i=1:k,
	for j = 1:h,
		x( (i-1)*h + j, : ) = ...
			xxx((i-1)*(h*w+1) + (j-1)*w + (1:h))' ;
end;	end;

clear xxx
% save -ascii data.txt x

if (1==0),	%just comment
% The structure of the raw data file can be best seen by running following comands:
t = 1:length(xxx)-17;
I = find(xxx(t)==0 & xxx(t+17)==0 );
J = find(diff(I)==20);
disp(find(diff(J)~=14));
end; %if
return