1 function [HDR] =
ssave(FILENAME,DATA,TYPE,Fs,gdftyp)
2 % SSAVE saves signal data in various data formats
4 % Currently are the following data formats supported:
5 % EDF, BDF, GDF, BKR, SND/AU, (WAV, AIF)
6 % and WSCORE event file
8 % HDR =
ssave(HDR,data);
9 % HDR =
ssave(FILENAME,data,TYPE,Fs);
11 % FILENAME name of file
12 % data signal data, each column is a channel
13 % TYPE determines dataformat
16 % see also: SSAVE, SOPEN, SWRITE, SCLOSE, doc/README
19 % $Id:
ssave.m 3061 2012-08-27 09:19:14Z schloegl $
20 % Copyright (C) 2003,2004,2007 by Alois Schloegl <a.schloegl@ieee.org>
21 % This file is part of the biosig project http:
23 % This library is free software; you can redistribute it and/or
24 % modify it under the terms of the GNU Library General Public
25 % License as published by the Free Software Foundation; either
26 % Version 2 of the License, or (at your option) any later version.
28 % This library is distributed in the hope that it will be useful,
29 % but WITHOUT ANY WARRANTY; without even the implied warranty of
30 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 % Library General Public License
for more details.
33 % You should have received a copy of the GNU Library General Public
34 % License along with
this library;
if not, write to the
35 % Free Software Foundation, Inc., 59 Temple Place - Suite 330,
36 % Boston, MA 02111-1307, USA.
40 if isstruct(FILENAME),
42 if isfield(HDR,
'FileName'),
43 FILENAME = HDR.FileName;
45 fprintf(2,
'Error SSAVE: missing FileName.\n');
49 HDR.FileName = FILENAME;
56 [HDR.SPR, HDR.NS] = size(DATA); HDR.NRec = 1;
57 % HDR.AS = rmfield(HDR.AS,
'SPR');
58 if (strcmp(HDR.TYPE,
'BDF') || strcmp(HDR.TYPE,
'EDF') || strcmp(HDR.TYPE,
'GDF')) && (~isfield(HDR,
'DigMax') || ~isfield(HDR,
'DigMin') || ~isfield(HDR,
'PhysMax') || ~isfield(HDR,
'PhysMin'))
59 HDR.PhysMax = max(DATA,[],1);
60 HDR.PhysMin = min(DATA,[],1);
61 ix = find(HDR.PhysMax == HDR.PhysMin);
62 HDR.PhysMin(ix) = HDR.PhysMin(ix) - 1;
63 if strcmp(HDR.TYPE,
'BDF')
64 [datatyp,HDR.THRESHOLD,datatypes,HDR.bits,HDR.GDFTYP] =
gdfdatatype(511+24*ones(HDR.NS,1));
65 HDR.DigMax = HDR.THRESHOLD(:,2)
';
66 HDR.DigMin = HDR.THRESHOLD(:,1)';
67 elseif strcmp(HDR.TYPE,
'EDF')
68 [datatyp,HDR.THRESHOLD,datatypes,HDR.bits,HDR.GDFTYP] =
gdfdatatype(3*ones(HDR.NS,1));
69 HDR.DigMax = HDR.THRESHOLD(:,2)
';
70 HDR.DigMin = HDR.THRESHOLD(:,1)';
71 elseif strcmp(HDR.TYPE,
'GDF')
72 [datatyp,HDR.THRESHOLD,datatypes,HDR.bits,HDR.GDFTYP] =
gdfdatatype(16*ones(HDR.NS,1));
73 HDR.DigMax = HDR.PhysMax;
74 HDR.DigMin = HDR.PhysMin;
76 HDR.DigMax = HDR.PhysMax;
77 HDR.DigMin = HDR.PhysMin;
82 if strcmp(TYPE,
'EVENT'),
86 elseif strcmp(TYPE,
'GDF2'),
89 elseif strncmp(TYPE,
'GDF',3),
93 HDR.TYPE = TYPE; % type of data format
103 if (nargin>2) && strcmp(TYPE,
'EVENT') return; end;
105 % Convert EVENT into WSCORE event format
106 if all([length(HDR.EVENT.POS), length(HDR.EVENT.TYP)]),
107 p = which('
sopen'); [p,H,e] = fileparts(p);
108 H =
sload(fullfile(p,'../doc/eventcodes.txt'));
110 HDR.EVENT.CodeDesc = H.CodeDesc;
111 HDR.EVENT.CodeIndex = H.CodeIndex;
112 if isfield(HDR.EVENT,'DUR')
113 HDR.EVENT.POS = [HDR.EVENT.POS; HDR.EVENT.POS + HDR.EVENT.DUR];
114 HDR.EVENT.TYP = [HDR.EVENT.TYP; HDR.EVENT.TYP + hex2dec('8000')];
116 OnOff = {
'On',
'Off'};
118 [HDR.EVENT.POS, ix] = sort(HDR.EVENT.POS);
119 HDR.EVENT.TYP = HDR.EVENT.TYP(ix);
120 [TYP, IX, IY] = unique(HDR.EVENT.TYP);
122 % write
"free form" scoring file
for WSCORE
123 fid = fopen(fullfile(HDR.FILE.Path,[HDR.FILE.Name,
'.C07']),
'w');
124 for k = 1:length(TYP),
125 fprintf(fid,
'%2i %s (%s)\r\n', k, HDR.EVENT.CodeDesc{mod(TYP(k),2^15)==HDR.EVENT.CodeIndex}, OnOff{(TYP(k)>=2^15)+1});
129 % write
"free form" scoring file
for WSCORE
130 fid = fopen(fullfile(HDR.FILE.Path,[HDR.FILE.Name,
'.007']),
'w');
131 fprintf(fid,
'%8i %i\r\n', [round(HDR.EVENT.POS(:)),IY(:)]
');