TEAP (Toolbox for Emotion Analysis using Physiological Signals) doc
openldr.m
Go to the documentation of this file.
1 function LDR = openldr(FN,PERMISSION,Mode,arg4,arg5,arg6)
2 % OPENLDR loads neuroscan LDR files
3 % LDR = OPENLDR(Filename [, PERMISSION [, Mode]]);
4 % LDR = OPENLDR(LDR [, PERMISSION [, Mode]]);
5 %
6 % LDR is a struct with the following fields
7 % LDR.FileName Name of LDR-file
8 % LDR.Label_Out Labels of output channels
9 % LDR.Label_In Labels of input channels
10 % LDR.RR re-referencing matrix
11 % LDR.datatype 'REREF_MATRIX' indicates this datatype
12 %
13 % PERMISSION 'r' reads LDR file
14 % 'w' writes LDR file
15 % 'r+w' reads and writes LDR file
16 % (useful in combination with RESCALE-Mode)
17 %
18 % Mode [optional] 'RESCALE' performs a rescaling of the weights
19 % sum of positive weights becomes +1
20 % sum of negative weights becomes -1
21 %
22 
23 % $Id: openldr.m 2473 2010-06-04 09:56:57Z schloegl $
24 % Copyright (C) 1997-2003,2008 by Alois Schloegl <a.schloegl@ieee.org>
25 % This is part of the BIOSIG-toolbox http://biosig.sf.net/
26 
27 % This program is free software; you can redistribute it and/or
28 % modify it under the terms of the GNU General Public License
29 % as published by the Free Software Foundation; either version 3
30 % of the License, or (at your option) any later version.
31 
32 
33 if nargin<2, PERMISSION=''; end;
34 if nargin<3, Mode=''; end;
35 
36 if ~isstruct(FN),
37  LDR.FileName = FN;
38  PERMISSION = [PERMISSION,'r'];
39 else
40  LDR = FN;
41  PERMISSION = [PERMISSION,'w'];
42 end;
43 
44 
45 if any(PERMISSION=='r');
46  % load LDR-file
47  fid = fopen(LDR.FileName);
48  if fid<0, fprintf(2,'File %s not found.\n',LDR.FileName); return; end;
49 
50  % reads line 1: size information
51  s = fgetl(fid);
52  if ~ischar(s), fprintf(2,'ERROR LOADLDR: file %s corrupted\n',FN); end;
53  sz = str2num(s);
54 
55  % reads line 2: output labels
56  s = fgetl(fid);
57  if ~ischar(s), fprintf(2,'ERROR LOADLDR: file %s corrupted\n',FN); end;
58  tmp = reshape(s,12,sz(2)+1)';
59  LDR.Label_Out = tmp(2:sz(2)+1,:);
60 
61  % read lines 3+: input labels and weights
62  for k = 1:sz(1),
63  s = fgetl(fid);
64  if ~ischar(s), fprintf(2,'ERROR LOADLDR: file %s corrupted\n',FN); end;
65  r = reshape(s,12,sz(2)+1)';
66  LDR.Label_In(k,1:12) = char(abs(s(1:12)));
67  LDR.RR(k,1:sz(2)) = str2num(r(2:size(r,1),:))';
68  end;
69 
70  fclose(fid);
71  LDR.datatype='REREF_MATRIX';
72 
73 end;
74 
75 
76 if strcmp(Mode,'RESCALE');
77  tmp = LDR.RR;
78  tmp(tmp<0) = 0;
79  w = sum(tmp);
80  RR = zeros(sz);
81  if ~all(w==0 | w==1)
82  ix = find(w);
83  RR(:,ix) = tmp(:,ix)./w(ones(sz(1),1),ix);
84  % RR(:,ix) = (tmp(:,ix)>0)./(ones(sz(1),1)*sum(tmp(:,ix)>0));
85  end;
86  tmp = LDR.RR;
87  tmp(tmp>0)=0;
88  w = sum(tmp);
89  if ~all(w==0 | w==-1)
90  ix = find(w);
91  RR(:,ix) = tmp(:,ix)./w(ones(sz(1),1),ix);
92  % RR(:,ix) = RR(:,ix)-(tmp(:,ix)<0)./(ones(sz(1),1)*sum(tmp(:,ix)<0));
93  end;
94  LDR.RR=RR;
95 end;
96 
97 if any(PERMISSION=='w');
98  tmp = isfield(LDR,'datatype') & strcmp(LDR.datatype,'REREF_MATRIX');
99  if ~tmp,
100  warning('LDR.datatype does not fit');
101  end;
102  sz = size(LDR.RR);
103  if sz(1)~=size(LDR.Label_In,1);
104  fprintf(2,'Size of Label_In does not fit RR\n');
105  return;
106  end;
107  if sz(2)~=size(LDR.Label_Out,1);
108  fprintf(2,'Size of Label_Out does not fit RR\n');
109  return;
110  end;
111 
112  % open LDR-file
113  fid = fopen(LDR.FileName,'w+');
114  if fid<0, fprintf(2,'Couldnot open file %s .\n',LDR.FileName); return; end;
115 
116  % write line 1: size information
117  fprintf(fid,'%i %i\n',sz);
118 
119  % condition label information
120  %LDR.Label_Out = char(LDR.Label_Out);
121 
122  nc = size(LDR.Label_Out,2);
123  if nc<12,
124  LDR.Label_Out = [LDR.Label_Out,abs(' ')*ones(sz(1),12-nc)];
125  elseif nc>12,
126  LDR.Label_Out = LDR.Label_Out(:,1:12);
127  end;
128  %LDR.Label_In = char(LDR.Label_In);
129  nc = size(LDR.Label_In,2);
130  if nc<12,
131  LDR.Label_In = [LDR.Label_In,abs(' ')*ones(sz(1),12-nc)];
132  elseif nc>12,
133  LDR.Label_In = LDR.Label_In(:,1:12);
134  end;
135 
136  % write line 2: out-labels
137  fwrite(fid,32+zeros(12,1),'uint8');
138  %fprintf(fid,'%c',abs(' ')*ones(12,1));
139  for k = 1:sz(2),
140  fwrite(fid,abs(LDR.Label_Out(k,1:12)),'uint8');
141  end;
142 
143  % write lines 3+: in-labels and weights
144  for k = 1:sz(1),
145  fprintf(fid,'\n');
146  fwrite(fid,abs(LDR.Label_In(k,1:12)),'uint8');
147  fprintf(fid,'%12.5f',LDR.RR(k,:));
148  end;
149  fclose(fid);
150 end;
openldr
function openldr(in FN, in PERMISSION, in Mode, in arg4, in arg5, in arg6)