|
libGDF
|
00001 // 00002 // This file is part of libGDF. 00003 // 00004 // libGDF is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU Lesser General Public License as 00006 // published by the Free Software Foundation, either version 3 of 00007 // the License, or (at your option) any later version. 00008 // 00009 // libGDF is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU Lesser General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU Lesser General Public License 00015 // along with libGDF. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 // Copyright 2010 Martin Billinger 00018 00019 #include "GDF/Channel.h" 00020 #include "GDF/ChannelData.h" 00021 #include <boost/numeric/conversion/cast.hpp> 00022 #include <boost/lexical_cast.hpp> 00023 //#include <iostream> 00024 00025 namespace gdf { 00026 00027 Channel::Channel( const SignalHeader *sig_hdr, const size_t length ) 00028 { 00029 m_signalheader = sig_hdr; 00030 00031 switch( m_signalheader->get_datatype( ) ) 00032 { 00033 case INT8: 00034 { 00035 m_data = new ChannelData<int8>( length ); 00036 } break; 00037 case UINT8: 00038 { 00039 m_data = new ChannelData<uint8>( length ); 00040 } break; 00041 case INT16: 00042 { 00043 m_data = new ChannelData<int16>( length ); 00044 } break; 00045 case UINT16: 00046 { 00047 m_data = new ChannelData<uint16>( length ); 00048 } break; 00049 case INT32: 00050 { 00051 m_data = new ChannelData<int32>( length ); 00052 } break; 00053 case UINT32: 00054 { 00055 m_data = new ChannelData<uint32>( length ); 00056 } break; 00057 case INT64: 00058 { 00059 m_data = new ChannelData<int64>( length ); 00060 } break; 00061 case UINT64: 00062 { 00063 m_data = new ChannelData<uint64>( length ); 00064 } break; 00065 case FLOAT32: 00066 { 00067 m_data = new ChannelData<float32>( length ); 00068 } break; 00069 case FLOAT64: 00070 { 00071 m_data = new ChannelData<float64>( length ); 00072 } break; 00073 default: throw exception::invalid_type_id( boost::lexical_cast<std::string>(m_signalheader->get_datatype( )) ); break; 00074 } 00075 } 00076 00077 //=================================================================================================== 00078 //=================================================================================================== 00079 00080 Channel::Channel( const SignalHeader *sig_hdr ) 00081 { 00082 m_signalheader = sig_hdr; 00083 00084 size_t length = sig_hdr->get_samples_per_record( ); 00085 00086 switch( m_signalheader->get_datatype( ) ) 00087 { 00088 case INT8: 00089 { 00090 m_data = new ChannelData<int8>( length ); 00091 } break; 00092 case UINT8: 00093 { 00094 m_data = new ChannelData<uint8>( length ); 00095 } break; 00096 case INT16: 00097 { 00098 m_data = new ChannelData<int16>( length ); 00099 } break; 00100 case UINT16: 00101 { 00102 m_data = new ChannelData<uint16>( length ); 00103 } break; 00104 case INT32: 00105 { 00106 m_data = new ChannelData<int32>( length ); 00107 } break; 00108 case UINT32: 00109 { 00110 m_data = new ChannelData<uint32>( length ); 00111 } break; 00112 case INT64: 00113 { 00114 m_data = new ChannelData<int64>( length ); 00115 } break; 00116 case UINT64: 00117 { 00118 m_data = new ChannelData<uint64>( length ); 00119 } break; 00120 case FLOAT32: 00121 { 00122 m_data = new ChannelData<float32>( length ); 00123 } break; 00124 case FLOAT64: 00125 { 00126 m_data = new ChannelData<float64>( length ); 00127 } break; 00128 default: throw exception::invalid_type_id( boost::lexical_cast<std::string>(m_signalheader->get_datatype( )) ); break; 00129 } 00130 } 00131 00132 //=================================================================================================== 00133 //=================================================================================================== 00134 00135 Channel::Channel( const Channel &other ) 00136 { 00137 m_signalheader = other.m_signalheader; 00138 //size_t length = m_signalheader->get_samples_per_record( ); 00139 00140 switch( m_signalheader->get_datatype( ) ) 00141 { 00142 case INT8: 00143 { 00144 m_data = new ChannelData<int8>( other.m_data ); 00145 } break; 00146 case UINT8: 00147 { 00148 m_data = new ChannelData<uint8>( other.m_data ); 00149 } break; 00150 case INT16: 00151 { 00152 m_data = new ChannelData<int16>( other.m_data ); 00153 } break; 00154 case UINT16: 00155 { 00156 m_data = new ChannelData<uint16>( other.m_data ); 00157 } break; 00158 case INT32: 00159 { 00160 m_data = new ChannelData<int32>( other.m_data ); 00161 } break; 00162 case UINT32: 00163 { 00164 m_data = new ChannelData<uint32>( other.m_data ); 00165 } break; 00166 case INT64: 00167 { 00168 m_data = new ChannelData<int64>( other.m_data ); 00169 } break; 00170 case UINT64: 00171 { 00172 m_data = new ChannelData<uint64>( other.m_data ); 00173 } break; 00174 case FLOAT32: 00175 { 00176 m_data = new ChannelData<float32>( other.m_data ); 00177 } break; 00178 case FLOAT64: 00179 { 00180 m_data = new ChannelData<float64>( other.m_data ); 00181 } break; 00182 default: throw exception::invalid_type_id( boost::lexical_cast<std::string>(m_signalheader->get_datatype( )) ); break; 00183 } 00184 } 00185 00186 //=================================================================================================== 00187 //=================================================================================================== 00188 00189 Channel::~Channel( ) 00190 { 00191 delete m_data; 00192 } 00193 00194 //=================================================================================================== 00195 //=================================================================================================== 00196 00197 void Channel::clear( ) 00198 { 00199 m_data->clear( ); 00200 } 00201 00202 //=================================================================================================== 00203 //=================================================================================================== 00204 00205 void Channel::addSamplePhys( const double value ) 00206 { 00207 using boost::numeric_cast; 00208 00209 double rawval = m_signalheader->phys_to_raw( value ); 00210 00211 switch( m_signalheader->get_datatype( ) ) 00212 { 00213 case INT8: m_data->addSample( numeric_cast<int8>(rawval) ); break; 00214 case UINT8: m_data->addSample( numeric_cast<uint8>(rawval) ); break; 00215 case INT16: m_data->addSample( numeric_cast<int16>(rawval) ); break; 00216 case UINT16: m_data->addSample( numeric_cast<uint16>(rawval) ); break; 00217 case INT32: m_data->addSample( numeric_cast<int32>(rawval) ); break; 00218 case UINT32: m_data->addSample( numeric_cast<uint32>(rawval) ); break; 00219 case INT64: m_data->addSample( numeric_cast<int64>(rawval) ); break; 00220 case UINT64: m_data->addSample( numeric_cast<uint64>(rawval) ); break; 00221 case FLOAT32: m_data->addSample( numeric_cast<float32>(rawval) ); break; 00222 case FLOAT64: m_data->addSample( numeric_cast<float64>(rawval) ); break; 00223 default: throw exception::invalid_type_id( boost::lexical_cast<std::string>(m_signalheader->get_datatype( )) ); break; 00224 }; 00225 } 00226 00227 //=================================================================================================== 00228 //=================================================================================================== 00229 00230 void Channel::blitSamplesPhys( const double *values, size_t num ) 00231 { 00232 for( size_t i=0; i<num; i++ ) 00233 addSamplePhys( values[i] ); 00234 } 00235 00236 //=================================================================================================== 00237 //=================================================================================================== 00238 00239 void Channel::fillPhys( const double value, size_t num ) 00240 { 00241 using boost::numeric_cast; 00242 double rawval = value * m_signalheader->phys_to_raw( value ); 00243 00244 switch( m_signalheader->get_datatype( ) ) 00245 { 00246 case INT8: m_data->fill( numeric_cast<int8>(rawval), num ); break; 00247 case UINT8: m_data->fill( numeric_cast<uint8>(rawval), num ); break; 00248 case INT16: m_data->fill( numeric_cast<int16>(rawval), num ); break; 00249 case UINT16: m_data->fill( numeric_cast<uint16>(rawval), num ); break; 00250 case INT32: m_data->fill( numeric_cast<int32>(rawval), num ); break; 00251 case UINT32: m_data->fill( numeric_cast<uint32>(rawval), num ); break; 00252 case INT64: m_data->fill( numeric_cast<int64>(rawval), num ); break; 00253 case UINT64: m_data->fill( numeric_cast<uint64>(rawval), num ); break; 00254 case FLOAT32: m_data->fill( numeric_cast<float32>(rawval), num ); break; 00255 case FLOAT64: m_data->fill( numeric_cast<float64>(rawval), num ); break; 00256 default: throw exception::invalid_type_id( boost::lexical_cast<std::string>(m_signalheader->get_datatype( )) ); break; 00257 }; 00258 } 00259 00260 //=================================================================================================== 00261 //=================================================================================================== 00262 00263 template<typename T> void Channel::fillRaw( const T rawval, size_t num ) 00264 { 00265 using boost::numeric_cast; 00266 00267 switch( m_signalheader->get_datatype( ) ) 00268 { 00269 case INT8: m_data->fill( numeric_cast<int8>(rawval), num ); break; 00270 case UINT8: m_data->fill( numeric_cast<uint8>(rawval), num ); break; 00271 case INT16: m_data->fill( numeric_cast<int16>(rawval), num ); break; 00272 case UINT16: m_data->fill( numeric_cast<uint16>(rawval), num ); break; 00273 case INT32: m_data->fill( numeric_cast<int32>(rawval), num ); break; 00274 case UINT32: m_data->fill( numeric_cast<uint32>(rawval), num ); break; 00275 case INT64: m_data->fill( numeric_cast<int64>(rawval), num ); break; 00276 case UINT64: m_data->fill( numeric_cast<uint64>(rawval), num ); break; 00277 case FLOAT32: m_data->fill( numeric_cast<float32>(rawval), num ); break; 00278 case FLOAT64: m_data->fill( numeric_cast<float64>(rawval), num ); break; 00279 default: throw exception::invalid_type_id( boost::lexical_cast<std::string>(m_signalheader->get_datatype( )) ); break; 00280 }; 00281 } 00282 00283 //=================================================================================================== 00284 //=================================================================================================== 00285 00286 void Channel::setSamplePhys( size_t pos, double value ) 00287 { 00288 using boost::numeric_cast; 00289 00290 double rawval = m_signalheader->phys_to_raw( value ); 00291 00292 switch( m_signalheader->get_datatype() ) 00293 { 00294 case INT8: m_data->setSample( pos, numeric_cast<int8>( rawval ) ); break; 00295 case UINT8: m_data->setSample( pos, numeric_cast<uint8>( rawval ) ); break; 00296 case INT16: m_data->setSample( pos, numeric_cast<int16>( rawval ) ); break; 00297 case UINT16: m_data->setSample( pos, numeric_cast<uint16>( rawval ) ); break; 00298 case INT32: m_data->setSample( pos, numeric_cast<int32>( rawval ) ); break; 00299 case UINT32: m_data->setSample( pos, numeric_cast<uint32>( rawval ) ); break; 00300 case INT64: m_data->setSample( pos, numeric_cast<int64>( rawval ) ); break; 00301 case UINT64: m_data->setSample( pos, numeric_cast<uint64>( rawval ) ); break; 00302 case FLOAT32: m_data->setSample( pos, numeric_cast<float32>( rawval ) ); break; 00303 case FLOAT64: m_data->setSample( pos, numeric_cast<float64>( rawval ) ); break; 00304 default: throw exception::invalid_type_id( boost::lexical_cast<std::string>(m_signalheader->get_datatype( )) ); break; 00305 } 00306 } 00307 00308 //=================================================================================================== 00309 //=================================================================================================== 00310 00311 double Channel::getSamplePhys( size_t pos ) 00312 { 00313 using boost::numeric_cast; 00314 00315 switch( m_signalheader->get_datatype() ) 00316 { 00317 case INT8: return m_signalheader->raw_to_phys( numeric_cast<double>( m_data->getSample( pos, int8() ) ) ); 00318 case UINT8: return m_signalheader->raw_to_phys( numeric_cast<double>( m_data->getSample( pos, uint8() ) ) ); 00319 case INT16: return m_signalheader->raw_to_phys( numeric_cast<double>( m_data->getSample( pos, int16() ) ) ); 00320 case UINT16: return m_signalheader->raw_to_phys( numeric_cast<double>( m_data->getSample( pos, uint16() ) ) ); 00321 case INT32: return m_signalheader->raw_to_phys( numeric_cast<double>( m_data->getSample( pos, int32() ) ) ); 00322 case UINT32: return m_signalheader->raw_to_phys( numeric_cast<double>( m_data->getSample( pos, uint32() ) ) ); 00323 case INT64: return m_signalheader->raw_to_phys( numeric_cast<double>( m_data->getSample( pos, int64() ) ) ); 00324 case UINT64: return m_signalheader->raw_to_phys( numeric_cast<double>( m_data->getSample( pos, uint64() ) ) ); 00325 case FLOAT32: return m_signalheader->raw_to_phys( numeric_cast<double>( m_data->getSample( pos, float32() ) ) ); 00326 case FLOAT64: return m_signalheader->raw_to_phys( numeric_cast<double>( m_data->getSample( pos, float64() ) ) ); 00327 default: throw exception::invalid_type_id( boost::lexical_cast<std::string>(m_signalheader->get_datatype( )) ); break; 00328 } 00329 } 00330 00331 //=================================================================================================== 00332 //=================================================================================================== 00333 00334 void Channel::deblitSamplesPhys( double *values, size_t start, size_t num ) 00335 { 00336 for( size_t i=0; i<num; i++ ) 00337 { 00338 values[i] = getSamplePhys( start + i ); 00339 } 00340 } 00341 00342 //=================================================================================================== 00343 //=================================================================================================== 00344 00345 size_t Channel::getFree( ) 00346 { 00347 return m_data->getFree( ); 00348 } 00349 00350 //=================================================================================================== 00351 //=================================================================================================== 00352 00353 size_t Channel::getWritten( ) 00354 { 00355 return m_data->getWritten( ); 00356 } 00357 00358 //=================================================================================================== 00359 //=================================================================================================== 00360 00361 uint32 Channel::getTypeID( ) 00362 { 00363 return m_signalheader->get_datatype( ); 00364 } 00365 00366 //=================================================================================================== 00367 //=================================================================================================== 00368 00369 std::ostream &operator<<( std::ostream &out, const Channel &c ) 00370 { 00371 c.m_data->tostream( out ); 00372 return out; 00373 } 00374 00375 //=================================================================================================== 00376 //=================================================================================================== 00377 00378 std::istream &operator>>( std::istream &in, Channel &c ) 00379 { 00380 c.m_data->fromstream( in ); 00381 return in; 00382 } 00383 00384 }