• API Main Page
  • Documentation
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

src/mpitools.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Matthias Ihrke   *
00003  *   mihrke@uni-goettingen.de   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "mpitools.h"
00022 
00023 ulonglong sizeof_channelinfos( const ChannelInfo *chaninfo, int nbchan ){
00024   if( !chaninfo )
00025      return (ulonglong)0;
00026   else 
00027      return (ulonglong)nbchan*sizeof(ChannelInfo);
00028 }
00029 
00033 ulonglong sizeof_eegstream( const EEG *eeg ){
00034 #ifdef FIXEEG
00035   ulonglong n, tmp;
00036   int i;
00037 
00038   n=0;
00039   n += sizeof(unsigned int) + strlen( eeg->filename ); /* length+filename */
00040   n += sizeof(unsigned int) + strlen( eeg->comment );  /* length+comment */
00041   n += 3*sizeof(unsigned int); /* nbchan,ntrials,n */
00042   n += sizeof(double); /* sampling_rate */
00043 
00044   n += 1; /* if 0, no times array is present, else it is */
00045   if( eeg->times ){
00046      n += eeg->n*sizeof(double); /* times-array */
00047   }
00048   n += sizeof(ulonglong); /* nchannelinfo */  
00049   tmp = sizeof_channelinfos( eeg->chaninfo, eeg->nbchan );
00050   if( tmp>0 )
00051      n+=tmp;
00052   /* data */
00053   n += eeg->ntrials*eeg->nbchan*eeg->n*sizeof(double);
00054   /* markers */
00055   n += 1; /* markers present? */
00056   if( eeg->markers ){
00057      n += eeg->ntrials*sizeof(unsigned int); /* nmarkers */
00058      for( i=0; i<eeg->ntrials; i++ ){
00059         n += eeg->nmarkers[i]*sizeof(unsigned int);
00060         if( eeg->marker_labels ){
00061           n += eeg->nmarkers[i]*MAX_LABEL_LENGTH;
00062         }
00063      }
00064   } 
00065 
00066   return n;
00067 #endif
00068 }
00069 
00086 char* eeg_to_stream( const EEG *eeg, char *stream, ulonglong *n ){
00087 #ifdef FIXEEG
00088   char *s, /* stream ptr */
00089      *t;    /* temporary ptr */
00090   unsigned int t_uint; 
00091   ulonglong t_ulong;
00092   int c,trial,i;
00093 
00094   s = stream;
00095   /*-------- calculate size of stream -----------------*/
00096   *n=sizeof_eegstream( eeg );
00097   dprintf("stream is n=%llu long (ulonlong)\n", *n );
00098   
00099   if( !s ){
00100      s = (char*) malloc( *n );
00101   }
00102 
00103   /*-------fill stream ------------*/
00104 
00105   /* filename */
00106   t=s;
00107   t_uint = strlen( eeg->filename );
00108   memcpy( t, &t_uint, sizeof( unsigned int ) );
00109   t += sizeof( unsigned int );
00110   memcpy( t, eeg->filename, t_uint );
00111   t += t_uint;
00112   /* comment */ 
00113   t_uint = strlen( eeg->comment );
00114   memcpy( t, &t_uint, sizeof( unsigned int ) );
00115   t += sizeof( unsigned int );
00116   memcpy( t, eeg->comment, t_uint );
00117   t += t_uint;
00118   /* nbchan,ntrials,n */
00119   memcpy( t, &(eeg->nbchan), sizeof( unsigned int ) );
00120   t += sizeof(unsigned int);
00121   memcpy( t, &(eeg->ntrials), sizeof( unsigned int ) );
00122   t += sizeof(unsigned int);
00123   memcpy( t, &(eeg->n), sizeof( unsigned int ) );
00124   t += sizeof(unsigned int);
00125   /* sampling rate */
00126   memcpy( t, &(eeg->sampling_rate), sizeof( double ) );
00127   t += sizeof( double );
00128   /* times array */
00129   if( eeg->times ){
00130      *t=1; t++;
00131      memcpy( t, eeg->times, eeg->n*sizeof(double) );
00132      t += eeg->n*sizeof( double );
00133   } else {
00134      *t=0; t++;
00135   }
00136   /* nchannelinfo */
00137   t_ulong = sizeof_channelinfos( eeg->chaninfo, eeg->nbchan );
00138   memcpy( t, &t_ulong, sizeof( ulonglong ) );
00139   t += sizeof( ulonglong );
00140   /* channelinfos */
00141   if( t_ulong>0 ){
00142      for( i=0; i<eeg->nbchan; i++ ){
00143         memcpy( t, &(eeg->chaninfo[i]), sizeof( ChannelInfo ) );
00144         t += sizeof( ChannelInfo );
00145      }
00146   }
00147   /* data */
00148   for( c=0; c<eeg->nbchan; c++ ){
00149      for( trial=0; trial<eeg->ntrials; trial++ ){
00150         memcpy( t, eeg->data[c][trial], eeg->n*sizeof(double) );
00151         t += eeg->n*sizeof( double );
00152      }
00153   }
00154   /* markers */
00155   if( eeg->nmarkers ){
00156      *t=1; t++;
00157      memcpy( t, eeg->nmarkers, eeg->ntrials*sizeof(unsigned int) );
00158      t += eeg->ntrials*sizeof(unsigned int);
00159      for( i=0; i<eeg->ntrials; i++ ){
00160         memcpy( t, eeg->markers[i], eeg->nmarkers[i]*sizeof(unsigned int) );
00161         t += eeg->nmarkers[i]*sizeof(unsigned int);
00162      }
00163      if( eeg->marker_labels ){
00164         *t=1; t++;
00165         for( trial=0; trial<eeg->ntrials; trial++ ){
00166           for( i=0; i<eeg->nmarkers[trial]; i++ ){
00167              memcpy( t, eeg->marker_labels[trial][i], MAX_LABEL_LENGTH );
00168              t += MAX_LABEL_LENGTH;
00169           }
00170         }
00171      } else {
00172         *t=0; t++;
00173      }
00174   } else {
00175      *t=0; t++;
00176   }
00177 
00178   return s;
00179 #endif
00180 }
00193 EEG*  stream_to_eeg( const char *stream, ulonglong n, EEG *eeg ){
00194 #ifdef FIXEEG
00195   unsigned int nbchan, ntrials, nsamples;
00196   unsigned int t_uint;
00197   ulonglong t_ulong;
00198   char *filename;
00199   char *comment;
00200   int c,i,j;
00201 
00202   const char *t; /* temporary pointer to stream */
00203 
00204   t=stream;
00205   /* filename */
00206   memcpy( &t_uint, t, sizeof(unsigned int) );
00207   t+=sizeof(unsigned int);
00208   filename = (char*) malloc( t_uint+1 );
00209   memcpy( filename, t, t_uint );
00210   filename[t_uint]='\0';
00211   t+=t_uint; 
00212   /* comment */
00213   memcpy( &t_uint, t, sizeof(unsigned int) );
00214   t+=sizeof(unsigned int);
00215   comment = (char*) malloc( t_uint+1 );
00216   memcpy( comment, t, t_uint );
00217   comment[t_uint]='\0';
00218   t+=t_uint; 
00219   /* nbchan, ntrials,n */
00220   memcpy( &nbchan, t, sizeof(unsigned int)); 
00221   t+=sizeof(unsigned int);
00222   memcpy( &ntrials, t, sizeof(unsigned int)); 
00223   t+=sizeof(unsigned int);
00224   memcpy( &nsamples, t, sizeof(unsigned int)); 
00225   t+=sizeof(unsigned int);
00226   
00227   if( !eeg ){
00228      eeg = eeg_init( nbchan, ntrials, nsamples );
00229   } else {
00230      eeg->ntrials = ntrials;
00231      eeg->nbchan = nbchan;
00232      eeg->n = nsamples;
00233   }
00234   eeg->filename = filename;
00235   eeg->comment = comment;
00236   /* sampling_rate */
00237   memcpy( &(eeg->sampling_rate), t, sizeof( double ) );
00238   t += sizeof(double);
00239   
00240   /* times array */
00241   if(*t>0){ /* there is the times array */
00242      t++;
00243      eeg->times = (double*)malloc( nsamples*sizeof(double) );
00244      memcpy( eeg->times, t, eeg->n*sizeof(double) );
00245      t+=eeg->n*sizeof(double);
00246   } else {
00247      t++;
00248      eeg->times = NULL;
00249   }
00250   /* chaninfo */
00251   memcpy( &t_ulong, t, sizeof( ulonglong ) );
00252   t += sizeof( ulonglong );
00253   if( t_ulong>0 ){
00254      eeg->chaninfo = (ChannelInfo*) malloc( eeg->nbchan*sizeof(ChannelInfo) );
00255      for( i=0; i<eeg->nbchan; i++ ){
00256         memcpy( &(eeg->chaninfo[i]), t, sizeof(ChannelInfo) );
00257         t+=sizeof(ChannelInfo);
00258      }
00259   }
00260   /* data */
00261   for( c=0; c<nbchan; c++ ){
00262      for( i=0; i<ntrials; i++ ){
00263         memcpy( eeg->data[c][i], t, nsamples*sizeof(double) );
00264         t+=nsamples*sizeof(double);
00265      }
00266   }
00267   /* markers */
00268   if( *t>0 ){
00269      t++;
00270      eeg->nmarkers = (unsigned int*)malloc( ntrials*sizeof(unsigned int) );
00271      eeg->markers = (unsigned int**)malloc( ntrials*sizeof(unsigned int*));
00272      memcpy( eeg->nmarkers, t, ntrials*sizeof(unsigned int) );
00273      t += ntrials*sizeof(unsigned int);
00274      for( i=0; i<ntrials; i++ ){
00275         eeg->markers[i] = (unsigned int*) malloc( eeg->nmarkers[i]*sizeof(unsigned int) );
00276         memcpy( eeg->markers[i], t, eeg->nmarkers[i]*sizeof(unsigned int) );
00277         t += eeg->nmarkers[i]*sizeof(unsigned int);
00278      }
00279      /* marker labels */
00280      if( *t>0 ){
00281         t++;
00282         eeg->marker_labels = (char***)malloc( ntrials*sizeof(char**) );
00283         for( i=0; i<ntrials; i++ ){
00284           eeg->marker_labels[i] = (char**)malloc( eeg->nmarkers[i]*sizeof(char*) );
00285           for( j=0; j<eeg->nmarkers[i]; j++ ){
00286              eeg->marker_labels[i][j] = (char*) malloc( MAX_LABEL_LENGTH );
00287              memcpy( eeg->marker_labels[i][j], t, MAX_LABEL_LENGTH );
00288              t+=MAX_LABEL_LENGTH;
00289           }
00290         }
00291      }  
00292   }
00293 
00294   return eeg;
00295 #endif
00296 }

Generated on Fri Jun 25 2010 14:10:19 for libeegtools by  doxygen 1.7.0