TEAP (Toolbox for Emotion Analysis using Physiological Signals) doc
ssave.m
Go to the documentation of this file.
1 function [HDR] = ssave(FILENAME,DATA,TYPE,Fs,gdftyp)
2 % SSAVE saves signal data in various data formats
3 %
4 % Currently are the following data formats supported:
5 % EDF, BDF, GDF, BKR, SND/AU, (WAV, AIF)
6 % and WSCORE event file
7 %
8 % HDR = ssave(HDR,data);
9 % HDR = ssave(FILENAME,data,TYPE,Fs);
10 %
11 % FILENAME name of file
12 % data signal data, each column is a channel
13 % TYPE determines dataformat
14 % Fs sampling rate
15 %
16 % see also: SSAVE, SOPEN, SWRITE, SCLOSE, doc/README
17 %
18 
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://biosig.sf.net/
22 
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.
27 %
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.
32 %
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.
37 
38 
39 
40 if isstruct(FILENAME),
41  HDR = FILENAME;
42  if isfield(HDR,'FileName'),
43  FILENAME = HDR.FileName;
44  else
45  fprintf(2,'Error SSAVE: missing FileName.\n');
46  return;
47  end;
48 else
49  HDR.FileName = FILENAME;
50  HDR.SampleRate = Fs;
51  gdftyp = 16;
52  HDR.TYPE = 'native';
53 end;
54 
55 if (nargin > 1),
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;
75  else
76  HDR.DigMax = HDR.PhysMax;
77  HDR.DigMin = HDR.PhysMin;
78  end;
79  end;
80 
81  if (nargin > 2),
82  if strcmp(TYPE,'EVENT'),
83  HDR.TYPE = 'GDF';
84  HDR.VERSION = 2;
85  HDR.NS = 0;
86  elseif strcmp(TYPE,'GDF2'),
87  HDR.TYPE = 'GDF';
88  HDR.VERSION = 2;
89  elseif strncmp(TYPE,'GDF',3),
90  HDR.TYPE = 'GDF';
91  HDR.VERSION = 1.25;
92  else
93  HDR.TYPE = TYPE; % type of data format
94  end;
95  end;
96  HDR = sopen(HDR,'w');
97  if ~isempty(DATA)
98  HDR = swrite(HDR,DATA);
99  end;
100  HDR = sclose(HDR);
101 end;
102 
103 if (nargin>2) && strcmp(TYPE,'EVENT') return; end;
104 
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'));
109 
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')];
115  end;
116  OnOff = {'On','Off'};
117 
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);
121 
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});
126  end;
127  fclose(fid);
128 
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(:)]');
132  fclose(fid);
133 end;
sclose
function sclose(in HDR)
ssave
function ssave(in FILENAME, in DATA, in TYPE, in Fs, in gdftyp)
gdfdatatype
function gdfdatatype(in GDFTYP)
sload
function sload(in FILENAME, in varargin)
swrite
function swrite(in HDR, in data)
sopen
function sopen(in arg1, in PERMISSION, in CHAN, in MODE, in arg5, in arg6)