-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrex_analog.m
91 lines (69 loc) · 2.26 KB
/
rex_analog.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
function [aseq, aecode, t, u, c, d] = rex_analog(name, offset)
% % % function [aseq, aecode, t, u, c, d] = rex_analog(name, offset)
% % % returns the analog stream from the afile indexed by 'offset'
% % % from the efile
% % % note that name can be a file pointer
% % % returns sequence number, ecode, time of trace, user code, cont code, data
% % % by James R. Cavanaugh 2001
SPECIALNUM = 1210832817; % magic number for testing integrity
persistent s2l;
persistent sa2la;
if ~isstr(name) % name is a file pointer
afp = name;
opened_it = 0;
else % name is a string
fname = [rawdatadir, name, 'A'];
% see if we understand how to unload the data
comptype = lower(computer);
if strcmp(comptype(1:3),'mac') | strcmp(comptype(1:2),'pc')
byteorder = 'ieee-le';
% short to long and short array (Nx2) to long array (Nx1)
if isempty(sa2la)
s2l = inline('double(uint8(s(1)))+256*double(uint8(s(2)))','s');
sa2la = inline('double(uint8(s(:,1)))+256.*double(uint8(s(:,2)))','s');
end;
else
errordlg('Not sure how to decode a file on this computer','Unknown host');
error('check byteorder in rex_analog()');
end;
% open the file
afp = fopen(fname, 'r', byteorder);
if afp == -1
data = [];
errordlg(['Could not open files for ', name],'Open error');
return;
end;
opened_it = 1;
end;
% shoot right to the specified offset
fseek(afp, offset, 'bof');
% get the magic number
[magicnum, nread] = fread(afp, 1, 'long');
if magicnum ~= SPECIALNUM
disp([dec2hex(magicnum),', ',dec2bin(magicnum)]);
disp('should be');
disp([dec2hex(SPECIALNUM),', ',dec2bin(SPECIALNUM)]);
disp(['at offset ', num2str(offset)]);
error('magic number wrong');
end;
% sequence number
[aseq, nread] = fread(afp, 1, 'short');
% event code
[aecode, nread] = fread(afp, 1, 'short');
% time
[atime, nread] = fread(afp, 1, 'long');
% user assigned
[auser, nread] = fread(afp, 1, 'long');
% continuation flag
[acont, nread] = fread(afp, 1, 'short');
% data record length
[adatalen, nread] = fread(afp, 1, 'short');
% variable length data record
[adata, nread] = fread(afp, adatalen, 'uchar');
if opened_it
fclose(afp);
end;
t = atime;
u = auser;
c = acont;
d = adata;