[1f6d2f]: libkwave / Filter.cpp Maximize Restore History

Download this file

Filter.cpp    212 lines (175 with data), 6.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
/***************************************************************************
Filter.cpp - parameters of a digital IIR or FIR filter
-------------------
begin : Jan 20 2001
copyright : (C) 2001 by Thomas Eschenbacher
email : Thomas Eschenbacher <thomas.eschenbacher@gmx.de>
***************************************************************************/
/***************************************************************************
* *
* 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#include <QFile>
#include <QString>
#include <QTextStream>
#include "Filter.h"
#include "Parser.h"
//***************************************************************************
Kwave::Filter::Filter(const QString &command)
:m_fir(true), m_rate(0), m_coeff(), m_delay()
{
Kwave::Parser parse(command);
m_rate = parse.toInt();
m_fir = (parse.nextParam().toLower() == "fir");
resize(parse.toInt());
for (unsigned int i = 0; i < count(); i++) {
m_delay[i] = parse.toInt();
m_coeff[i] = parse.toDouble();
}
}
//***************************************************************************
Kwave::Filter::Filter(int rate)
:m_fir(true), m_rate(rate), m_coeff(), m_delay()
{
}
//***************************************************************************
Kwave::Filter::~Filter()
{
resize(0);
}
//***************************************************************************
QString Kwave::Filter::command()
{
QString s;
s = "filter (";
s += QString::number(m_rate);
s += ',';
s += (m_fir ? "fir" : "iir");
s += ',' + QString::number(count());
for (unsigned int i = 0; i < count(); i++) {
s += ',';
s += QString::number(m_delay[i]);
s += ',';
s += QString::number(m_coeff[i]);
}
return s;
}
//***************************************************************************
unsigned int Kwave::Filter::resize(unsigned int newnum)
{
unsigned int oldnum = count();
if (newnum == oldnum) return oldnum; // nothing to do
// resize both arrays
m_delay.resize(newnum);
m_coeff.resize(newnum);
Q_ASSERT(m_delay.size() >= static_cast<int>(newnum));
Q_ASSERT(m_coeff.size() >= static_cast<int>(newnum));
Q_ASSERT(m_delay.size() == m_coeff.size());
// initialize the new entries
while (oldnum < newnum) {
m_delay[oldnum] = oldnum;
m_coeff[oldnum] = 0.0;
oldnum++;
}
return newnum;
}
//***************************************************************************
unsigned int Kwave::Filter::count()
{
Q_ASSERT(m_coeff.count() == m_delay.count());
return m_coeff.count();
}
//***************************************************************************
double Kwave::Filter::coeff(unsigned int index)
{
Q_ASSERT(static_cast<int>(index) < m_coeff.count());
return m_coeff[index];
}
//***************************************************************************
void Kwave::Filter::setCoeff(unsigned int index, double newval)
{
Q_ASSERT(static_cast<int>(index) < m_coeff.count());
m_coeff[index] = newval;
}
//***************************************************************************
unsigned int Kwave::Filter::delay(unsigned int index)
{
Q_ASSERT(static_cast<int>(index) < m_delay.count());
return m_delay[index];
}
//***************************************************************************
void Kwave::Filter::setDelay(unsigned int index, unsigned int newval)
{
Q_ASSERT(static_cast<int>(index) < m_delay.count());
m_delay[index] = newval;
}
//***************************************************************************
void Kwave::Filter::save(const QString &filename)
{
QString name(filename);
Q_ASSERT(name.length());
if (!name.length()) return;
if (name.lastIndexOf(".filter") != static_cast<int>(name.length()-7)){
name.append(".filter");
}
QFile file(name);
file.open(QIODevice::WriteOnly);
QTextStream out(&file);
out << ((m_fir) ? "FIR " : "IIR ") << count() << endl;
for (unsigned int i = 0; i < count(); i++) {
out << m_delay[i] << ' ' << m_coeff[i] << endl;
}
file.close();
}
//***************************************************************************
void Kwave::Filter::load(const QString &filename)
{
unsigned int i;
bool ok;
QString line;
unsigned int linenr = 0;
QFile file(filename);
file.open(QIODevice::ReadOnly);
QTextStream in(&file);
// type of the filter (FIR/IIR)
while (!in.atEnd()) {
line = in.readLine().simplified();
linenr++;
if (line.isEmpty() || line.isNull()) continue;
if ((line[0] == '#') || (line[0] == '/')) continue;
break;
}
m_fir = line.startsWith("FIR ");
qDebug("Filter::load(): fir = %d", m_fir);
// order
unsigned int order = line.remove(0,4).toUInt(&ok);
resize(0);
resize(order);
qDebug("Filter::load(): order = %d", order);
// read delays and coefficients
i = 0;
while (!in.atEnd()) {
line = in.readLine().simplified();
linenr++;
if (line.isEmpty() || line.isNull()) continue;
if ((line[0] == '#') || (line[0] == '/')) continue;
int spacepos = line.indexOf(' ');
ok = true;
if (ok) m_delay[i] = line.left(spacepos).toUInt(&ok);
line.remove(0, spacepos);
if (ok) m_coeff[i] = line.toDouble(&ok);
if (ok) {
i++;
} else {
qDebug("Filter::load(%s): syntax error in line %d",
filename.toLocal8Bit().data(), linenr);
}
}
}
//***************************************************************************
//***************************************************************************