MNE-CPP  0.1.9
A Framework for Electrophysiology
filterio.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //=============================================================================================================
37 // INCLUDES
38 //=============================================================================================================
39 
40 #include "filterio.h"
41 
42 //=============================================================================================================
43 // QT INCLUDES
44 //=============================================================================================================
45 
46 #include <QFile>
47 #include <QDebug>
48 #include <QStringList>
49 
50 //=============================================================================================================
51 // USED NAMESPACES
52 //=============================================================================================================
53 
54 using namespace RTPROCESSINGLIB;
55 using namespace Eigen;
56 
57 //=============================================================================================================
58 // DEFINE MEMBER METHODS
59 //=============================================================================================================
60 
62 {
63 }
64 
65 //=============================================================================================================
66 
67 bool FilterIO::readFilter(QString path, FilterKernel &filter)
68 {
69  //Open .txt file
70  if(!path.contains(".txt"))
71  return false;
72 
73  QFile file(path);
74  if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
75  qDebug()<<"Error opening filter txt file for reading";
76  return false;
77  }
78 
79  //Start reading from file
80  QTextStream in(&file);
81  QVector<double> coefficientsTemp;
82 
83  while(!in.atEnd())
84  {
85  QString line = in.readLine();
86 
87  QStringList fields = line.split(QRegExp("\\s+"));
88 
89  //Delete last element if it is a blank character
90  if(fields.at(fields.size()-1) == "")
91  fields.removeLast();
92 
93  if(line.contains("#")) //Filter meta information commented areas in file
94  {
95  //Read filter sFreq
96  if(line.contains("sFreq") && fields.size()==2)
97  filter.setSamplingFrequency(fields.at(1).toDouble());
98 
99  //Read filter name
100  if(line.contains("name")) {
101  QString sFilterName;
102  for(int i=1; i<fields.size(); i++)
103  sFilterName.append(fields.at(i));
104  filter.setName(sFilterName);
105  }
106 
107  //Read the filter order
108  if(line.contains("order") && fields.size()==2)
109  filter.setFilterOrder(fields.at(1).toInt());
110 
111  //Read the filter type
112  if(line.contains("type") && fields.size()==2)
113  filter.setFilterType(FilterKernel::m_filterTypes.indexOf(FilterParameter(fields.at(1))));
114 
115  //Read the filter LPFreq
116  if(line.contains("LPFreq") && fields.size()==2)
117  filter.setLowpassFreq(fields.at(1).toDouble());
118 
119  //Read the filter HPFreq
120  if(line.contains("HPFreq") && fields.size()==2)
121  filter.setHighpassFreq(fields.at(1).toDouble());
122 
123  //Read the filter CenterFreq
124  if(line.contains("CenterFreq") && fields.size()==2)
125  filter.setCenterFrequency(fields.at(1).toDouble());
126 
127  //Read the filter DesignMethod
128  if(line.contains("DesignMethod") && fields.size()==2)
129  filter.setDesignMethod(FilterKernel::m_designMethods.indexOf(FilterParameter(fields.at(1))));
130 
131  } else // Read filter coefficients
132  coefficientsTemp.push_back(fields.join("").toDouble());
133  }
134  // Check if reading was successful and correct
135  if(filter.getFilterOrder() != coefficientsTemp.size()) {
136  filter.setFilterOrder(coefficientsTemp.size());
137  }
138 
139  RowVectorXd vecCoeff = RowVectorXd::Zero(coefficientsTemp.size());
140  for(int i=0; i < vecCoeff.cols(); i++) {
141  vecCoeff(i) = coefficientsTemp.at(i);
142  }
143  filter.setCoefficients(vecCoeff);
144 
145  file.close();
146 
147  return true;
148 }
149 
150 //=============================================================================================================
151 
152 bool FilterIO::writeFilter(const QString &path, const FilterKernel &filter)
153 {
154  // Open file dialog
155  if(!path.isEmpty())
156  {
157  QFile file(path);
158  if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
159  qDebug()<<"Error opening filter txt file for writing";
160  return false;
161  }
162 
163  //Write coefficients to file
164  QTextStream out(&file);
165 
166  out << "#sFreq " << filter.getSamplingFrequency() << "\n";
167  out << "#name " << filter.getName() << "\n";
168  out << "#type " << filter.getFilterType().getName() << "\n";
169  out << "#order " << filter.getFilterOrder() << "\n";
170  out << "#HPFreq " << filter.getHighpassFreq() << "\n";
171  out << "#LPFreq " << filter.getLowpassFreq() << "\n";
172  out << "#CenterFreq " << filter.getCenterFrequency() << "\n";
173  out << "#DesignMethod " << filter.getDesignMethod().getName() << "\n";
174 
175  for(int i = 0 ; i<filter.getCoefficients().cols() ;i++)
176  out << filter.getCoefficients()(i) << "\n";
177 
178  file.close();
179 
180  return true;
181  }
182 
183  qDebug()<<"Error Filter File path is empty";
184 
185  return false;
186 }
The FilterKernel class provides methods to create/design a FIR filter kernel.
Definition: filterkernel.h:132
static bool readFilter(QString path, FilterKernel &filter)
Definition: filterio.cpp:67
static QVector< FilterParameter > m_filterTypes
Definition: filterkernel.h:241
static QVector< FilterParameter > m_designMethods
Definition: filterkernel.h:240
The FilterParameter class.
Definition: filterkernel.h:83
FilterIO class declaration.
static bool writeFilter(const QString &path, const FilterKernel &filter)
Definition: filterio.cpp:152