MNE-CPP  beta 1.0
main.cpp
1 //=============================================================================================================
38 //*************************************************************************************************************
39 //=============================================================================================================
40 // INCLUDES
41 //=============================================================================================================
42 
43 #include <iostream>
44 #include <vector>
45 #include <math.h>
46 
47 
48 #include <fiff/fiff.h>
49 #include <mne/mne.h>
50 
51 #include <Eigen/Core>
52 
53 //*************************************************************************************************************
54 //=============================================================================================================
55 // QT INCLUDES
56 //=============================================================================================================
57 
58 #include <QtCore/QCoreApplication>
59 
60 
61 //*************************************************************************************************************
62 //=============================================================================================================
63 // USED NAMESPACES
64 //=============================================================================================================
65 
66 using namespace FIFFLIB;
67 using namespace MNELIB;
68 
69 
70 //*************************************************************************************************************
71 //=============================================================================================================
72 // MAIN
73 //=============================================================================================================
74 
75 //=============================================================================================================
84 int main(int argc, char *argv[])
85 {
86  QCoreApplication a(argc, argv);
87 
88  //generate FiffEvokedSet
89  QFile t_sampleFile("./MNE-sample-data/MEG/sample/sample_audvis-ave.fif");
90  FiffEvokedSet p_FiffEvokedSet(t_sampleFile);
91 
92  //mne_ex_evoked_grad_amp.m example
93  fiff_int_t coil1,coil2;
94  QString one,two;
95  QChar lastone,lasttwo;
96  qint32 npair = 0;
97  MatrixXi pairs(p_FiffEvokedSet.info.nchan,2);
98  fiff_double_t base1,base2;
99 
100  QStringList ch_sel_names;
101 
102  //settings
103  bool do_baseline = false;
104  fiff_int_t b1 = 1;
105  fiff_int_t b2 = 1;
106  fiff_double_t bmin = 0;
107  fiff_double_t bmax = 0.231;
108 
109  for(qint32 i=0; i < p_FiffEvokedSet.info.nchan-1; ++i) {
110  //First check the coil types
111  coil1 = p_FiffEvokedSet.info.chs.at(i).coil_type;
112  coil2 = p_FiffEvokedSet.info.chs.at(i+1).coil_type;
113  if (coil1 == coil2 && (coil1 == 2 || coil1 == 3012 || coil1 == 3013)) {
114  one = p_FiffEvokedSet.info.ch_names[i];
115  two = p_FiffEvokedSet.info.ch_names[i+1];
116  lastone = one.at(one.size()-1);
117  lasttwo = one.at(two.size()-1);
118 
119  //Then the channel names
120  if((one.left(3) == "MEG") && (two.left(3) == "MEG") && (one.left(one.size()-1) == two.left(two.size()-1)) && ((one.right(1)=="2" && two.right(1)=="3") || (one.right(1)=="3" && two.right(1)=="2"))) {
121  pairs(npair,0) = (int) i;
122  pairs(npair,1) = i+1;
123  ++npair;
124  ++i;
125  }
126  }
127  }
128 
129 
130  printf("\nComputing the amplitudes");
131  if(do_baseline) {
132  printf("(Baseline = %7.1f ... %7.1f ms)',1000*bmin,1000*bmax)",1000*bmin,1000*bmax);
133  }
134  printf("...");
135 
136  for(qint32 i=0; i < p_FiffEvokedSet.evoked.size()-1; ++i) {
137  if(b2>b1) {
138  b1 = (p_FiffEvokedSet.info.sfreq*bmin) - p_FiffEvokedSet.evoked[i].first;
139  b2 = (p_FiffEvokedSet.info.sfreq*bmax) - p_FiffEvokedSet.evoked[i].last;
140  if(b1 < 1) b1 = 1;
141  if(b2 > p_FiffEvokedSet.evoked[i].data.cols()) b2 = p_FiffEvokedSet.evoked[i].data.cols();
142  }
143  else {
144  b1 = 1;
145  b2 = 1;
146  }
147 
148  //go through all pairs
149  qint16 p0,p1;
150  ArrayXd tmparray;
151 
152  for(qint32 p=0; p < npair; ++p) {
153  p0 = pairs(p,0);
154  p1 = pairs(p,1);
155 
156  if(b2 > b1) {
157  Matrix<double,1,Dynamic> tmpbase1;
158  Matrix<double,1,Dynamic> tmpbase2;
159 
160  tmpbase1 = p_FiffEvokedSet.evoked[i].data.block(pairs(p,0),(b2-b1),1,p_FiffEvokedSet.evoked[i].data.cols());
161  base1 = tmpbase1.sum()/tmpbase1.rows();
162  tmpbase2 = p_FiffEvokedSet.evoked[i].data.block(pairs(p,1),(b2-b1),1,p_FiffEvokedSet.evoked[i].data.cols());
163  base2 = tmpbase2.sum()/tmpbase2.rows();
164 
165  tmparray = ((p_FiffEvokedSet.evoked[i].data.row(p0).array()-base1).square()) + ((p_FiffEvokedSet.evoked[i].data.row(p1).array()-base2).square()).square();
166  p_FiffEvokedSet.evoked[i].data.row(p0) = tmparray.matrix();
167  }
168  else {
169  tmparray = ((p_FiffEvokedSet.evoked[i].data.row(p0).array()).square()) + ((p_FiffEvokedSet.evoked[i].data.row(p1).array()).square());
170  p_FiffEvokedSet.evoked[i].data.row(p0) = tmparray.matrix();
171  }
172  }
173  printf(".");
174  }
175  printf("[done]\n");
176 
177  //Compose the selection name list
178  for(qint16 i=0; i < npair; ++i) {
179  ch_sel_names.append(p_FiffEvokedSet.info.ch_names.at(i));
180  }
181 
182  //Omit MEG channels but include others
183  qint16 k = npair;
184  for(qint16 p=0; p < p_FiffEvokedSet.info.nchan; ++p) {
185  if((p_FiffEvokedSet.info.channel_type(p) == "grad") || (p_FiffEvokedSet.info.channel_type(p) == "mag")) {
186  ++k;
187  ch_sel_names.append(p_FiffEvokedSet.info.ch_names.at(p));
188  }
189  }
190 
191  //Modify the bad channel list
192  if(!p_FiffEvokedSet.info.bads.isEmpty()) {
193  QString one,two;
194 
195  for(qint32 i=0; i < npair; ++i) {
196  one = p_FiffEvokedSet.info.ch_names.at(pairs(i,0));
197  two = p_FiffEvokedSet.info.ch_names.at(pairs(i,1));
198 
199  //If one channel of the planar gradiometer is marked bad, add the other to the bad channel list
200  if(!p_FiffEvokedSet.info.bads.contains(one) && p_FiffEvokedSet.info.bads.contains(two))
201  p_FiffEvokedSet.info.bads.append(two);
202  if(p_FiffEvokedSet.info.bads.contains(one) && !p_FiffEvokedSet.info.bads.contains(two))
203  p_FiffEvokedSet.info.bads.append(one);
204  }
205  }
206 
207  //Do the picking
208  p_FiffEvokedSet.pick_channels(ch_sel_names);
209 
210  //Optionally write an output file
211  //ToDo: implement MNE root function fiff_write_evoked
212 
213  return a.exec();
214 }
215 
216 //*************************************************************************************************************
217 //=============================================================================================================
218 // STATIC DEFINITIONS
219 //=============================================================================================================
Definition: fiff.h:98
evoked data set
FIFF class declaration, which provides static wrapper functions to stay consistent with mne matlab to...