BioSignalPi  v2
convertecgtoibi.cpp
Go to the documentation of this file.
1 /*
2  * convertecgtoibi.cpp
3  *
4  * Created on: Nov 5, 2015
5  * Author: martin
6  */
7 
8 #include "libFiles/QRSDET.H"
9 #include "libFiles/QRSFILT.CPP"
10 
11 #include "convertecgtoibi.h"
12 #include <QThread>
13 #include <QString>
14 #include <QVector>
15 #include <QPointF>
16 #include <QFile>
17 #include <QTextStream>
18 #include <QRegularExpression>
19 #include <QDebug>
20 #include <iostream>
21 
22 
23 ConvertEcgToIbi::ConvertEcgToIbi(QString fileName, QObject *parent):
24 QThread( parent )
25 {
26  fname = fileName;
27 
28 
29 }
30 
31 /*
32  * Load the file with chosen filename and emit the signal sendFileData() when the file is read.
33  */
35 {
36  QFile myFile(fname);
37  if (myFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
38  time.start();
39  QTextStream ecgInfo(&myFile);
40  QVector<int > ecgVals;
41  QVector<double> timeData;
42  int iterations;
43  if (!ecgInfo.atEnd()) {
44  QString strVals = ecgInfo.readLine();
45  ecgInfo.readLine();
46  ecgInfo.readLine();
47  double tmp;
48  int i=0;
49  while (!ecgInfo.atEnd()) {
50 
51  strVals = ecgInfo.readLine();
52  QStringList strPieces = strVals.split(QRegularExpression("\\s+"));
53 
54 
55  if (strPieces.length()==4) {
56  tmp=strPieces[2].toDouble();
57  ecgVals.append((tmp*500));
58 
59  }
60  else if (strPieces.length()==3) {
61  tmp=strPieces[2].toDouble();
62  ecgVals.append((tmp*500));
63  }
64  else if (strPieces.length()==5){
65 
66  tmp=strPieces[2].toDouble();
67  ecgVals.append((tmp*500));
68  }
69  else {
70  std::cerr << "Wrong File" << std::endl;
71  return;
72  }
73  i++;
74  }
75  QVector<double> qrsPeaks;
76  extractRtoR(&ecgVals, &qrsPeaks);
77  qrsPeaks.takeFirst();// Remove the influense of the QRS-detectors learning period
78  qrsPeaks.takeFirst();// Remove the influense of the QRS-detectors learning period
79  qrsPeaks.takeFirst();// Remove the influense of the QRS-detectors learning period
80  tmp=0;
81  for (int i; i<qrsPeaks.length(); i++){
82  tmp=tmp+(qrsPeaks.at(i));
83  timeData.append(tmp);
84  }
85  if (qrsPeaks.length()>10){
86  emit sendFileData(qrsPeaks,timeData);
87  saveAsIbiFile(&qrsPeaks);
88  }
89  else
90  std::cerr << "Not enough R peaks detected" << std::endl;
91  qDebug("Time elapsed: %d ms", time.elapsed());
92 
93  }
94  ecgInfo.flush();
95  myFile.close();
96 
97 
98 
99  }
100 
101 }
102 
103 void ConvertEcgToIbi::extractRtoR(QVector<int>* input, QVector<double>* output) {
104 
105  output->append(QRSDet(input->takeFirst(),1));
106  double tmpQrs=0;
107  double count=0;
108  while (!input->empty()) {
109  tmpQrs=QRSDet(input->takeFirst(),0);
110  if (tmpQrs==0){
111  count=count+1000/sampRate;
112  }
113  else {
114  output->append((count+1000/sampRate)/1000);
115  count=0;
116  }
117 
118 
119 
120  }
121 }
122 void ConvertEcgToIbi::saveAsIbiFile(QVector<double>* input){
123  std::cerr << input->length() <<std::endl;
124  QFile outFile(fname + "_RR.txt");
125 
126  if (!outFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
127 
128  std::cerr << "Failed to open data file for write" << std::endl;
129  } else {
130  QTextStream out(&outFile);
131  double time=0,tmp=0;
132  qDebug() << "Writing " << input->length() << " samples to " << fname << "..." << endl;
133 
134  out <<"Length:" << "\t" << QString::number(input->length()) << "\n";
135  out <<"[s]" << "\t" << "[s]" << "\n";
136  while (!input->empty()){
137  tmp=input->takeFirst();
138  // tmp=tmp/1000;
139  out << QString::number(time, 'f', 3) << "\t"
140  << QString::number(tmp, 'f', 3) << "\n";
141  time+=tmp;
142  }
143 
144  std::cerr << "Samples saved in text format" << std::endl;
145  outFile.close();
146  }
147 }
148 
149 
150 
void sendFileData(QVector< double > qrsPeaks, QVector< double > timeData)
sendFileData
ConvertEcgToIbi(QString fileName, QObject *parent=NULL)
ConvertEcgToIbi.
int QRSDet(int, int)
QRSDet Function used for the QRS-detection.
virtual void run()