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