1 function [HDR]=
swrite(HDR,data)
2 % SWRITE writes signal data.
4 % Appends data to an Signal File
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.
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.
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.
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:
27 fprintf(HDR.FILE.stderr,
'Error SWRITE can not be applied, File %s is not opened in WRITE mode\n',HDR.FileName);
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');
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);
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
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));
56 elseif (HDR.SPR == 1),
57 D = data(:, HDR.AS.SPR > 0)';
60 % fill missing data with NaN
61 tmp = rem(size(data,1),HDR.SPR);
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))];
66 NRec = size(data,1)/HDR.SPR;
67 D = repmat(NaN,sum(HDR.AS.SPR),NRec);
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);
74 GDFTYP = HDR.GDFTYP(1);
75 if ~exist('OCTAVE_VERSION','builtin')
85 if any(GDFTYP==([255,511]+4)),
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');
95 elseif any(GDFTYP==([255,511]+12)),
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');
106 elseif any(GDFTYP==([255,511]+24)),
108 %% BDF,
signed 24 bit
110 D(s) = D(s) + (2^24);
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');
118 error('SWRITE: writing of data type %s(%i) not supported.\n',datatypes,GDFTYP)
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);
131 HDR.FILE.POS = HDR.FILE.POS + count/HDR.AS.spb;
133 elseif strcmp(HDR.TYPE,'BKR'),
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');
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.
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);
148 data = data*(HDR.DigMax/HDR.PhysMax);
151 data((data>2^15-1) | (data<-2^15)) = -2^15;
152 count = fwrite(HDR.FILE.FID,data','
short');
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);
158 HDR.SPR=size(data,1);
160 if HDR.FLAG.TRIGGERED > 1,
161 HDR.FILE.POS = HDR.FILE.POS + size(data,1)/HDR.SPR;
163 HDR.FILE.POS = 1; % untriggered data
165 %HDR.AS.endpos = HDR.AS.endpos + size(data,1);
168 elseif strcmp(HDR.TYPE,'CFWB')
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');
177 data = data*diag(1./HDR.Cal)-HDR.Off(:,ones(1,size(data,1)))';
180 data(data>2^15-1)= 2^15-1;
181 data(data<-2^15) = -2^15;
182 count = fwrite(HDR.FILE.FID,data','
short');
184 count = fwrite(HDR.FILE.FID,data',
gdfdatatype(HDR.GDFTYP));
187 HDR.NRec=size(data,1);
189 HDR.FILE.POS = HDR.FILE.POS + size(data,1);
190 %HDR.AS.endpos = HDR.AS.endpos + size(data,1);
193 elseif strcmp(HDR.TYPE,'AIF') || strcmp(HDR.TYPE,'SND') || strcmp(HDR.TYPE,'WAV'),
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');
200 if strcmp(HDR.TYPE,'SND')
201 if (HDR.FILE.TYPE==1),
204 elseif strcmp(HDR.TYPE,'WAV'),
206 data = round((data + HDR.Off) / HDR.Cal-.5);
208 elseif strcmp(HDR.TYPE,'AIF'),
210 data = data * 2^(HDR.Bits-1);
214 count = fwrite(HDR.FILE.FID,data',
gdfdatatype(HDR.GDFTYP));
220 elseif strcmp(HDR.TYPE,'MIT')
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');
228 data = data - HDR.Off(ones(size(data,1),1),:);
229 data = data * diag(1./HDR.Cal);
231 if all(HDR.GDFTYP==3),
233 data(data>2^15-1)= 2^15-1;
234 data(data<-2^15) = -2^15;
235 count = fwrite(HDR.FILE.FID,data','
short');
237 fprintf(2,'ERROR SWRITE (MIT): datatype %i not supported\n',HDR.GDFTYP(1));
240 HDR.NRec=size(data,1);
242 HDR.FILE.POS = HDR.FILE.POS + size(data,1);
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);
249 HDR.NS = size(data,2);
252 if (HDR.FILE.POS~=HDR.NRec*HDR.SPR)
253 HDR.SPR = size(data,1);
260 fprintf(2,'Error SWRITE: file type %s not supported \n',HDR.TYPE);