v2.0.0
Loading...
Searching...
No Matches
frequencyspectrummodel.cpp
Go to the documentation of this file.
1//=============================================================================================================
14
15//=============================================================================================================
16// INCLUDES
17//=============================================================================================================
18
20
21#include <fiff/fiff_info.h>
22
23//=============================================================================================================
24// QT INCLUDES
25//=============================================================================================================
26
27//=============================================================================================================
28// EIGEN INCLUDES
29//=============================================================================================================
30
31//=============================================================================================================
32// USED NAMESPACES
33//=============================================================================================================
34
35using namespace DISPLIB;
36using namespace FIFFLIB;
37using namespace Eigen;
38
39//=============================================================================================================
40// DEFINE MEMBER METHODS
41//=============================================================================================================
42
44: QAbstractTableModel(parent)
45, m_fSps(1024.0f)
46, m_iT(10)
47, m_iLowerFrqIdx(0)
48, m_iUpperFrqIdx(0)
49, m_iScaleType(0)
50, m_bIsFreezed(false)
51, m_bInitialized(false)
52{
53}
54
55//=============================================================================================================
56
57int FrequencySpectrumModel::rowCount(const QModelIndex& /*parent*/) const
58{
59 if (!m_qMapIdxRowSelection.empty())
60 return m_qMapIdxRowSelection.size();
61 else
62 return 0;
63}
64
65//=============================================================================================================
66
67int FrequencySpectrumModel::columnCount(const QModelIndex& /*parent*/) const
68{
69 return 2;
70}
71
72//=============================================================================================================
73
74QVariant FrequencySpectrumModel::data(const QModelIndex& index, int role) const
75{
76 if (role != Qt::DisplayRole && role != Qt::BackgroundRole)
77 return QVariant();
78
79 if (index.isValid()) {
80 qint32 r = m_qMapIdxRowSelection[index.row()];
81
82 //******** first column (chname) ********
83 if (index.column() == 0 && role == Qt::DisplayRole)
84 if (m_pFiffInfo)
85 return QVariant(m_pFiffInfo->chs[r].ch_name);
86
87 //******** second column (data plot) ********
88 if (index.column() == 1) {
89 QVariant v;
90
91 switch (role) {
92 case Qt::DisplayRole: {
93 //pack all adjacent (after reload) RowVectorPairs into a QList
94 const MatrixXd& data = m_bIsFreezed ? m_dataCurrentFreeze : m_dataCurrent;
95 // Rows exist from setInfo() on; the spectrum only once data has arrived
96 if (r < 0 || r >= data.rows()) {
97 return QVariant();
98 }
99 v.setValue(RowVectorXd(data.row(r)));
100 return v;
101 }
102 case Qt::BackgroundRole: {
103 // if(m_fiffInfo.bads.contains(m_chInfolist[row].ch_name)) {
104 // QBrush brush;
105 // brush.setStyle(Qt::SolidPattern);
106 // // qDebug() << m_chInfolist[row].ch_name << "is marked as bad, index:" << row;
107 // brush.setColor(Qt::red);
108 // return QVariant(brush);
109 // }
110 // else
111 return QVariant();
112 }
113 } // end role switch
114 } // end column check
115
116 } // end index.valid() check
117
118 return QVariant();
119}
120
121//=============================================================================================================
122
123QVariant FrequencySpectrumModel::headerData(int section, Qt::Orientation orientation, int role) const
124{
125 if (role != Qt::DisplayRole && role != Qt::TextAlignmentRole)
126 return QVariant();
127
128 if (orientation == Qt::Horizontal) {
129 switch (section) {
130 case 0: //chname column
131 return QVariant();
132 case 1: //data plot column
133 switch (role) {
134 case Qt::DisplayRole:
135 return QVariant("data plot");
136 case Qt::TextAlignmentRole:
137 return QVariant(Qt::AlignLeft);
138 }
139 return QVariant("data plot");
140 }
141 } else if (orientation == Qt::Vertical) {
142 QModelIndex chname = createIndex(section, 0);
143 switch (role) {
144 case Qt::DisplayRole:
145 return QVariant(data(chname).toString());
146 }
147 }
148
149 return QVariant();
150}
151
152//=============================================================================================================
153
155{
156 beginResetModel();
157 m_pFiffInfo = info;
158 endResetModel();
159
161}
162
163//=============================================================================================================
164
166{
167 m_iScaleType = ScaleType;
168}
169
170//=============================================================================================================
171
173{
174 m_dataCurrent = data;
175
176 if (m_vecFreqScale.size() != m_dataCurrent.cols() && m_pFiffInfo) {
177 double freqRes = (m_pFiffInfo->sfreq / 2) / m_dataCurrent.cols();
178 double k = 1.0;
179 m_vecFreqScale.resize(1, m_dataCurrent.cols());
180
181 double currFreq = 0;
182 for (qint32 i = 0; i < m_dataCurrent.cols(); ++i) {
183 if (m_iScaleType) //log
184 m_vecFreqScale[i] = log10(currFreq + k);
185 else // normal
186 m_vecFreqScale[i] = currFreq;
187
188 currFreq += freqRes;
189 }
190
191 double max = m_vecFreqScale.maxCoeff();
192 m_vecFreqScale /= max;
193
194 m_vecFreqScaleBound = m_vecFreqScale;
195 m_iLowerFrqIdx = 0;
196 m_iUpperFrqIdx = m_vecFreqScale.size() - 1;
197
198 m_bInitialized = true;
199 }
200
201 //Update data content
202 QModelIndex topLeft = this->index(0, 1);
203 QModelIndex bottomRight = this->index(m_dataCurrent.rows() - 1, 1);
204 QVector<int> roles;
205 roles << Qt::DisplayRole;
206 emit dataChanged(topLeft, bottomRight, roles);
207}
208
209//=============================================================================================================
210
211void FrequencySpectrumModel::selectRows(const QList<qint32>& selection)
212{
213 beginResetModel();
214
215 m_qMapIdxRowSelection.clear();
216
217 qint32 count = 0;
218 for (qint32 i = 0; i < selection.size(); ++i) {
219 if (selection[i] < m_pFiffInfo->chs.size()) {
220 m_qMapIdxRowSelection.insert(count, selection[i]);
221 ++count;
222 }
223 }
224
225 emit newSelection(selection);
226
227 endResetModel();
228}
229
230//=============================================================================================================
231
233{
234 beginResetModel();
235
236 m_qMapIdxRowSelection.clear();
237
238 for (qint32 i = 0; i < m_pFiffInfo->chs.size(); ++i)
239 m_qMapIdxRowSelection.insert(i, i);
240
241 endResetModel();
242}
243
244//=============================================================================================================
245
246void FrequencySpectrumModel::toggleFreeze(const QModelIndex& index)
247{
248 Q_UNUSED(index);
249
250 m_bIsFreezed = !m_bIsFreezed;
251
252 if (m_bIsFreezed)
253 m_dataCurrentFreeze = m_dataCurrent;
254
255 //Update data content
256 QModelIndex topLeft = this->index(0, 1);
257 QModelIndex bottomRight = this->index(m_dataCurrent.rows() - 1, 1);
258 QVector<int> roles;
259 roles << Qt::DisplayRole;
260 emit dataChanged(topLeft, bottomRight, roles);
261}
262
263//=============================================================================================================
264
265void FrequencySpectrumModel::setBoundaries(float fLowerFrqBound, float fUpperFrqBound)
266{
267 if (!m_bInitialized) {
268 return;
269 }
270
271 beginResetModel();
272
273 double nf = m_pFiffInfo->sfreq / 2;
274
275 m_iLowerFrqIdx = 0;
276 m_iUpperFrqIdx = m_vecFreqScale.size() - 1;
277
278 //find boundaries
279 for (qint32 i = 0; i < m_vecFreqScale.size(); ++i) {
280 float val = m_vecFreqScale[i] * nf;
281 if (val < fLowerFrqBound) {
282 m_iLowerFrqIdx = i;
283 }
284
285 if (val > fUpperFrqBound) {
286 m_iUpperFrqIdx = i;
287 break;
288 }
289 }
290
291 // scale it new
292 m_vecFreqScaleBound = m_vecFreqScale;
293 for (qint32 i = 0; i < m_vecFreqScaleBound.size(); ++i) {
294 m_vecFreqScaleBound[i] = (m_vecFreqScaleBound[i] - m_vecFreqScale[m_iLowerFrqIdx]) / (m_vecFreqScale[m_iUpperFrqIdx] - m_vecFreqScale[m_iLowerFrqIdx]);
295 }
296
297 endResetModel();
298}
Full FIFF measurement metadata: everything from FIFFB_MEAS / FIFFB_MEAS_INFO needed to interpret a re...
QAbstractTableModel storing per-channel FFT magnitudes for DISPLIB::SpectrumView.
FIFF file I/O, in-memory data structures and high-level readers/writers.
2-D display widgets and visualisation helpers (charts, topography, colour maps).
void newSelection(QList< qint32 > selection)
void setBoundaries(float fLowerFrqBound, float fUpperFrqBound)
void selectRows(const QList< qint32 > &selection)
void addData(const Eigen::MatrixXd &data)
void toggleFreeze(const QModelIndex &index)
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
void setInfo(QSharedPointer< FIFFLIB::FiffInfo > &info)
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
QSharedPointer< FiffInfo > SPtr
Definition fiff_info.h:92