v2.0.0
Loading...
Searching...
No Matches
mne_meas_data_set.cpp
Go to the documentation of this file.
1//=============================================================================================================
15
16//=============================================================================================================
17// INCLUDES
18//=============================================================================================================
19
20#include "mne_meas_data_set.h"
21
22#include <fiff/fiff_file.h>
23
24#include <cmath>
25
26//=============================================================================================================
27// USED NAMESPACES
28//=============================================================================================================
29
30using namespace Eigen;
31using namespace MNELIB;
32
33//=============================================================================================================
34// DEFINE MEMBER METHODS
35//=============================================================================================================
36
38: first(0)
39, np(0)
40, nave(1)
42, tmin(0.0f)
43, tstep(0.0f)
44{
45}
46
47//=============================================================================================================
48
52
53//=============================================================================================================
54
55int MNEMeasDataSet::getValuesAtTime(float time, float integ, int nch, bool use_abs, float* value) const
56{
57 constexpr float EPS = 0.05f;
58 const float sfreq = 1.0f / tstep;
59
60 for (int ch = 0; ch < nch; ch++) {
61 float sum;
62 if (std::fabs(sfreq * integ) < EPS) {
63 /* Single-sample case */
64 float s1 = sfreq * (time - tmin);
65 int n1 = static_cast<int>(std::floor(s1));
66 float f1 = 1.0f + n1 - s1;
67 if (n1 < 0 || n1 > np - 1) {
68 qWarning("Sample value out of range %d (0..%d)", n1, np - 1);
69 return -1;
70 }
71 if (n1 == np - 1) {
72 if (std::fabs(f1 - 1.0f) < 1e-3f)
73 f1 = 1.0f;
74 }
75 if (f1 < 1.0f && n1 > np - 2) {
76 qWarning("Sample value out of range %d (0..%d) %.4f", n1, np - 1, f1);
77 return -1;
78 }
79 if (f1 < 1.0f) {
80 if (use_abs)
81 sum = f1 * std::fabs(data(n1, ch)) + (1.0f - f1) * std::fabs(data(n1 + 1, ch));
82 else
83 sum = f1 * data(n1, ch) + (1.0f - f1) * data(n1 + 1, ch);
84 } else {
85 sum = use_abs ? std::fabs(data(n1, ch)) : data(n1, ch);
86 }
87 } else {
88 /* Multiple samples */
89 float s1 = sfreq * (time - 0.5f * integ - tmin);
90 float s2 = sfreq * (time + 0.5f * integ - tmin);
91 int n1 = static_cast<int>(std::ceil(s1));
92 int n2 = static_cast<int>(std::floor(s2));
93 if (n2 < n1) {
94 /* Within one sample interval */
95 n1 = static_cast<int>(std::floor(s1));
96 if (n1 < 0 || n1 > np - 2)
97 return -1;
98 float f1 = s1 - n1;
99 float f2 = s2 - n1;
100 if (use_abs)
101 sum = 0.5f * ((f1 + f2) * std::fabs(data(n1 + 1, ch)) + (2.0f - f1 - f2) * std::fabs(data(n1, ch)));
102 else
103 sum = 0.5f * ((f1 + f2) * data(n1 + 1, ch) + (2.0f - f1 - f2) * data(n1, ch));
104 } else {
105 float f1 = n1 - s1;
106 float f2 = s2 - n2;
107 if (n1 < 0 || n1 > np - 1) {
108 qWarning("Sample value out of range %d (0..%d)", n1, np - 1);
109 return -1;
110 }
111 if (n2 < 0 || n2 > np - 1) {
112 qWarning("Sample value out of range %d (0..%d)", n2, np - 1);
113 return -1;
114 }
115 if (f1 != 0.0f && n1 < 1)
116 return -1;
117 if (f2 != 0.0f && n2 > np - 2)
118 return -1;
119 sum = 0.0f;
120 float width = 0.0f;
121 if (n2 > n1) {
122 if (use_abs) {
123 sum = 0.5f * std::fabs(data(n1, ch));
124 for (int k = n1 + 1; k < n2; k++)
125 sum += std::fabs(data(k, ch));
126 sum += 0.5f * std::fabs(data(n2, ch));
127 } else {
128 sum = 0.5f * data(n1, ch);
129 for (int k = n1 + 1; k < n2; k++)
130 sum += data(k, ch);
131 sum += 0.5f * data(n2, ch);
132 }
133 width = static_cast<float>(n2 - n1);
134 }
135 if (use_abs) {
136 if (f1 != 0.0f)
137 sum += 0.5f * f1 * (f1 * std::fabs(data(n1 - 1, ch)) + (2.0f - f1) * std::fabs(data(n1, ch)));
138 if (f2 != 0.0f)
139 sum += 0.5f * f2 * (f2 * std::fabs(data(n2 + 1, ch)) + (2.0f - f2) * std::fabs(data(n2, ch)));
140 } else {
141 if (f1 != 0.0f)
142 sum += 0.5f * f1 * (f1 * data(n1 - 1, ch) + (2.0f - f1) * data(n1, ch));
143 if (f2 != 0.0f)
144 sum += 0.5f * f2 * (f2 * data(n2 + 1, ch) + (2.0f - f2) * data(n2, ch));
145 }
146 width += f1 + f2;
147 sum /= width;
148 }
149 }
150 value[ch] = sum;
151 }
152 return 0;
153}
154
155//=============================================================================================================
156
157int MNEMeasDataSet::getValuesFromChannelData(float time, float integ, float** data, int nsamp, int nch,
158 float tmin, float sfreq, bool use_abs, float* value)
159{
160 constexpr float EPS = 0.05f;
161
162 for (int ch = 0; ch < nch; ch++) {
163 float sum;
164 if (std::fabs(sfreq * integ) < EPS) {
165 float s1 = sfreq * (time - tmin);
166 int n1 = static_cast<int>(std::floor(s1));
167 float f1 = 1.0f + n1 - s1;
168 if (n1 < 0 || n1 > nsamp - 1)
169 return -1;
170 if (f1 < 1.0f && n1 > nsamp - 2)
171 return -1;
172 if (f1 < 1.0f) {
173 if (use_abs)
174 sum = f1 * std::fabs(data[ch][n1]) + (1.0f - f1) * std::fabs(data[ch][n1 + 1]);
175 else
176 sum = f1 * data[ch][n1] + (1.0f - f1) * data[ch][n1 + 1];
177 } else {
178 sum = use_abs ? std::fabs(data[ch][n1]) : data[ch][n1];
179 }
180 } else {
181 float s1 = sfreq * (time - 0.5f * integ - tmin);
182 float s2 = sfreq * (time + 0.5f * integ - tmin);
183 int n1 = static_cast<int>(std::ceil(s1));
184 int n2 = static_cast<int>(std::floor(s2));
185 if (n2 < n1) {
186 n1 = static_cast<int>(std::floor(s1));
187 if (n1 < 0 || n1 > nsamp - 2)
188 return -1;
189 float f1 = s1 - n1;
190 float f2 = s2 - n1;
191 if (use_abs)
192 sum = 0.5f * ((f1 + f2) * std::fabs(data[ch][n1 + 1]) + (2.0f - f1 - f2) * std::fabs(data[ch][n1]));
193 else
194 sum = 0.5f * ((f1 + f2) * data[ch][n1 + 1] + (2.0f - f1 - f2) * data[ch][n1]);
195 } else {
196 float f1 = n1 - s1;
197 float f2 = s2 - n2;
198 if (n1 < 0 || n1 > nsamp - 1 || n2 < 0 || n2 > nsamp - 1)
199 return -1;
200 if (f1 != 0.0f && n1 < 1)
201 return -1;
202 if (f2 != 0.0f && n2 > nsamp - 2)
203 return -1;
204 sum = 0.0f;
205 float width = 0.0f;
206 if (n2 > n1) {
207 if (use_abs) {
208 sum = 0.5f * std::fabs(data[ch][n1]);
209 for (int k = n1 + 1; k < n2; k++)
210 sum += std::fabs(data[ch][k]);
211 sum += 0.5f * std::fabs(data[ch][n2]);
212 } else {
213 sum = 0.5f * data[ch][n1];
214 for (int k = n1 + 1; k < n2; k++)
215 sum += data[ch][k];
216 sum += 0.5f * data[ch][n2];
217 }
218 width = static_cast<float>(n2 - n1);
219 }
220 if (use_abs) {
221 if (f1 != 0.0f)
222 sum += 0.5f * f1 * (f1 * std::fabs(data[ch][n1 - 1]) + (2.0f - f1) * std::fabs(data[ch][n1]));
223 if (f2 != 0.0f)
224 sum += 0.5f * f2 * (f2 * std::fabs(data[ch][n2 + 1]) + (2.0f - f2) * std::fabs(data[ch][n2]));
225 } else {
226 if (f1 != 0.0f)
227 sum += 0.5f * f1 * (f1 * data[ch][n1 - 1] + (2.0f - f1) * data[ch][n1]);
228 if (f2 != 0.0f)
229 sum += 0.5f * f2 * (f2 * data[ch][n2 + 1] + (2.0f - f2) * data[ch][n2]);
230 }
231 width += f1 + f2;
232 sum /= width;
233 }
234 }
235 value[ch] = sum;
236 }
237 return 0;
238}
FIFF tag-kind, block-kind and type-code numerical definitions, authoritative for FIFFLIB.
constexpr auto FIFFV_ASPECT_AVERAGE
Definition fiff_file.h:429
constexpr double EPS
One condition / averaging slice within a legacy MNELIB::MNEMeasData.
Core MNE data structures (source spaces, source estimates, hemispheres).
static int getValuesFromChannelData(float time, float integ, float **data, int nsamp, int nch, float tmin, float sfreq, bool use_abs, float *value)
~MNEMeasDataSet()
Destroys the measurement data set and frees all owned C-style arrays.
MNEMeasDataSet()
Constructs an empty measurement data set.
int getValuesAtTime(float time, float integ, int nch, bool use_abs, float *value) const