-
Notifications
You must be signed in to change notification settings - Fork 1
/
iir-filter.cpp
351 lines (299 loc) · 12 KB
/
iir-filter.cpp
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
/*
Copyright (C) 2011 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/>.
*/
/*
* IIRfilter
* Computes IIR filter coefficients
*
*/
#include "iir-filter.h"
#include <math.h>
#include <algorithm>
#include <numeric>
#include <time.h>
#include <DSP/log2.h>
#include <sys/stat.h>
//create plug-in
extern "C" Plugin::Object *createRTXIPlugin(void){
return new IIRfilter(); // Change the name of the plug-in here
}
//set up parameters/inputs/outputs derived from defaultGUIModel, calls for initialization, creation, update, and refresh of GUI
static DefaultGUIModel::variable_t vars[] = {
{ "Input", "Input to Filter", DefaultGUIModel::INPUT, },
{ "Output", "Output of Filter", DefaultGUIModel::OUTPUT },
{ "Filter Order", "Filter Order", DefaultGUIModel::PARAMETER
| DefaultGUIModel::INTEGER, },
{ "Passband Ripple (dB)", "Passband Ripple (dB)", DefaultGUIModel::PARAMETER
| DefaultGUIModel::DOUBLE, },
{ "Passband Edge (Hz)", "Passband Edge (Hz)", DefaultGUIModel::PARAMETER
| DefaultGUIModel::DOUBLE, },
{ "Stopband Ripple (dB)", "Stopband Ripple (dB)", DefaultGUIModel::PARAMETER
| DefaultGUIModel::DOUBLE, },
{ "Stopband Edge (Hz)", "Stopband Edge (Hz)", DefaultGUIModel::PARAMETER
| DefaultGUIModel::DOUBLE, },
{ "Input quantizing factor", "Bits eg. 10, 12, 16",
DefaultGUIModel::PARAMETER | DefaultGUIModel::INTEGER, },
{ "Coefficients quantizing factor", "Bits eg. 10, 12, 16",
DefaultGUIModel::PARAMETER | DefaultGUIModel::INTEGER, },
};
static size_t num_vars = sizeof(vars) / sizeof(DefaultGUIModel::variable_t);
IIRfilter::IIRfilter(void) : DefaultGUIModel("IIR Filter", ::vars, ::num_vars) {
setWhatsThis(
"<p><b>IIR Filter:</b><br>This plugin computes filter coefficients for three types of IIR filters. "
"They require the following parameters: <br><br>"
"Butterworth: passband edge <br>"
"Chebyshev: passband ripple, passband edge, ripple bw_norm <br>"
"Elliptical: passband ripple, stopband ripple, passband edge, stopband edge <br><br>"
"Since this plug-in computes new filter coefficients whenever you change the parameters, you should not"
"change any settings during real-time.</p>");
initParameters();
DefaultGUIModel::createGUI(vars, num_vars);
customizeGUI();
update(INIT);
refresh(); // refresh the GUI
QTimer::singleShot(0, this, SLOT(resizeMe()));
}
IIRfilter::~IIRfilter(void) {}
//execute, the code block that actually does the signal processing
void IIRfilter::execute(void) {
output(0) = filter_implem->ProcessSample(input(0));
return;
}
void IIRfilter::update(DefaultGUIModel::update_flags_t flag) {
switch (flag) {
case INIT:
setParameter("Filter Order", QString::number(filter_order));
setParameter("Passband Ripple (dB)", QString::number(passband_ripple));
setParameter("Passband Edge (Hz)", QString::number(passband_edge));
setParameter("Stopband Ripple (dB)", QString::number(stopband_ripple));
setParameter("Stopband Edge (Hz)", QString::number(stopband_edge));
setParameter("Input quantizing factor", QString::number(ilog2(input_quan_factor)));
setParameter("Coefficients quantizing factor", QString::number(ilog2(coeff_quan_factor)));
filterType->setCurrentIndex(filter_type);
break;
case MODIFY:
filter_order = int(getParameter("Filter Order").toDouble());
passband_ripple = getParameter("Passband Ripple (dB)").toDouble();
passband_edge = getParameter("Passband Edge (Hz)").toDouble();
stopband_ripple = getParameter("Stopband Ripple (dB)").toDouble();
stopband_edge = getParameter("Stopband Edge (Hz)").toDouble();
filter_type = filter_t(filterType->currentIndex());
stopband_edge *= TWO_PI;
input_quan_factor = 2 ^ getParameter("Input quantizing factor").toInt(); // quantize input to 12 bits
coeff_quan_factor = 2 ^ getParameter("Coefficients quantizing factor").toInt(); // quantize filter coefficients to 12 bits
makeFilter();
break;
case PAUSE:
output(0) = 0; // stop command in case pause occurs in the middle of command
break;
case UNPAUSE:
break;
case PERIOD:
dt = RT::System::getInstance()->getPeriod() * 1e-9; // s
break;
default:
break;
}
}
// custom functions, as defined in the header file
void IIRfilter::initParameters() {
dt = RT::System::getInstance()->getPeriod() * 1e-9; // s
filter_type = BUTTER;
filter_order = 10;
passband_ripple = 3;
passband_edge = 60;
stopband_ripple = 60;
stopband_edge = 200;
ripple_bw_norm = 0;
predistort_enabled = true;
quant_enabled = false;
input_quan_factor = 4096; // quantize input to 12 bits
coeff_quan_factor = 4096; // quantize filter coefficients to 12 bits
makeFilter();
}
void IIRfilter::updateFilterType(int index) {
if (index == 0) {
filter_type = BUTTER;
normType->setEnabled(false);
makeFilter();
} else if (index == 1) {
filter_type = CHEBY;
normType->setEnabled(true);
makeFilter();
} else if (index == 2) {
filter_type = ELLIP;
normType->setEnabled(false);
makeFilter();
}
}
void IIRfilter::updateNormType(int index) {
ripple_bw_norm = index;
makeFilter();
}
void IIRfilter::makeFilter() {
switch (filter_type) {
case BUTTER:
analog_filter = new ButterworthTransFunc(filter_order);
analog_filter->LowpassDenorm(passband_edge);
break;
case CHEBY:
analog_filter = new ChebyshevTransFunc(filter_order, passband_ripple,
ripple_bw_norm);
analog_filter->LowpassDenorm(passband_edge);
break;
case ELLIP:
int upper_summation_limit = 5;
analog_filter = new EllipticalTransFunc(filter_order, passband_ripple,
stopband_ripple, passband_edge, stopband_edge, upper_summation_limit);
break;
} // end of switch on window_shape
if (predistort_enabled) analog_filter->FrequencyPrewarp(dt);
filter_design = BilinearTransf(analog_filter, dt);
if (quant_enabled) {
filter_implem = new DirectFormIir(filter_design->GetNumNumerCoeffs(),
filter_design->GetNumDenomCoeffs(),
filter_design->GetNumerCoefficients(),
filter_design->GetDenomCoefficients(), coeff_quan_factor,
input_quan_factor);
} else {
filter_implem = new UnquantDirectFormIir(
filter_design->GetNumNumerCoeffs(),
filter_design->GetNumDenomCoeffs(),
filter_design->GetNumerCoefficients(),
filter_design->GetDenomCoefficients());
}
}
void IIRfilter::saveIIRData() {
QFileDialog* fd = new QFileDialog(this, "Save File As"/*, TRUE*/);
fd->setFileMode(QFileDialog::AnyFile);
fd->setViewMode(QFileDialog::Detail);
QString fileName;
if (fd->exec() == QDialog::Accepted) {
QStringList files = fd->selectedFiles();
if (!files.isEmpty()) fileName = files.takeFirst();
if (OpenFile(fileName)) {
// stream.setPrintableData(true);
switch (filter_type) {
case BUTTER:
stream << QString("BUTTERWORTH order=") << (int) filter_order
<< " passband edge=" << (double) passband_edge;
break;
case CHEBY:
stream << QString("CHEBYSHEV order=") << (int) filter_order
<< " passband ripple=" << (double) passband_ripple
<< " passband edge=" << (double) passband_edge;
if (ripple_bw_norm == 0) stream << " with 3 dB bandwidth normalization";
else stream << " with ripple bandwidth normalization";
break;
case ELLIP:
stream << QString("ELLIPTICAL order=") << (int) filter_order
<< " passband ripple=" << (double) passband_ripple
<< " passband edge=" << (double) passband_edge
<< " stopband ripple=" << (double) stopband_ripple
<< " stopband edge=" << (double) stopband_edge;
break;
}
stream << QString(" \n");
double *numer_coeff = filter_design->GetNumerCoefficients();
double *denom_coeff = filter_design->GetDenomCoefficients();
stream << QString("Filter numerator coefficients:\n");
for (int i = 0; i < filter_design->GetNumNumerCoeffs(); i++) {
stream << QString("numer_coeff[") << i << "] = "
<< (double) numer_coeff[i] << "\n";
}
stream << QString("Filter denominator coefficients:\n");
for (int i = 0; i < filter_design->GetNumDenomCoeffs() + 1; i++) {
stream << QString("denom_coeff[") << i << "] = "
<< (double) denom_coeff[i] << "\n";
}
dataFile.close();
delete[] numer_coeff;
delete[] denom_coeff;
}
else {
QMessageBox::information(this, "IIR filter: Save filter parameters",
"There was an error writing to this file. You can view\n"
"the parameters in the terminal.\n");
}
}
}
bool IIRfilter::OpenFile(QString FName) {
dataFile.setFileName(FName);
if (dataFile.exists()) {
switch (QMessageBox::warning(this, "IIR filter", tr(
"This file already exists: %1.\n").arg(FName), "Overwrite", "Append",
"Cancel", 0, 2)){
case 0: // overwrite
dataFile.remove();
if (!dataFile.open(QIODevice::Unbuffered | QIODevice::WriteOnly)) return false;
break;
case 1: // append
if (!dataFile.open(QIODevice::Unbuffered | QIODevice::WriteOnly | QIODevice::Append)) return false;
break;
case 2: // cancel
return false;
break;
}
} else if (!dataFile.open(QIODevice::Unbuffered | QIODevice::WriteOnly)) return false;
stream.setDevice(&dataFile);
// stream.setPrintableData(false); // write binary
return true;
}
//create the GUI components
void IIRfilter::customizeGUI(void) {
QGridLayout *customLayout = DefaultGUIModel::getLayout();
QVBoxLayout *customGUILayout = new QVBoxLayout;
QPushButton *saveDataButton = new QPushButton("Save IIR Coefficients");
customGUILayout->addWidget(saveDataButton);
QObject::connect(saveDataButton, SIGNAL(clicked()), this, SLOT(saveIIRData()));
saveDataButton->setToolTip("Save filter parameters and coefficients to a file");
QFormLayout *optionLayout = new QFormLayout;
customGUILayout->addLayout(optionLayout);
filterType = new QComboBox;
filterType->setToolTip("IIR filter.");
filterType->insertItem(1, "Butterworth");
filterType->insertItem(2, "Chebyshev");
filterType->insertItem(3," Elliptical");
optionLayout->addRow("IIR filter", filterType);
QObject::connect(filterType,SIGNAL(activated(int)), this, SLOT(updateFilterType(int)));
normType = new QComboBox;
normType->insertItem(1, "3 dB bandwidth");
normType->insertItem(2, "Ripple bandwidth");
normType->setToolTip("Type of Chebyshev normalization");
optionLayout->addRow("Chebyshev Normalize Type:", normType);
QObject::connect(normType,SIGNAL(activated(int)), this, SLOT(updateNormType(int)));
normType->setEnabled(false);
QFormLayout *checkBoxLayout = new QFormLayout;
QCheckBox *predistortCheckBox = new QCheckBox;
checkBoxLayout->addRow("Predistort frequencies", predistortCheckBox);
QCheckBox *quantizeCheckBox = new QCheckBox;
checkBoxLayout->addRow("Quantize input and coefficients", quantizeCheckBox);
QObject::connect(predistortCheckBox,SIGNAL(toggled(bool)),this,SLOT(togglePredistort(bool)));
QObject::connect(quantizeCheckBox,SIGNAL(toggled(bool)),this,SLOT(toggleQuantize(bool)));
predistortCheckBox->setToolTip("Predistort frequencies for bilinear transform");
quantizeCheckBox->setToolTip("Quantize input and coefficients");
QObject::connect(DefaultGUIModel::pauseButton, SIGNAL(toggled(bool)), saveDataButton, SLOT(setEnabled(bool)));
QObject::connect(DefaultGUIModel::pauseButton, SIGNAL(toggled(bool)), modifyButton, SLOT(setEnabled(bool)));
DefaultGUIModel::pauseButton->setToolTip("Start/Stop filter");
DefaultGUIModel::modifyButton->setToolTip("Commit changes to parameter values");
DefaultGUIModel::unloadButton->setToolTip("Close plug-in");
customLayout->addLayout(customGUILayout, 0, 0);
customLayout->addLayout(checkBoxLayout, 2, 0);
setLayout(customLayout);
}
void IIRfilter::togglePredistort(bool on) {
predistort_enabled = on;
}
void IIRfilter::toggleQuantize(bool on) {
quant_enabled = on;
}