forked from justingardner/gru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readext.m
71 lines (60 loc) · 1.82 KB
/
readext.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
% readext.m
%
% usage: readext.m()
% by: justin gardner/modified to read ext. file
% date: 05/10/03
% purpose: Read Ext. file generated by Labview data
%
function d = readext(filename,numchannels)
if (nargin == 1)
numchannels = -1;
elseif (nargin ~= 2)
disp('USAGE: readext filename');
return
end
% check which platform we are running (Unix, Intel Mac, Power Mac)
% MAC, MACI, PCWIN, GLNX86
[platform maxsize endian] = computer;
% set filename
d.filename = filename;
d.channels = [];
% open the file
fid = fopen(filename,'r');
if (fid == -1)
disp(sprintf('ERROR: Could not open %s',filename));
return
end
% read the data
%[raw, n] = fread(fid,inf,'unsigned short');
%[raw, n] = fread(fid,inf,'ushort');
if strcmp(endian, 'L')
[raw, n] = fread(fid,inf,'ushort','ieee-be');
else
[raw, n] = fread(fid,inf,'ushort');
end
% get the number of channels as the top four bits
topfourbits = bitshift(bitand(raw(1),hex2dec('F000')),-12);
if (topfourbits == 0),d.version = 1;,else,d.version = 2;,end
% if the user hasn't called for a specific number of channelsn
if (numchannels == -1)
% if there is nothing in the top four bits, then assume 5 channels
if (topfourbits == 0)
numchannels = 5;
else
numchannels = topfourbits;
end
else
if (numchannels ~= topfourbits)
disp(sprintf('UHOH: Number of channels in file is %i, not %i',topfourbits,numchannels));
end
end
d.numchannels = numchannels;
% reshape into channels array
d.channels = reshape(raw,numchannels, n/numchannels);
% remove top 4 bits of each short.
d.channels = bitand(uint16(d.channels),hex2dec('FFF'));
% for each value greater than 0x7FF replace with -0xFFF+value
d.channels = double(d.channels>hex2dec('7FF')).*-double(hex2dec('FFF'))+double(d.channels);
%scale by 5/0x7FF
d.channels = 5*d.channels/double(hex2dec('7ff'));
fclose(fid);