kwave  18.07.70
Kwave::Filter Class Reference

#include <Filter.h>

Collaboration diagram for Kwave::Filter:
Collaboration graph

Public Member Functions

 Filter (int rate)
 
 Filter (const QString &command)
 
virtual ~Filter ()
 
QString command ()
 
unsigned int resize (unsigned int newnum)
 
bool isFIR () const
 
int rate () const
 
unsigned int count ()
 
double coeff (unsigned int index)
 
void setCoeff (unsigned int index, double newval)
 
unsigned int delay (unsigned int index)
 
void setDelay (unsigned int index, unsigned int newval)
 
void load (const QString &filename)
 
void save (const QString &filename)
 

Private Attributes

bool m_fir
 
unsigned int m_rate
 
QVector< double > m_coeff
 
QVector< int > m_delay
 

Detailed Description

Holds a set of parameters for a digital IIR or FIR filter.

Todo:

use KIONetAccess in the load/save methods

more error checks in load/save (current code is too optimistic)

Definition at line 36 of file Filter.h.

Constructor & Destructor Documentation

◆ Filter() [1/2]

Kwave::Filter::Filter ( int  rate)
explicit

Constructor, creates an empty filter with a given sample rate.

Parameters
ratenumber of samples per second

Definition at line 47 of file Filter.cpp.

48  :m_fir(true), m_rate(rate), m_coeff(), m_delay()
49 {
50 }
QVector< double > m_coeff
Definition: Filter.h:120
bool m_fir
Definition: Filter.h:114
int rate() const
Definition: Filter.h:71
unsigned int m_rate
Definition: Filter.h:117
QVector< int > m_delay
Definition: Filter.h:123

◆ Filter() [2/2]

Kwave::Filter::Filter ( const QString &  command)
explicit

Constructor, creates a filter from a Kwave command string.

Parameters
commandpart of the Kwave command with parameters

Definition at line 31 of file Filter.cpp.

References _, count(), m_coeff, m_delay, m_fir, m_rate, Kwave::Parser::nextParam(), resize(), Kwave::Parser::toDouble(), and Kwave::Parser::toInt().

32  :m_fir(true), m_rate(0), m_coeff(), m_delay()
33 {
34  Kwave::Parser parse(command);
35 
36  m_rate = parse.toInt();
37  m_fir = (parse.nextParam().toLower() == _("fir"));
38  resize(parse.toInt());
39 
40  for (unsigned int i = 0; i < count(); i++) {
41  m_delay[i] = parse.toInt();
42  m_coeff[i] = parse.toDouble();
43  }
44 }
QVector< double > m_coeff
Definition: Filter.h:120
bool m_fir
Definition: Filter.h:114
unsigned int count()
Definition: Filter.cpp:102
unsigned int m_rate
Definition: Filter.h:117
#define _(m)
Definition: memcpy.c:66
QVector< int > m_delay
Definition: Filter.h:123
unsigned int resize(unsigned int newnum)
Definition: Filter.cpp:79
QString command()
Definition: Filter.cpp:59
Here is the call graph for this function:

◆ ~Filter()

Kwave::Filter::~Filter ( )
virtual

Destructor

Definition at line 53 of file Filter.cpp.

References resize().

54 {
55  resize(0);
56 }
unsigned int resize(unsigned int newnum)
Definition: Filter.cpp:79
Here is the call graph for this function:

Member Function Documentation

◆ coeff()

double Kwave::Filter::coeff ( unsigned int  index)

Returns a filter coefficient.

Parameters
indexinternal index [0...count-1]

Definition at line 109 of file Filter.cpp.

References m_coeff, and Kwave::toInt().

Referenced by rate().

110 {
111  Q_ASSERT(Kwave::toInt(index) < m_coeff.count());
112  return m_coeff[index];
113 }
QVector< double > m_coeff
Definition: Filter.h:120
int toInt(T x)
Definition: Utils.h:127
Here is the call graph for this function:
Here is the caller graph for this function:

◆ command()

QString Kwave::Filter::command ( )

Returns the Kwave command string from the current parameters.

Definition at line 59 of file Filter.cpp.

References _, count(), m_coeff, m_delay, m_fir, and m_rate.

60 {
61  QString s;
62 
63  s = _("filter (");
64  s += QString::number(m_rate);
65  s += QLatin1Char(',');
66  s += _((m_fir) ? "fir" : "iir");
67  s += QLatin1Char(',') + QString::number(count());
68 
69  for (unsigned int i = 0; i < count(); i++) {
70  s += QLatin1Char(',');
71  s += QString::number(m_delay[i]);
72  s += QLatin1Char(',');
73  s += QString::number(m_coeff[i]);
74  }
75  return s;
76 }
QVector< double > m_coeff
Definition: Filter.h:120
bool m_fir
Definition: Filter.h:114
unsigned int count()
Definition: Filter.cpp:102
unsigned int m_rate
Definition: Filter.h:117
#define _(m)
Definition: memcpy.c:66
QVector< int > m_delay
Definition: Filter.h:123
Here is the call graph for this function:

◆ count()

unsigned int Kwave::Filter::count ( )

Returns the number of coefficients and delay times (order) of the filter.

Definition at line 102 of file Filter.cpp.

References m_coeff, and m_delay.

Referenced by command(), Filter(), rate(), resize(), and save().

103 {
104  Q_ASSERT(m_coeff.count() == m_delay.count());
105  return m_coeff.count();
106 }
QVector< double > m_coeff
Definition: Filter.h:120
QVector< int > m_delay
Definition: Filter.h:123
Here is the caller graph for this function:

◆ delay()

unsigned int Kwave::Filter::delay ( unsigned int  index)

Returns a delay time of the filter.

Parameters
indexinternal index [0...count-1]

Definition at line 123 of file Filter.cpp.

References m_delay, and Kwave::toInt().

Referenced by rate().

124 {
125  Q_ASSERT(Kwave::toInt(index) < m_delay.count());
126  return m_delay[index];
127 }
int toInt(T x)
Definition: Utils.h:127
QVector< int > m_delay
Definition: Filter.h:123
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isFIR()

bool Kwave::Filter::isFIR ( ) const
inline

Returns true if the filter is a FIR one, or false else

Definition at line 68 of file Filter.h.

References m_fir.

68 { return m_fir; }
bool m_fir
Definition: Filter.h:114

◆ load()

void Kwave::Filter::load ( const QString &  filename)

Loads the filter parameters from a URL

Definition at line 159 of file Filter.cpp.

References _, DBG, m_coeff, m_delay, m_fir, and resize().

Referenced by rate().

160 {
161  unsigned int i;
162  bool ok;
163  QString line;
164  unsigned int linenr = 0;
165 
166  QFile file(filename);
167  file.open(QIODevice::ReadOnly);
168  QTextStream in(&file);
169 
170  // type of the filter (FIR/IIR)
171  while (!in.atEnd()) {
172  line = in.readLine().simplified();
173  linenr++;
174 
175  if (line.isEmpty() || line.isNull()) continue;
176  if ((line[0] == QLatin1Char('#')) || (line[0] == QLatin1Char('/')))
177  continue;
178  break;
179  }
180  m_fir = line.startsWith(_("FIR "));
181  qDebug("Filter::load(): fir = %d", m_fir);
182 
183  // order
184  unsigned int order = line.remove(0,4).toUInt(&ok);
185  resize(0);
186  resize(order);
187  qDebug("Filter::load(): order = %d", order);
188 
189  // read delays and coefficients
190  i = 0;
191  while (!in.atEnd()) {
192  line = in.readLine().simplified();
193  linenr++;
194 
195  if (line.isEmpty() || line.isNull()) continue;
196  if ((line[0] == QLatin1Char('#')) || (line[0] == QLatin1Char('/')))
197  continue;
198 
199  int spacepos = line.indexOf(QLatin1Char(' '));
200  ok = true;
201  if (ok) m_delay[i] = line.left(spacepos).toUInt(&ok);
202  line.remove(0, spacepos);
203  if (ok) m_coeff[i] = line.toDouble(&ok);
204 
205  if (ok) {
206  i++;
207  } else {
208  qDebug("Filter::load(%s): syntax error in line %d",
209  DBG(filename), linenr);
210  }
211  }
212 }
QVector< double > m_coeff
Definition: Filter.h:120
bool m_fir
Definition: Filter.h:114
#define _(m)
Definition: memcpy.c:66
#define DBG(qs)
Definition: String.h:55
QVector< int > m_delay
Definition: Filter.h:123
unsigned int resize(unsigned int newnum)
Definition: Filter.cpp:79
Here is the call graph for this function:
Here is the caller graph for this function:

◆ rate()

int Kwave::Filter::rate ( ) const
inline

Returns the sample rate in samples/second

Definition at line 71 of file Filter.h.

References coeff(), count(), delay(), load(), m_rate, save(), setCoeff(), and setDelay().

71 { return m_rate; }
unsigned int m_rate
Definition: Filter.h:117
Here is the call graph for this function:

◆ resize()

unsigned int Kwave::Filter::resize ( unsigned int  newnum)

Resizes the filter to a new number of coefficients.

Parameters
newnumnew number of coefficients [1..]
Returns
new number of coefficients or zero if failed

Definition at line 79 of file Filter.cpp.

References count(), m_coeff, m_delay, and Kwave::toInt().

Referenced by Filter(), load(), and ~Filter().

80 {
81  unsigned int oldnum = count();
82  if (newnum == oldnum) return oldnum; // nothing to do
83 
84  // resize both arrays
85  m_delay.resize(newnum);
86  m_coeff.resize(newnum);
87  Q_ASSERT(m_delay.size() >= Kwave::toInt(newnum));
88  Q_ASSERT(m_coeff.size() >= Kwave::toInt(newnum));
89  Q_ASSERT(m_delay.size() == m_coeff.size());
90 
91  // initialize the new entries
92  while (oldnum < newnum) {
93  m_delay[oldnum] = oldnum;
94  m_coeff[oldnum] = 0.0;
95  oldnum++;
96  }
97 
98  return newnum;
99 }
QVector< double > m_coeff
Definition: Filter.h:120
unsigned int count()
Definition: Filter.cpp:102
int toInt(T x)
Definition: Utils.h:127
QVector< int > m_delay
Definition: Filter.h:123
Here is the call graph for this function:
Here is the caller graph for this function:

◆ save()

void Kwave::Filter::save ( const QString &  filename)

Saves the filter parameters to a URL

Definition at line 137 of file Filter.cpp.

References _, count(), m_coeff, m_delay, m_fir, name, and Kwave::toInt().

Referenced by rate().

138 {
139  QString name(filename);
140  Q_ASSERT(name.length());
141  if (!name.length()) return;
142 
143  if (name.lastIndexOf(_(".filter")) != Kwave::toInt(name.length() - 7))
144  name.append(_(".filter"));
145 
146  QFile file(name);
147  file.open(QIODevice::WriteOnly);
148  QTextStream out(&file);
149 
150  out << ((m_fir) ? "FIR " : "IIR ") << count() << endl;
151  for (unsigned int i = 0; i < count(); i++) {
152  out << m_delay[i] << ' ' << m_coeff[i] << endl;
153  }
154 
155  file.close();
156 }
QVector< double > m_coeff
Definition: Filter.h:120
bool m_fir
Definition: Filter.h:114
const char name[16]
Definition: memcpy.c:510
unsigned int count()
Definition: Filter.cpp:102
int toInt(T x)
Definition: Utils.h:127
#define _(m)
Definition: memcpy.c:66
QVector< int > m_delay
Definition: Filter.h:123
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setCoeff()

void Kwave::Filter::setCoeff ( unsigned int  index,
double  newval 
)

Sets a filter coefficient to a new value.

Parameters
indexinternal index [0...count-1]
newvalnew coefficient

Definition at line 116 of file Filter.cpp.

References m_coeff, and Kwave::toInt().

Referenced by rate().

117 {
118  Q_ASSERT(Kwave::toInt(index) < m_coeff.count());
119  m_coeff[index] = newval;
120 }
QVector< double > m_coeff
Definition: Filter.h:120
int toInt(T x)
Definition: Utils.h:127
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setDelay()

void Kwave::Filter::setDelay ( unsigned int  index,
unsigned int  newval 
)

Sets a delay value to a new value.

Parameters
indexinternal index [0...count-1]
newvalnew delay value

Definition at line 130 of file Filter.cpp.

References m_delay, and Kwave::toInt().

Referenced by rate().

131 {
132  Q_ASSERT(Kwave::toInt(index) < m_delay.count());
133  m_delay[index] = newval;
134 }
int toInt(T x)
Definition: Utils.h:127
QVector< int > m_delay
Definition: Filter.h:123
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_coeff

QVector<double> Kwave::Filter::m_coeff
private

array of coefficients

Definition at line 120 of file Filter.h.

Referenced by coeff(), command(), count(), Filter(), load(), resize(), save(), and setCoeff().

◆ m_delay

QVector<int> Kwave::Filter::m_delay
private

array of delay times

Definition at line 123 of file Filter.h.

Referenced by command(), count(), delay(), Filter(), load(), resize(), save(), and setDelay().

◆ m_fir

bool Kwave::Filter::m_fir
private

boolean if filter is FIR or IIR

Definition at line 114 of file Filter.h.

Referenced by command(), Filter(), isFIR(), load(), and save().

◆ m_rate

unsigned int Kwave::Filter::m_rate
private

sample rate in samples/second

Definition at line 117 of file Filter.h.

Referenced by command(), Filter(), and rate().


The documentation for this class was generated from the following files: