MNE-CPP  beta 1.0
fiffproducer.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "fiffproducer.h"
42 #include "fiffsimulator.h"
43 
44 
45 //*************************************************************************************************************
46 //=============================================================================================================
47 // Qt INCLUDES
48 //=============================================================================================================
49 
50 #include <QDebug>
51 
52 
53 //*************************************************************************************************************
54 //=============================================================================================================
55 // USED NAMESPACES
56 //=============================================================================================================
57 
58 using namespace FiffSimulatorPlugin;
59 
60 
61 //*************************************************************************************************************
62 //=============================================================================================================
63 // DEFINE MEMBER METHODS
64 //=============================================================================================================
65 
67 : m_pFiffSimulator(p_pFiffSimulator)
68 , m_bIsRunning(false)
69 {
70 
71 }
72 
73 
74 //*************************************************************************************************************
75 
77 {
78  qDebug() << "Destroy FiffProducer::~FiffProducer()";
79 
80  stop();
81 }
82 
83 
84 //*************************************************************************************************************
85 
87 {
88  m_bIsRunning = false;
89  QThread::wait();
90 
91  return true;
92 }
93 
94 
95 //*************************************************************************************************************
96 
98 {
99  m_bIsRunning = true;
100 
101  // reopen file in this thread
102  QFile t_File(m_pFiffSimulator->m_RawInfo.info.filename);
103  FiffStream::SPtr p_pStream(new FiffStream(&t_File));
104  m_pFiffSimulator->m_RawInfo.file = p_pStream;
105 
106  //
107  // Set up the reading parameters
108  //
109  fiff_int_t from = m_pFiffSimulator->m_RawInfo.first_samp;
110  fiff_int_t to = m_pFiffSimulator->m_RawInfo.last_samp;
111 // float quantum_sec = (float)uiSamplePeriod/1000000.0f; //read and write in 10 sec junks
112  fiff_int_t quantum = m_pFiffSimulator->m_uiBufferSampleSize;//ceil(quantum_sec*m_pFiffSimulator->m_pRawInfo->info.sfreq);
113 
114  qDebug() << "quantum " << quantum;
115 
116  //
117  // To read the whole file at once set
118  //
119  //quantum = to - from + 1;
120  //
121  //
122  // Read and write all the data
123  //
124 
125  fiff_int_t first, last;
126  MatrixXd data;
127  MatrixXd times;
128 
129  first = from;
130 
131  qint32 nchan = m_pFiffSimulator->m_RawInfo.info.nchan;
132 
133  MatrixXd cals(1,nchan);
134 
135 // SparseMatrix<double> inv_calsMat(nchan, nchan);
136 
137 // for(qint32 i = 0; i < nchan; ++i)
138 // inv_calsMat.insert(i, i) = 1.0f/m_pFiffSimulator->m_RawInfo.info.chs[i].cal;
139 
140  //Not good cause production time is not accurate
141  //loading and thread sleep is longer than thread sleep time - better to have a extra loading thread
142  // ToDo restructure this producer as laoding buffer --> and thread sleep to simulator buffer
143  fiff_int_t t_iDiff;
144  bool t_bRestart = false;
145 
146  while(m_bIsRunning)
147  {
148  last = first+quantum-1;
149  if (last > to)
150  {
151  t_iDiff = last - to;
152  t_bRestart = true;
153 
154  last = to;
155  }
156 
157  if (!m_pFiffSimulator->m_RawInfo.read_raw_segment(data,times,first,last))
158  {
159  printf("error during read_raw_segment\n");
160  }
161 
162  MatrixXf tmp = data.cast<float>();//(inv_calsMat*data).cast<float>();
163 
164  if(t_bRestart)
165  {
166  //
167  // Case end of Simulation: restart file from the beginning and read remaining bytes
168  //
169  printf("### RESTART Simulation File ###\r\n");
170 
171  first = from;
172  last = first+t_iDiff-1;
173 
174  if (!m_pFiffSimulator->m_RawInfo.read_raw_segment(data,times,first,last))
175  {
176  printf("error during read_raw_segment\n");
177  }
178 
179  MatrixXf tmp2 = data.cast<float>();//(inv_calsMat*data).cast<float>();
180 
181  MatrixXf tmp3(tmp.rows(), tmp.cols()+tmp2.cols());
182 
183  tmp3.block(0,0,tmp.rows(),tmp.cols()) = tmp;
184  tmp3.block(0,tmp.cols(),tmp.rows(),tmp2.cols()) = tmp2;
185 
186  tmp = tmp3;
187 
188  t_bRestart = false;
189  first += t_iDiff;
190  }
191  else
192  {
193  first += quantum;
194  }
195 
196  // call blocks until there is free space in the buffer
197  m_pFiffSimulator->m_pRawMatrixBuffer->push(&tmp);
198  }
199 
200  // close datastream in this thread
201 // delete m_pFiffSimulator->m_RawInfo.file;
202 // m_pFiffSimulator->m_RawInfo.file = NULL;
203 }
FiffProducer(FiffSimulator *simulator)
QSharedPointer< FiffStream > SPtr
Definition: fiff_stream.h:132
FiffStream::SPtr file
bool read_raw_segment(MatrixXd &data, MatrixXd &times, fiff_int_t from=-1, fiff_int_t to=-1, const RowVectorXi &sel=defaultRowVectorXi)
The FiffSimulator class provides a Fiff data simulator.
Definition: fiffsimulator.h:97
FIFF File I/O routines.
Definition: fiff_stream.h:129