File: gdf_writer.cpp

package info (click to toggle)
libgdf 0.1.3-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,352 kB
  • sloc: cpp: 7,096; makefile: 65; sh: 49
file content (314 lines) | stat: -rw-r--r-- 13,190 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//
// This file is part of libGDF.
//
// libGDF is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// libGDF is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libGDF.  If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2010 Martin Billinger

#include "gdf_mex.h"
#include "matlab_tools/InstanceManager.h"
#include "matlab_tools/Commands.h"
#include "matlab_tools/mxStructAccess.h"
#include <GDF/Writer.h>
#include <mex.h>
#include "math.h"

using namespace std;

#ifndef trunc
inline double trunc( const double a ) { return floor( a ); }
#endif

#ifdef _WIN32
inline bool isfinite( const double a ) { return _finite( a ); }
#endif

// ===================================================================================================
//      Object Interface to mex
// ===================================================================================================

class CmexObject
{
public:
    CmexObject( );

    ~CmexObject( );

    void execute( size_t nlhs, mxArray *plhs[], size_t nrhs, const mxArray *prhs[] );

    static CmexObject &getInstance( ) { return instance; }

    InstanceManager<gdf::Writer> writers;
    CommandManager commands;

private:
    static CmexObject instance;
};

// ===================================================================================================
//      Commands
// ===================================================================================================

class CMD_init : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };
class CMD_clear : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };
class CMD_clearall : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };
class CMD_getheader : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };
class CMD_setheader : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };
class CMD_createsignal : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };
class CMD_newsignal : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };
class CMD_recduration : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };
class CMD_eventconfig : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };
class CMD_addsample : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };
class CMD_addrawsample : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };
class CMD_addsamplevector : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };
class CMD_blitsamples : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };
class CMD_blitrawsamples : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };
class CMD_mode1event : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };
class CMD_mode3event : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };
class CMD_open : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };
class CMD_close : public Command { void execute( mxArray *plhs[], const mxArray *prhs[] ); };

// ===================================================================================================
//      mexFunction
// ===================================================================================================

CmexObject CmexObject::instance;

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
    CmexObject::getInstance( ).execute( nlhs, plhs, nrhs, prhs );
}

// ===================================================================================================
//      CmexObject Definition
// ===================================================================================================


CmexObject::CmexObject( )
{
    commands.registerCommand( "Init", new CMD_init( ), 1, 0 );
    commands.registerCommand( "Clear", new CMD_clear( ), 0, 1 );
    commands.registerCommand( "Clear All", new CMD_clearall( ), 0, 0 );
    commands.registerCommand( "CreateSignal", new CMD_createsignal( ), 0, 2 );
    commands.registerCommand( "NewSignal", new CMD_newsignal( ), 1, 3 );
    commands.registerCommand( "Open", new CMD_open( ), 0, 2 );
    commands.registerCommand( "Close", new CMD_close( ), 0, 1 );
    commands.registerCommand( "GetHeader", new CMD_getheader( ), 1, 1 );
    commands.registerCommand( "SetHeader", new CMD_setheader( ), 0, 2 );
    commands.registerCommand( "RecordDuration", new CMD_recduration( ), 0, 2 );
    commands.registerCommand( "EventConfig", new CMD_eventconfig( ), 0, 3 );
    commands.registerCommand( "AddSample", new CMD_addsample( ), 0, 3 );
    commands.registerCommand( "AddRawSample", new CMD_addrawsample( ), 0, 3 );
    commands.registerCommand( "AddVecSample", new CMD_addsamplevector( ), 0, 3 );
    commands.registerCommand( "BlitSamples", new CMD_blitsamples( ), 0, 3 );
    commands.registerCommand( "BlitRawSamples", new CMD_blitrawsamples( ), 0, 3 );
    commands.registerCommand( "Mode1Ev", new CMD_mode1event( ), 0, 3 );
    commands.registerCommand( "Mode3Ev", new CMD_mode3event( ), 0, 5 );
}

CmexObject::~CmexObject( )
{
}

void CmexObject::execute( size_t nlhs, mxArray *plhs[], size_t nrhs, const mxArray *prhs[] )
{
    if( nrhs < 1 )
        throw std::invalid_argument( "Expecting at least one argument." );

    if( !mxIsChar(prhs[0]) )
        throw std::invalid_argument( "First argument must be a command (string)." );

    char *command;
    command = new char[32];
    if( mxGetString( prhs[0], command, 32 ) != 0 )
        throw std::invalid_argument( "Unknown problem with command string." );

    commands.execute( command, nlhs, plhs, nrhs-1, prhs+1 );
    delete[] command;
}

// ===================================================================================================
//      Command Definitions
// ===================================================================================================

void CMD_init::execute( mxArray *plhs[], const mxArray ** /*prhs*/ )
{
    size_t handle = CmexObject::getInstance().writers.newInstance( );
    plhs[0] = mxCreateNumericMatrix( 1, 1, mxUINT64_CLASS, mxREAL );
    *reinterpret_cast<size_t*>(mxGetData( plhs[0] )) = handle;
}

void CMD_clear::execute( mxArray ** /*plhs*/, const mxArray *prhs[] )
{
    size_t handle = mx::getNumeric<size_t>( prhs[0] );
    CmexObject::getInstance().writers.remove( handle );
}

void CMD_clearall::execute( mxArray ** /*plhs*/, const mxArray ** /*prhs*/ )
{
    CmexObject::getInstance().writers.clear( );
}

void CMD_getheader::execute( mxArray *plhs[], const mxArray *prhs[] )
{
    size_t handle = mx::getNumeric<size_t>( prhs[0] );
    gdf::Writer *w = CmexObject::getInstance().writers.get( handle );
    plhs[0] = constructHeaderStruct( w->getNumSignals( ) );
    Header2Struct( plhs[0], &w->getHeaderAccess_readonly() );
}

void CMD_setheader::execute( mxArray ** /*plhs*/, const mxArray *prhs[] )
{
    size_t handle = mx::getNumeric<size_t>( prhs[0] );
    gdf::Writer *w = CmexObject::getInstance().writers.get( handle );
    Struct2Header( prhs[1], &w->getHeaderAccess( ) );
}

void CMD_createsignal::execute( mxArray ** /*plhs*/, const mxArray *prhs[] )
{
    size_t handle = mx::getNumeric<size_t>( prhs[0] );
    gdf::Writer *w = CmexObject::getInstance().writers.get( handle );
    size_t channel = mx::getNumeric<size_t>( prhs[1] );
    if( channel < 1 )
        throw std::invalid_argument( "Channel index must be >= 1." );
    w->createSignal( channel - 1, true );
}

void CMD_newsignal::execute( mxArray *plhs[], const mxArray *prhs[] )
{
    size_t handle = mx::getNumeric<size_t>( prhs[0] );
    gdf::uint32 type = mx::getNumeric<gdf::uint32>( prhs[1] );
    gdf::uint32 fs = mx::getNumeric<gdf::uint32>( prhs[2] );
    gdf::Writer *w = CmexObject::getInstance().writers.get( handle );
    size_t channel = w->getFirstFreeSignalIndex( );
    w->createSignal( channel, true );
    plhs[0] = mxCreateNumericMatrix( 1, 1, mxUINT64_CLASS, mxREAL );
    *reinterpret_cast<size_t*>(mxGetData( plhs[0] )) = channel + 1;

    w->getSignalHeader(channel).set_datatype( type );
    w->getSignalHeader(channel).set_samplerate( fs );
}

void CMD_recduration::execute( mxArray ** /*plhs[]*/, const mxArray *prhs[] )
{
    size_t handle = mx::getNumeric<size_t>( prhs[0] );
    gdf::Writer *w = CmexObject::getInstance().writers.get( handle );
    double recdur = mx::getNumeric<double>( prhs[1] );
    if( recdur == 0 || !isfinite(recdur) )
        w->getHeaderAccess( ).enableAutoRecordDuration( );
    else
    {
        gdf::uint32 num = boost::numeric_cast<gdf::uint32>( trunc(recdur) );
        gdf::uint32 den = 1;
        while( recdur*den - num != 0 )
        {
            cout << num << "/" << den << endl;
            den *= 10;
            num = boost::numeric_cast<gdf::uint32>( trunc(recdur*den) );
        }
        w->getHeaderAccess( ).setRecordDuration( num, den );
    }
}

void CMD_eventconfig::execute( mxArray ** /*plhs*/, const mxArray *prhs[] )
{
    size_t handle = mx::getNumeric<size_t>( prhs[0] );
    gdf::Writer *w = CmexObject::getInstance().writers.get( handle );
    gdf::uint8 mode = mx::getNumeric<gdf::uint8>( prhs[1] );
    gdf::float32 fs = mx::getNumeric<gdf::float32>( prhs[2] );
    w->setEventMode( mode );
    w->setEventSamplingRate( fs );
}

void CMD_addsample::execute( mxArray ** /*plhs*/, const mxArray *prhs[] )
{
    size_t handle = mx::getNumeric<size_t>( prhs[0] );
    gdf::Writer *w = CmexObject::getInstance().writers.get( handle );
    size_t channel_idx = mx::getNumeric<size_t>( prhs[1] );
    double value = mx::getNumeric<double>( prhs[2] );
    w->addSamplePhys( channel_idx-1, value );
}

void CMD_addrawsample::execute( mxArray ** /*plhs*/, const mxArray *prhs[] )
{
    size_t handle = mx::getNumeric<size_t>( prhs[0] );
    gdf::Writer *w = CmexObject::getInstance().writers.get( handle );
    size_t channel_idx = mx::getNumeric<size_t>( prhs[1] );
    double value = mx::getNumeric<double>( prhs[2] );
    w->addSampleRaw( channel_idx-1, value );
}

void CMD_addsamplevector::execute( mxArray ** /*plhs*/, const mxArray *prhs[] )
{
    size_t handle = mx::getNumeric<size_t>( prhs[0] );
    gdf::Writer *w = CmexObject::getInstance().writers.get( handle );
    size_t first_channel = mx::getNumeric<size_t>( prhs[1] ) - 1;
    std::vector<double> vals = mx::getNumericArray<double>( prhs[2] );
    for( size_t i=0; i<vals.size(); i++ )
        w->addSamplePhys( first_channel+i, vals[i] );
}

void CMD_blitsamples::execute( mxArray ** /*plhs*/, const mxArray *prhs[] )
{
    size_t handle = mx::getNumeric<size_t>( prhs[0] );
    gdf::Writer *w = CmexObject::getInstance().writers.get( handle );
    size_t channel_idx = mx::getNumeric<size_t>( prhs[1] );
    std::vector<double> vals = mx::getNumericArray<double>( prhs[2] );
    w->blitSamplesPhys( channel_idx-1, vals );
}

void CMD_blitrawsamples::execute( mxArray ** /*plhs*/, const mxArray *prhs[] )
{
    size_t handle = mx::getNumeric<size_t>( prhs[0] );
    gdf::Writer *w = CmexObject::getInstance().writers.get( handle );
    size_t channel_idx = mx::getNumeric<size_t>( prhs[1] );
    std::vector<double> vals = mx::getNumericArray<double>( prhs[2] );
    w->blitSamplesRaw( channel_idx-1, vals );
}

void CMD_mode1event::execute( mxArray ** /*plhs*/, const mxArray *prhs[] )
{
    size_t handle = mx::getNumeric<size_t>( prhs[0] );
    gdf::Writer *w = CmexObject::getInstance().writers.get( handle );
    gdf::uint32 pos = mx::getNumeric<gdf::uint32>( prhs[1] );
    gdf::uint16 type = mx::getNumeric<gdf::uint16>( prhs[2] );
    w->addEvent( pos, type );
}

void CMD_mode3event::execute( mxArray ** /*plhs*/, const mxArray *prhs[] )
{
    size_t handle = mx::getNumeric<size_t>( prhs[0] );
    gdf::Writer *w = CmexObject::getInstance().writers.get( handle );
    gdf::uint32 pos = mx::getNumeric<gdf::uint32>( prhs[1] );
    gdf::uint16 type = mx::getNumeric<gdf::uint16>( prhs[2] );
    gdf::uint16 chan = mx::getNumeric<gdf::uint16>( prhs[3] );
    gdf::uint32 dur = mx::getNumeric<gdf::uint32>( prhs[4] );
    w->addEvent( pos, type, chan, dur );
}

void CMD_open::execute( mxArray ** /*plhs*/, const mxArray *prhs[] )
{
    size_t handle = mx::getNumeric<size_t>( prhs[0] );
    gdf::Writer *w = CmexObject::getInstance().writers.get( handle );
    std::string filename = mx::getString( prhs[1] );
    w->open( filename );
}

void CMD_close::execute( mxArray ** /*plhs*/, const mxArray *prhs[] )
{
    size_t handle = mx::getNumeric<size_t>( prhs[0] );
    gdf::Writer *w = CmexObject::getInstance().writers.get( handle );
    w->close( );
}