TEAP (Toolbox for Emotion Analysis using Physiological Signals) doc
swrite.m
Go to the documentation of this file.
1 function [HDR]=swrite(HDR,data)
2 % SWRITE writes signal data.
3 % HDR = swrite(HDR,data)
4 % Appends data to an Signal File
5 
6 % This program is free software; you can redistribute it and/or
7 % modify it under the terms of the GNU General Public License
8 % as published by the Free Software Foundation; either version 3
9 % of the License, or (at your option) any later version.
10 %
11 % This program is distributed in the hope that it will be useful,
12 % but WITHOUT ANY WARRANTY; without even the implied warranty of
13 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 % GNU General Public License for more details.
15 %
16 % You should have received a copy of the GNU General Public License
17 % along with this program; if not, write to the Free Software
18 % Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 
20 
21 % $Id: swrite.m 3157 2012-11-26 09:05:55Z schloegl $
22 % Copyright (c) 1997-2005,2011 by Alois Schloegl <alois.schloegl@gmail.com>
23 % This file is part of the biosig project http://biosig.sf.net/
24 
25 
26 if HDR.FILE.OPEN < 2,
27  fprintf(HDR.FILE.stderr,'Error SWRITE can not be applied, File %s is not opened in WRITE mode\n',HDR.FileName);
28  return;
29 end;
30 
31 
32 if strcmp(HDR.TYPE,'EDF') || strcmp(HDR.TYPE,'GDF') || strcmp(HDR.TYPE,'BDF'),
33  if ~all(HDR.GDFTYP==HDR.GDFTYP(1))
34  fprintf(2,'Error SWRITE: different GDFTYPs not supported yet!\n');
35  return;
36  end;
37  if ~HDR.FLAG.UCAL,
38  data = data - repmat(HDR.PhysMin(:)',size(data,1),1);
39  data = data * sparse(1:HDR.NS,1:HDR.NS,(HDR.DigMax-HDR.DigMin)./(HDR.PhysMax-HDR.PhysMin)); % scale Phys->Dig
40  data = data + repmat(HDR.DigMin(:)',size(data,1),1);
41  end;
42 
43  if ~any(HDR.GDFTYP(1)==[16,17,18]),
44  data(data< HDR.THRESHOLD(1,1)) = HDR.THRESHOLD(1,1); %underflow
45  data(data> HDR.THRESHOLD(1,2)) = HDR.THRESHOLD(1,2); %overflow
46  data(isnan(data)) = HDR.THRESHOLD(1,3); % missing value
47  end
48 
49  if 0, HDR.SIE.RAW,
50  if sum(HDR.AS.SPR)~=size(data,1)
51  fprintf(2,'Warning SWRITE: datasize must fit to the Headerinfo %i %i %i\n',HDR.AS.spb,size(data));
52  fprintf(2,'Define the Headerinformation correctly.\n',HDR.AS.spb,size(data));
53  end;
54  D = data;
55 
56  elseif (HDR.SPR == 1),
57  D = data(:, HDR.AS.SPR > 0)';
58 
59  else
60  % fill missing data with NaN
61  tmp = rem(size(data,1),HDR.SPR);
62  if tmp,
63  fprintf(HDR.FILE.stderr,'Warning SWRITE: %i NaNs added to complete data block.\n',HDR.SPR-tmp);
64  data = [data;repmat(HDR.THRESHOLD(1,3),HDR.SPR-tmp,size(data,2))];
65  end;
66  NRec = size(data,1)/HDR.SPR;
67  D = repmat(NaN,sum(HDR.AS.SPR),NRec);
68  for k = 1:HDR.NS;
69  if HDR.AS.SPR(k)>0,
70  D(HDR.AS.bi(k)+1:HDR.AS.bi(k+1),:) = rs(reshape(data(:,k),HDR.SPR,NRec),HDR.SPR/HDR.AS.SPR(k),1);
71  end;
72  end;
73  end;
74  GDFTYP = HDR.GDFTYP(1);
75  if ~exist('OCTAVE_VERSION','builtin')
76  count = fwrite(HDR.FILE.FID,D,gdfdatatype(GDFTYP));
77  else
78  if GDFTYP<256,
79  count = fwrite(HDR.FILE.FID,D,gdfdatatype(GDFTYP));
80  else
81  [datatyp,limits,datatypes] = gdfdatatype(GDFTYP);
82  [nr,nc] = size(D);
83  bits = GDFTYP - 511;
84 
85  if any(GDFTYP==([255,511]+4)),
86  if GDFTYP<512,
87  %% signed 4 bit
88  s = D<0;
89  D(s) = D(s) + (2^4);
90  end,
91  X(2:2:2*nr,:) = floor(D/16);
92  X(1:2:2*nr,:) = mod(D,16);
93  count = fwrite(HDR.FILE.FID,X,'uint8');
94 
95  elseif any(GDFTYP==([255,511]+12)),
96  if GDFTYP<512,
97  %% signed 12 bit
98  s = D<0;
99  D(s) = D(s) + (2^12);
100  end,
101  X(3:3:1.5*nr,:) = floor(D(2:2:nr,:)/16);
102  X(1:3:1.5*nr,:) = mod(D([1:2:nr],:),256);
103  X(2:3:1.5*nr,:) = mod(floor(D(1:2:nr,:)/256),16)+16*mod(D(2:2:nr,:),16);
104  count = fwrite(HDR.FILE.FID,X,'uint8');
105 
106  elseif any(GDFTYP==([255,511]+24)),
107  if GDFTYP<512,
108  %% BDF, signed 24 bit
109  s = D<0;
110  D(s) = D(s) + (2^24);
111  end,
112  X(3:3:3*nr,:) = mod(floor(D*2^-16),256);
113  X(1:3:3*nr,:) = mod(D,256);
114  X(2:3:3*nr,:) = mod(floor(D/256),256);
115  count = fwrite(HDR.FILE.FID,X,'uint8');
116 
117  elseif 1,
118  error('SWRITE: writing of data type %s(%i) not supported.\n',datatypes,GDFTYP)
119 
120  else
121  ix0 = ceil([1:nr*bits]'/8);
122  ix1 = mod([1:nr*bits]',8);
123  ix2 = ceil([1:nr*bits]'/bits);
124  ix3 = mod([1:nr*bits]',bits);
125  for k = 1:nr*bits,
126 
127  end;
128  end;
129  end;
130  end;
131  HDR.FILE.POS = HDR.FILE.POS + count/HDR.AS.spb;
132 
133 elseif strcmp(HDR.TYPE,'BKR'),
134  count=0;
135  if HDR.NS~=size(data,2) && HDR.NS==size(data,1),
136  fprintf(2,'SWRITE: number of channels fits number of rows. Transposed data\n');
137  data = data';
138  end
139 
140  if any(HDR.NS==[size(data,2),0]), % check if HDR.NS = 0 (unknown channel number) or number_of_columns
141  if HDR.NS==0, HDR.NS = size(data,2); end; % if HDR.NS not set, set it.
142  if ~HDR.FLAG.UCAL,
143  if isnan(HDR.PhysMax)
144  HDR.PhysMax = max(abs(data(:)));
145  elseif HDR.PhysMax < max(abs(data(:))),
146  fprintf(2,'Warning SWRITE: Data Saturation. max(data)=%f is larger than HDR.PhysMax %f.\n',max(abs(data(:))),HDR.PhysMax);
147  end;
148  data = data*(HDR.DigMax/HDR.PhysMax);
149  end;
150  % Overflow detection
151  data((data>2^15-1) | (data<-2^15)) = -2^15;
152  count = fwrite(HDR.FILE.FID,data','short');
153  else
154  fprintf(2,'Error SWRITE: number of columns (%i) does not fit Header information (number of channels HDR.NS %i)',size(data,2),HDR.NS);
155  return;
156  end;
157  if HDR.SPR==0,
158  HDR.SPR=size(data,1);
159  end;
160  if HDR.FLAG.TRIGGERED > 1,
161  HDR.FILE.POS = HDR.FILE.POS + size(data,1)/HDR.SPR;
162  else
163  HDR.FILE.POS = 1; % untriggered data
164  end;
165  %HDR.AS.endpos = HDR.AS.endpos + size(data,1);
166 
167 
168 elseif strcmp(HDR.TYPE,'CFWB')
169  count=0;
170  if HDR.NS~=size(data,2) && HDR.NS==size(data,1),
171  fprintf(2,'SWRITE: number of channels fits number of rows. Transposed data\n');
172  data = data';
173  end
174 
175  if HDR.GDFTYP==3,
176  if ~HDR.FLAG.UCAL,
177  data = data*diag(1./HDR.Cal)-HDR.Off(:,ones(1,size(data,1)))';
178  end;
179  % Overflow detection
180  data(data>2^15-1)= 2^15-1;
181  data(data<-2^15) = -2^15;
182  count = fwrite(HDR.FILE.FID,data','short');
183  else
184  count = fwrite(HDR.FILE.FID,data',gdfdatatype(HDR.GDFTYP));
185  end;
186  if HDR.NRec==0,
187  HDR.NRec=size(data,1);
188  end;
189  HDR.FILE.POS = HDR.FILE.POS + size(data,1);
190  %HDR.AS.endpos = HDR.AS.endpos + size(data,1);
191 
192 
193 elseif strcmp(HDR.TYPE,'AIF') || strcmp(HDR.TYPE,'SND') || strcmp(HDR.TYPE,'WAV'),
194  count = 0;
195  if (HDR.NS ~= size(data,2)) && (HDR.NS==size(data,1)),
196  fprintf(2,'Warning SWRITE: number of channels fits number of rows. Transposed data\n');
197  data = data';
198  end
199 
200  if strcmp(HDR.TYPE,'SND')
201  if (HDR.FILE.TYPE==1),
202  data = lin2mu(data);
203  end;
204  elseif strcmp(HDR.TYPE,'WAV'),
205  if ~HDR.FLAG.UCAL,
206  data = round((data + HDR.Off) / HDR.Cal-.5);
207  end;
208  elseif strcmp(HDR.TYPE,'AIF'),
209  if ~HDR.FLAG.UCAL,
210  data = data * 2^(HDR.Bits-1);
211  end;
212  end;
213 
214  count = fwrite(HDR.FILE.FID,data',gdfdatatype(HDR.GDFTYP));
215  if HDR.NS==0,
216  HDR.NS=size(data,2);
217  end;
218 
219 
220 elseif strcmp(HDR.TYPE,'MIT')
221  count = 0;
222  if HDR.NS~=size(data,2) && HDR.NS==size(data,1),
223  fprintf(2,'SWRITE: number of channels do not fit number of columns but number of rows. Data transposed!?!\n');
224  data = data';
225  end
226 
227  if ~HDR.FLAG.UCAL,
228  data = data - HDR.Off(ones(size(data,1),1),:);
229  data = data * diag(1./HDR.Cal);
230  end;
231  if all(HDR.GDFTYP==3),
232  % Overflow detection
233  data(data>2^15-1)= 2^15-1;
234  data(data<-2^15) = -2^15;
235  count = fwrite(HDR.FILE.FID,data','short');
236  else
237  fprintf(2,'ERROR SWRITE (MIT): datatype %i not supported\n',HDR.GDFTYP(1));
238  end;
239  if HDR.NRec==0,
240  HDR.NRec=size(data,1);
241  end;
242  HDR.FILE.POS = HDR.FILE.POS + size(data,1);
243 
244 
245 elseif strcmp(HDR.TYPE,'ET-MEG')
246  count = fwrite(HDR.FILE.FID,data',gdfdatatype(HDR.GDFTYP));
247  HDR.FILE.POS = HDR.FILE.POS + size(data,1);
248  if ~(HDR.NS>0)
249  HDR.NS = size(data,2);
250  HDR.FILE.OPEN = 3;
251  end;
252  if (HDR.FILE.POS~=HDR.NRec*HDR.SPR)
253  HDR.SPR = size(data,1);
254  HDR.FILE.OPEN = 3;
255  end;
256 
257 
258 
259 else
260  fprintf(2,'Error SWRITE: file type %s not supported \n',HDR.TYPE);
261 
262 
263 end;
264 
gdfdatatype
function gdfdatatype(in GDFTYP)
swrite
function swrite(in HDR, in data)
rs
function rs(in y1, in T, in f2)