-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalysis-tools.cpp
More file actions
696 lines (615 loc) · 22.1 KB
/
analysis-tools.cpp
File metadata and controls
696 lines (615 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/*
Copyright (C) 2015 Georgia Institute of Technology
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* HDF viewer and analysis tools
*/
#include <QtGlobal>
#include <QFileInfo>
#include <algorithm>
#include <main_window.h>
#include <qwt_plot_renderer.h>
#include "analysis-tools.h"
#include <time.h>
#include <gsl/gsl_math.h>
#include <iostream>
namespace HdfViewerUtils {
herr_t op_func(hid_t, const char*, const H5O_info_t*, void*);
QTreeWidget *treeViewer;
QTreeWidgetItem *treeParent;
QTreeWidgetItem *treeChild1;
QTreeWidgetItem *treeChild2;
QString currentTrial, currentGroup;
int currentTrialFlag;
int firstChannelSelected;
double *data_buffer, *channel_data, *time_buffer, *period_buffer, *fft_output_y, *fft_output_x;
complex *fft_input, *fft_buffer;
};
using namespace HdfViewerUtils;
extern "C" Plugin::Object *createRTXIPlugin(void) {
return new HdfViewer();
}
static DefaultGUIModel::variable_t vars[] = {
};
static size_t num_vars = sizeof(vars) / sizeof(DefaultGUIModel::variable_t);
HdfViewer::HdfViewer(void) : DefaultGUIModel("Analysis Tools", ::vars, ::num_vars) {
setWhatsThis("<p><b>Analysis Tools</b></p><p>View HDF data recorded by the data recorded. Simply open the file in the File Control Menu and click specific channels in the HDF5 Viewer list. Under 'Plotting Options,' you can change from view from time series to an FFT. To zoom in to the plot, use the mouse to click-and-drag an area. Plots can be saved to PDF (via the Plot button), and data used in the plot (both x and y axis) can be saved to a text file (via the Export button)</p>");
initParameters();
DefaultGUIModel::createGUI(vars, num_vars); // this is required to create the GUI
customizeGUI();
update(INIT);
refresh(); // this is required to update the GUI with parameter and state values
QTimer::singleShot(0, this, SLOT(resizeMe()));
}
HdfViewer::~HdfViewer(void)
{
if(file_id) {
H5Fclose(file_id);
file_id = 0;
}
freePlotBuffers();
}
void HdfViewer::execute(void) {
return;
}
void HdfViewer::update(DefaultGUIModel::update_flags_t flag) {
switch (flag) {
case INIT:
break;
case MODIFY:
break;
case UNPAUSE:
break;
case PAUSE:
break;
case PERIOD:
break;
default:
break;
}
};
void HdfViewer::initParameters() {
fwrChecked = 0;
file_id = 0;
data_buffer = NULL;
channel_data = NULL;
time_buffer = NULL;
period_buffer = NULL;
dataset_id = 0;
plot_mode = TIMESERIES;
window_shape = RECT;
Kalpha = 1.5;
Calpha = 70;
}
void HdfViewer::customizeGUI(void) {
QGridLayout *customlayout = DefaultGUIModel::getLayout();
customlayout->itemAtPosition(1,0)->widget()->setVisible(false);
customlayout->itemAtPosition(10,0)->widget()->setVisible(false);
customlayout->setColumnStretch(0,0);
customlayout->setColumnStretch(1,1);
// File control
QVBoxLayout *fileColumnLayout = new QVBoxLayout;
QGroupBox *fileBox = new QGroupBox(tr("File Control"));
QHBoxLayout *fileLayout = new QHBoxLayout;
fileBox->setLayout(fileLayout);
fileNameEdit = new QLineEdit;
fileNameEdit->setReadOnly(true);
fileLayout->addWidget(fileNameEdit);
QPushButton *fileChangeButton = new QPushButton("Open");
fileLayout->addWidget(fileChangeButton);
QObject::connect(fileChangeButton,SIGNAL(released(void)),this,SLOT(changeDataFile(void)));
fileColumnLayout->addWidget(fileBox);
// Plot controls
QVBoxLayout *plotColumnLayout = new QVBoxLayout;
customlayout->addLayout(plotColumnLayout, 0, 1);
plotControls = new QGroupBox("Plot Controls");
QHBoxLayout *plotControlsLayout = new QHBoxLayout;
plotControls->setLayout(plotControlsLayout);
resetPlotButton = new QPushButton("Reset");
QObject::connect(resetPlotButton, SIGNAL(clicked()), this, SLOT(resetAxes()));
savePlotButton = new QPushButton("Save");
QObject::connect(savePlotButton, SIGNAL(clicked()), this, SLOT(screenshot()));
savePlotButton->setToolTip("Save screenshot of the plot");
exportSeriesButton = new QPushButton("Export");
QObject::connect(exportSeriesButton, SIGNAL(clicked()), this, SLOT(exportData()));
plotControlsLayout->addWidget(resetPlotButton);
plotControlsLayout->addSpacerItem(new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::Minimum));
plotControlsLayout->addWidget(savePlotButton);
plotControlsLayout->addWidget(exportSeriesButton);
plotColumnLayout->addWidget(plotControls);
plotControls->setEnabled(false);
// Put plot under plot controls
omniplot = new BasicPlot(this);
/*
* xAxisTitle.setText("X Axis");
* yAxisTitle.setText("Y Axis");
* omniplot->setAxisTitle(QwtPlot::xBottom, xAxisTitle);
* omniplot->setAxisTitle(QwtPlot::yLeft, yAxisTitle);
*/
plotColumnLayout->addWidget(omniplot);
tscurve = new QwtPlotCurve;
tscurve->setPen(Qt::white);
fftcurve = new QwtPlotCurve;
fftcurve->setPen(Qt::white);
// Plot options / file controls
plotOptions = new QGroupBox(tr("Plotting Options"));
QGridLayout *plotOptionsLayout = new QGridLayout;
plotOptions->setLayout(plotOptionsLayout);
QLabel *plotTypeLabel = new QLabel("Plot Type");
plotType = new QComboBox;
plotType->insertItem(1, "Time Series");
plotType->insertItem(2, "Scatter");
plotType->insertItem(3, "FFT");
QObject::connect(plotType, SIGNAL(currentIndexChanged(int)), this, SLOT(updatePlotMode(int)));
plotOptionsLayout->addWidget(plotTypeLabel, 1, 0);
plotOptionsLayout->addWidget(plotType, 1, 1);
QVBoxLayout *plotOptionsVerticalLayout = new QVBoxLayout;
plotOptionsButtons = new QButtonGroup;
plotOptionsButtons->setExclusive(false);
FWRCheckBox = new QCheckBox("TS: Full wave rectify");
plotOptionsVerticalLayout->addWidget(FWRCheckBox);
plotOptionsButtons->addButton(FWRCheckBox);
FWRCheckBox->setChecked(false);
QObject::connect(FWRCheckBox, SIGNAL(toggled(bool)),this,SLOT(toggleFWR(bool)));
FWRCheckBox->setToolTip("Enable full wave rectification of time series plot");
plotOptionsLayout->addLayout(plotOptionsVerticalLayout, 2, 0, 1, 2);
QLabel *windowLabel = new QLabel("FFT window shape:");
windowShape = new QComboBox;
windowShape->insertItem(1, "Rectangular");
windowShape->insertItem(2, "Triangular (Bartlett)");
windowShape->insertItem(3, "Hamming");
windowShape->insertItem(4, "Hann");
windowShape->insertItem(5, "Chebyshev");
windowShape->insertItem(6, "Kaiser");
QObject::connect(windowShape, SIGNAL(activated(int)), this, SLOT(updateWindow(int)));
windowShape->setToolTip("Choose a window to apply for the FFT plot. For no window, choose Rectangular.");
plotOptionsLayout->addWidget(windowLabel, 3, 0);
plotOptionsLayout->addWidget(windowShape, 3, 1);
QLabel *kalphaLabel = new QLabel("Kaiser Alpha");
plotOptionsLayout->addWidget(kalphaLabel, 4, 0);
QDoubleSpinBox *kalphaEdit = new QDoubleSpinBox(plotOptions);
kalphaEdit->setValue(Kalpha);
QObject::connect(kalphaEdit, SIGNAL(valueChanged(double)), this, SLOT(updateKalpha(double)));
kalphaEdit->setToolTip("Attenuation parameter for Kaiser window");
plotOptionsLayout->addWidget(kalphaEdit, 4, 1);
QLabel *calphaLabel = new QLabel("Chebyshev (dB)");
plotOptionsLayout->addWidget(calphaLabel, 5, 0);
calphaEdit = new QDoubleSpinBox(plotOptions);
calphaEdit->setValue(Calpha);
QObject::connect(calphaEdit, SIGNAL(valueChanged(double)), this, SLOT(updateCalpha(double)));
calphaEdit->setToolTip("Attenuation parameter for Chebyshev window");
plotOptionsLayout->addWidget(calphaEdit, 5, 1);
fileColumnLayout->addWidget(plotOptions);
plotOptions->setEnabled(false);
// HDF5 viewer
treeViewer = new QTreeWidget;
treeViewer->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
treeViewer->setHeaderLabels(QStringList("HDF Tree"));
QObject::connect(treeViewer, SIGNAL(clicked(QModelIndex)), this, SLOT(changeChannel(QModelIndex)));
fileColumnLayout->addWidget(treeViewer);
customlayout->addLayout(fileColumnLayout, 0, 0);
setLayout(customlayout);
QObject::connect(this, SIGNAL(setPlotRange(double,double,double,double)), omniplot, SLOT(setAxes(double,double,double,double)));
}
void HdfViewer::exportData() {
QFileDialog *fd = new QFileDialog(this);
fd->setFileMode(QFileDialog::AnyFile);
fd->setViewMode(QFileDialog::Detail);
fd->setDefaultSuffix("txt");
QString fileName = QFileDialog::getSaveFileName(
this, "Save the plotted data", "~/",
"Text Files (*.txt);;All Files (*.*)");
// If filename does not include .csp extension, add extension
if (!(fileName.endsWith(".txt"))) fileName.append(".txt");
// If filename exists, warn user
if (QFileInfo(fileName).exists() &&
QMessageBox::warning(
this, "File Exists", "Do you wish to overwrite " + fileName + "?",
QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
return; // Return if answer is no
// Save protocol to file
QFile file(fileName); // Open file
if (!file.open(
QIODevice::WriteOnly)) { // Open file, return error if unable to do so
QMessageBox::warning(
this, "Error", "Unable to save file: Please check folder permissions.");
return;
}
QTextStream stream(&file);
int n;
switch(plot_mode) {
case TIMESERIES:
n = tscurve->dataSize();
for (int i = 0; i < n; i++) {
stream << (double) time_buffer[i] << " " << (double) channel_data[i] << "\n";
}
break;
case SCATTER:
break;
case FFT:
n = fftcurve->dataSize();
for (int i = 0; i < n; i++) {
stream << (double) fft_output_x[i] << " " << (double) fft_output_y[i] << "\n";
}
break;
default:
break;
}
file.close();
}
void HdfViewer::changeChannel(QModelIndex id) {
getTrialData();
}
void HdfViewer::resetAxes(void) {
emit setPlotRange(xmin, xmax, ymin, ymax);
}
void HdfViewer::updateWindow(int index) {
window_shape = (window_t)index;
if (plot_mode == FFT) getTrialData();
}
void HdfViewer::updateKalpha(double KalphaInput) {
Kalpha = KalphaInput;
if (plot_mode == FFT) getTrialData();
}
void HdfViewer::updateCalpha(double CalphaInput) {
Calpha = CalphaInput;
if (plot_mode == FFT) getTrialData();
}
void HdfViewer::makeWindow(int num_points) {
switch (window_shape) {
case RECT: // rectangular
disc_window = new RectangularWindow(num_points);
break;
case TRI: // triangular
disc_window = new TriangularWindow(num_points, 1);
break;
case HAMM: // Hamming
disc_window = new HammingWindow(num_points);
break;
case HANN: // Hann
disc_window = new HannWindow(num_points, 1);
break;
case CHEBY: // Dolph-Chebyshev
disc_window = new DolphChebyWindow(num_points, Calpha);
break;
case KAISER:
disc_window = new KaiserWindow(num_points, Kalpha);
break;
} // end of switch on window_shape
}
void HdfViewer::screenshot() {
QwtPlotRenderer renderer;
renderer.exportTo(omniplot, "screenshot.pdf");
}
void HdfViewer::clearData() {
}
void HdfViewer::toggleFWR(bool fwrStatus) {
fwrChecked = fwrStatus;
if (plot_mode == FFT) getTrialData();
}
// TODO: may need to restore toggle functions to allow plots to be cleared when deselected
void HdfViewer::changeDataFile(void) {
QFileDialog fileDialog(this);
fileDialog.setFileMode(QFileDialog::AnyFile);
fileDialog.setWindowTitle("Select Data File");
QSettings userprefs;
userprefs.setPath(QSettings::NativeFormat, QSettings::SystemScope, "/usr/local/share/rtxi/");
fileDialog.setDirectory(userprefs.value("/dirs/data", getenv("HOME")).toString());
QStringList filterList;
filterList << "*.h5";
fileDialog.setNameFilters(filterList);
QStringList files;
QString filename;
if(fileDialog.exec()) {
closeFile(); // close previous file
treeViewer->clear();
files = fileDialog.selectedFiles();
filename = files[0];
fileNameEdit->setText(filename);
openFile(filename);
}
}
// TODO: populate HDF5, attribute, and parameter viewer contents
// enable any scatter/FFT specific options
int HdfViewer::openFile(QString &filename) {
herr_t status = -1;
currentTrialFlag = 0;
currentTrial = "";
firstChannelSelected = 0;
if (QFile::exists(filename)) {
file_id = H5Fopen(filename.toLatin1().constData(), H5F_ACC_RDONLY, H5P_DEFAULT);
// Iterate through file
status = H5Ovisit(file_id, H5_INDEX_NAME, H5_ITER_NATIVE, op_func, NULL);
if (!status) {
plotControls->setEnabled(true);
plotOptions->setEnabled(true);
getTrialData();
updatePlot();
}
else
closeFile();
}
return status;
}
// TODO: erase HDF5, attribute, and parameter viewer contents
// disable plot button and any scatter/FFT specific options
void HdfViewer::closeFile()
{
if(file_id) {
H5Fclose(file_id);
file_id = 0;
}
freePlotBuffers();
}
// TODO: clean up treeViewer -- no need to list full path for each parent/child
herr_t HdfViewerUtils::op_func(hid_t loc_id, const char *name, const H5O_info_t *info, void *operator_data) {
QString qName = QString(name);
//printf ("/");
if (name[0] == '.') {
//printf (" (Group)\n");
} else {
switch (info->type) {
case H5O_TYPE_GROUP:
//printf ("%s (Group)\n", name);
if (!qName.startsWith(currentTrial) || currentTrial == "") {
currentTrial = qName;
treeParent = new QTreeWidgetItem;
treeParent->setText(0, qName);
treeViewer->addTopLevelItem(treeParent);
} else if (qName.startsWith(currentTrial) && qName.endsWith("Asynchronous Data")) {
treeChild1 = new QTreeWidgetItem;
currentGroup = "Asynchronous Data";
treeChild1->setText(0, "Asynchronous Data");
treeParent->addChild(treeChild1);
} else if (qName.startsWith(currentTrial) && qName.endsWith("Synchronous Data")) {
treeChild1 = new QTreeWidgetItem;
currentGroup = "Synchronous Data";
treeChild1->setText(0, "Synchronous Data");
treeParent->addChild(treeChild1);
}
break;
case H5O_TYPE_DATASET:
//printf ("%s (Dataset)\n", name);
if (qName.startsWith(currentTrial + "/Asynchronous Data") && !qName.endsWith("Channel Data")) {
treeChild2 = new QTreeWidgetItem;
treeChild2->setText(0, qName.right(qName.length() - (currentTrial.length()+currentGroup.length()+2)));
treeChild1->addChild(treeChild2);
treeChild2->setToolTip(0, qName);
if (!firstChannelSelected) {
firstChannelSelected = 1;
treeViewer->setCurrentItem(treeChild2);
}
} else if (qName.startsWith(currentTrial + "/Synchronous Data") && !qName.endsWith("Channel Data")) {
treeChild2 = new QTreeWidgetItem;
treeChild2->setText(0, qName.right(qName.length() - (currentTrial.length()+currentGroup.length()+2)));
treeChild1->addChild(treeChild2);
treeChild2->setToolTip(0, qName);
if (!firstChannelSelected) {
firstChannelSelected = 1;
treeViewer->setCurrentItem(treeChild2);
}
}
// else statement -- add all remaining dataset contents to parameters
break;
case H5O_TYPE_NAMED_DATATYPE:
//printf ("%s (Datatype)\n", name);
break;
default:
//printf ("%s (Unknown)\n", name);
break;
}
}
return 0;
}
// TODO: think through error cases here (e.g. when one of the top-level groups
// are selected, etc.)
void HdfViewer::getTrialData() {
// TODO: check that current item is a dataset (and not a group), only
// open/plot if a dataset is selected (maybe display an warning otherwise?)
herr_t status;
hsize_t nrecords, ntrials, nchannels;
hid_t packettable_id, trial_id, period_id;
double channelDataSum = 0;
double channelDataMean;
// Check if selected channel is actually a channel (should have 0 children in
// the treeViewer)
if (treeViewer->currentItem()->childCount() != 0 || treeViewer->currentItem()->text(0) == "Asynchronous Data" || treeViewer->currentItem()->text(0) == "Synchronous Data") {
printf("getTrialData error: selected channel is not a dataset\n");
return;
}
// Reset data buffers if something is already plotted
freePlotBuffers();
// Get elements from GUI
QString selectedTrial = treeViewer->currentItem()->text(0);
QString channelNum = selectedTrial.split(" ").at(0);
int channelNumInt = channelNum.toInt();
QString trialToRead = treeViewer->currentItem()->parent()->parent()->text(0) + "/" +
treeViewer->currentItem()->parent()->text(0);
QString channelToRead = treeViewer->currentItem()->parent()->parent()->text(0) + "/" +
treeViewer->currentItem()->parent()->text(0) + "/Channel Data";
QString periodToRead = treeViewer->currentItem()->parent()->parent()->text(0) + "/Period (ns)";
QString trialLengthToRead = treeViewer->currentItem()->parent()->parent()->text(0) + "/Trial Length (ns)";
// Open packet table
packettable_id = H5PTopen(file_id, channelToRead.toLatin1().constData());
if(packettable_id < 0)
printf("Throw error - H5PTopen error %d\n", packettable_id);
// Get packet count
status = H5PTget_num_packets(packettable_id, &nrecords);
if(status < 0)
printf("Throw error - H5PTget_num_packets error %d\n", status);
// Get number of trials
status = H5Gget_num_objs(file_id, &ntrials);
if(status < 0)
printf("Throw error - H5Gget_num_objs %d\n", status);
// Get identifier for trial and group
printf("%s\n", trialToRead.toStdString().c_str());
trial_id = H5Gopen1(file_id, trialToRead.toLatin1().constData());
if(trial_id < 0)
printf("Throw error - H5Gopen1 %d\n", trial_id);
// Get number of channels from trial
// Returned number of objects includes "Channel Data" struct, so we subtract 1
status = H5Gget_num_objs(trial_id, &nchannels);
if(status < 0)
printf("Throw error - H5Gget_num_objs %d\n", status);
nchannels--;
// Initialize data buffer -- module will crash for large trials...
data_buffer = (double *)malloc(sizeof(double)*(int)(nrecords)*(int)nchannels);
channel_data = (double *)malloc(sizeof(double)*(int)(nrecords));
time_buffer = (double *)malloc(sizeof(double)*(int)(nrecords));
period_buffer = (double *)malloc(sizeof(double));
// Read data
status = H5PTread_packets(packettable_id, 0, (int)nrecords, data_buffer);
if(status < 0)
printf("Throw error - H5PTread_packets error %d\n", status);
// Build channel_data array -- pretty messy but seems unavoidable with the
// current Data Recorder structure. To eliminate a loop later on, find
// channelDataSum here if full wave rectification option is checked
if (fwrChecked) {
int j = 0;
for (int i = channelNumInt-1; i < (int)nrecords*(int)nchannels; i = i + (int)nchannels) {
channel_data[j] = data_buffer[i];
channelDataSum = channelDataSum + channel_data[j];
j++;
}
// find DC offset -- for long signals, it's approx. the mean
channelDataMean = channelDataSum / ((int)nrecords);
} else {
int j = 0;
for (int i = channelNumInt-1; i < (int)nrecords*(int)nchannels; i = i + nchannels) {
channel_data[j] = data_buffer[i];
j++;
}
}
// Build time vector
period_id = H5Dopen2(file_id, periodToRead.toLatin1().constData(), H5P_DEFAULT);
if(period_id < 0)
printf("Throw error - H5Dopen2 error %d\n", period_id);
status = H5Dread(period_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, period_buffer);
if(status < 0)
printf("Throw error - H5Dread error %d\n", status);
time_buffer[0] = 0;
for (int i = 1; i < (int)nrecords; i++) {
time_buffer[i] = time_buffer[i-1] + (*period_buffer / 1e9);
}
// FFT plot
// TODO: check if FFT plot is selected
double windowedChannelVal;
int fft_length = 2;
while (fft_length < (int)nrecords) {
fft_length = fft_length * 2;
}
fft_input = (complex *)malloc(sizeof(complex)*fft_length);
fft_buffer = (complex *)malloc(sizeof(complex)*fft_length);
fft_output_y = (double *)malloc(sizeof(double)*fft_length);
fft_output_x = (double *)malloc(sizeof(double)*fft_length);
makeWindow((int)nrecords);
for (int i = 0; i < fft_length; i++) {
if (i < (int)nrecords) {
windowedChannelVal = disc_window->GetDataWinCoeff(i) * channel_data[i];
fft_input[i] = complex(windowedChannelVal, 0.0);
} else {
fft_input[i] = 0; // pad the rest of the array with 0's
}
}
fft(fft_input, fft_buffer, fft_length);
for (int i = 0; i < fft_length; i++) {
fft_output_y[i] = fabs(real(fft_buffer[i]));
fft_output_x[i] = i / ((*period_buffer / 1e9) * fft_length);
}
// Full wave rectification
if (fwrChecked) {
// subtract DC offset from each value and take absolute value
for (int i = 0; i < (int)nrecords; i++) {
channel_data[i] = std::abs(channel_data[i] - channelDataMean);
}
}
// Plot
tscurve->setRawSamples(time_buffer, channel_data, (int)nrecords);
fftcurve->setRawSamples(fft_output_x, fft_output_y, fft_length);
updatePlot();
// Close identifiers
H5PTclose(packettable_id);
H5Gclose(trial_id);
H5Dclose(period_id);
}
// Temporary function for validating data access
void HdfViewer::updatePlot(void) {
tscurve->detach();
fftcurve->detach();
switch(plot_mode) {
case TIMESERIES:
tscurve->attach(omniplot);
break;
case SCATTER:
break;
case FFT:
fftcurve->attach(omniplot);
break;
default:
break;
}
omniplot->setAxisAutoScale(omniplot->yLeft, true);
omniplot->setAxisAutoScale(omniplot->xBottom, true);
omniplot->replot();
xmin = omniplot->axisScaleDiv(QwtPlot::xBottom).lowerBound();
xmax = omniplot->axisScaleDiv(QwtPlot::xBottom).upperBound();
ymin = omniplot->axisScaleDiv(QwtPlot::yLeft).lowerBound();
ymax = omniplot->axisScaleDiv(QwtPlot::yLeft).upperBound();
emit setPlotRange(xmin, xmax, ymin, ymax);
}
void HdfViewer::updatePlotMode(int mode) {
plot_mode = (plot_t)mode;
updatePlot();
}
// Temporary function for validating data access
void HdfViewer::dump_vals(double *data, hsize_t *ndims)
{
// Only printing first value out or else the printf will block
for(size_t i=0; i<10; i++)
printf("value is %f\n", data[i]);
}
void HdfViewer::freePlotBuffers()
{
if(data_buffer) {
free(data_buffer);
data_buffer = NULL;
}
if(channel_data) {
free(channel_data);
channel_data = NULL;
}
if(time_buffer) {
free(time_buffer);
time_buffer = NULL;
}
if(period_buffer) {
free(period_buffer);
period_buffer = NULL;
}
if(fft_buffer) {
free(fft_buffer);
fft_buffer = NULL;
}
if(fft_input) {
free(fft_input);
fft_input = NULL;
}
if(fft_output_x) {
free(fft_output_x);
fft_output_x = NULL;
}
if(fft_output_y) {
free(fft_output_y);
fft_output_y = NULL;
}
}