kwave  18.07.70
Kwave::LowPassFilter Class Reference

#include <LowPassFilter.h>

Inheritance diagram for Kwave::LowPassFilter:
Inheritance graph
Collaboration diagram for Kwave::LowPassFilter:
Collaboration graph

Public Slots

void input (Kwave::SampleArray data)
 
void setFrequency (const QVariant fc)
 

Signals

void output (Kwave::SampleArray data)
 
- Signals inherited from Kwave::StreamObject
void attributeChanged (const QVariant value)
 

Public Member Functions

 LowPassFilter ()
 
virtual ~LowPassFilter () Q_DECL_OVERRIDE
 
virtual double at (double f) Q_DECL_OVERRIDE
 
virtual void goOn () Q_DECL_OVERRIDE
 
- Public Member Functions inherited from Kwave::SampleSource
 SampleSource (QObject *parent=Q_NULLPTR)
 
virtual ~SampleSource ()
 
virtual bool done () const
 
- Public Member Functions inherited from Kwave::StreamObject
 StreamObject (QObject *parent=Q_NULLPTR)
 
virtual ~StreamObject ()
 
virtual unsigned int tracks () const
 
virtual Kwave::StreamObjectoperator[] (unsigned int track)
 
virtual unsigned int tracksOfPort (const char *port) const
 
virtual Kwave::StreamObjectport (const char *port, unsigned int track)
 
virtual unsigned int blockSize () const
 
void setAttribute (const char *attribute, const QVariant &value)
 
- Public Member Functions inherited from Kwave::TransmissionFunction
virtual ~TransmissionFunction ()
 

Private Member Functions

void initFilter ()
 
void normed_setfilter_shelvelowpass (double freq)
 

Private Attributes

Kwave::SampleArray m_buffer
 
double m_f_cutoff
 
struct {
   double   cx
 
   double   cx1
 
   double   cx2
 
   double   cy1
 
   double   cy2
 
   double   x
 
   double   x1
 
   double   x2
 
   double   y
 
   double   y1
 
   double   y2
 
m_filter
 

Additional Inherited Members

- Static Public Member Functions inherited from Kwave::StreamObject
static void setInteractive (bool interactive)
 

Detailed Description

Definition at line 32 of file LowPassFilter.h.

Constructor & Destructor Documentation

◆ LowPassFilter()

Kwave::LowPassFilter::LowPassFilter ( )

Constructor

Definition at line 28 of file LowPassFilter.cpp.

References initFilter().

29  :Kwave::SampleSource(Q_NULLPTR), m_buffer(blockSize()),
30  m_f_cutoff(M_PI)
31 {
32  initFilter();
33 }
Kwave::SampleArray m_buffer
Definition: LowPassFilter.h:78
virtual unsigned int blockSize() const
Here is the call graph for this function:

◆ ~LowPassFilter()

Kwave::LowPassFilter::~LowPassFilter ( )
virtual

Destructor

Definition at line 36 of file LowPassFilter.cpp.

37 {
38 }

Member Function Documentation

◆ at()

double Kwave::LowPassFilter::at ( double  f)
virtual
See also
TransmissionFunction::at()

Implements Kwave::TransmissionFunction.

Definition at line 166 of file LowPassFilter.cpp.

References m_filter.

167 {
168  /*
169  * filter function as extracted from the aRts code:
170  *
171  * y[t] = cx*x[t] + cx1*x[t-1] + cx2*x[t-2]
172  * + cy1*y[t-1] + cy2*y[t-2];
173  *
174  * convert filter coefficients to our notation:
175  */
176  double a0, a1, a2, b1, b2;
177  a0 = m_filter.cx;
178  a1 = m_filter.cx1;
179  a2 = m_filter.cx2;
180  b1 = m_filter.cy1;
181  b2 = m_filter.cy2;
182 
183  /*
184  * a0*z^2 + a1*z + a2
185  * H(z) = ------------------ | z = e ^ (j*2*pi*f)
186  * z^2 - b1*z - b0
187  */
188  std::complex<double> h;
189  std::complex<double> w;
190  std::complex<double> j(0.0,1.0);
191  std::complex<double> z;
192 
193  w = f;
194  z = std::exp(j*w);
195 
196  // get h[z] at z=e^jw
197  h = 0.95 * (a0 * (z*z) + (a1*z) + a2) / ((z*z) - (b1*z) - b2);
198 
199  return sqrt(std::norm(h));
200 }
struct Kwave::LowPassFilter::@13 m_filter

◆ goOn()

void Kwave::LowPassFilter::goOn ( )
virtual

does the calculation

Implements Kwave::SampleSource.

Definition at line 41 of file LowPassFilter.cpp.

References m_buffer, and output().

42 {
43  emit output(m_buffer);
44 }
Kwave::SampleArray m_buffer
Definition: LowPassFilter.h:78
void output(Kwave::SampleArray data)

◆ initFilter()

void Kwave::LowPassFilter::initFilter ( )
private

reset/initialize the filter coefficients

Definition at line 47 of file LowPassFilter.cpp.

References m_filter.

Referenced by LowPassFilter(), and setFrequency().

48 {
49  m_filter.x1 = 0.0;
50  m_filter.x2 = 0.0;
51  m_filter.y1 = 0.0;
52  m_filter.y2 = 0.0;
53  m_filter.y = 0.0;
54 }
struct Kwave::LowPassFilter::@13 m_filter
Here is the caller graph for this function:

◆ input

void Kwave::LowPassFilter::input ( Kwave::SampleArray  data)
slot

receives input data

Definition at line 138 of file LowPassFilter.cpp.

References double2sample(), m_buffer, m_f_cutoff, m_filter, normed_setfilter_shelvelowpass(), Kwave::SampleArray::resize(), sample2double(), and Kwave::SampleArray::size().

139 {
140  const Kwave::SampleArray &in = data;
141  bool ok = m_buffer.resize(in.size());
142  Q_ASSERT(ok);
143  Q_UNUSED(ok);
144 
146 
147  for (unsigned i = 0; i < in.size(); i++)
148  {
149  // do the filtering
150  m_filter.x = sample2double(in[i]);
151  m_filter.y =
152  m_filter.cx * m_filter.x +
153  m_filter.cx1 * m_filter.x1 +
154  m_filter.cx2 * m_filter.x2 +
155  m_filter.cy1 * m_filter.y1 +
156  m_filter.cy2 * m_filter.y2;
157  m_filter.x2 = m_filter.x1;
158  m_filter.x1 = m_filter.x;
159  m_filter.y2 = m_filter.y1;
160  m_filter.y1 = m_filter.y;
161  m_buffer[i] = double2sample(0.95 * m_filter.y);
162  }
163 }
Kwave::SampleArray m_buffer
Definition: LowPassFilter.h:78
static double sample2double(const sample_t s)
Definition: Sample.h:73
static sample_t double2sample(const double f)
Definition: Sample.h:81
unsigned int size() const
void normed_setfilter_shelvelowpass(double freq)
bool resize(unsigned int size) Q_REQUIRED_RESULT
struct Kwave::LowPassFilter::@13 m_filter
Here is the call graph for this function:

◆ normed_setfilter_shelvelowpass()

void Kwave::LowPassFilter::normed_setfilter_shelvelowpass ( double  freq)
private

calculate filter coefficients for a given frequency

Definition at line 121 of file LowPassFilter.cpp.

References m_filter, and shelve().

Referenced by input(), and setFrequency().

122 {
123  double gain;
124  double boost = 80.0;
125 
126  gain = pow(10.0, boost / 20.0);
127  shelve(freq / (2 * M_PI), boost,
128  &m_filter.cx, &m_filter.cx1, &m_filter.cx2,
129  &m_filter.cy1, &m_filter.cy2);
130  m_filter.cx /= gain;
131  m_filter.cx1 /= gain;
132  m_filter.cx2 /= gain;
133  m_filter.cy1 = -m_filter.cy1;
134  m_filter.cy2 = -m_filter.cy2;
135 }
static void shelve(double cf, double boost, double *a0, double *a1, double *a2, double *b1, double *b2)
struct Kwave::LowPassFilter::@13 m_filter
Here is the call graph for this function:
Here is the caller graph for this function:

◆ output

void Kwave::LowPassFilter::output ( Kwave::SampleArray  data)
signal

emits a block with the filtered data

Referenced by goOn().

Here is the caller graph for this function:

◆ setFrequency

void Kwave::LowPassFilter::setFrequency ( const QVariant  fc)
slot

Sets the cutoff frequency, normed to [0...2Pi]. The calculation is: fc = frequency [Hz] * 2 * Pi / f_sample [Hz]. The default setting is 0.5.

Definition at line 203 of file LowPassFilter.cpp.

References initFilter(), m_f_cutoff, and normed_setfilter_shelvelowpass().

Referenced by Kwave::LowPassDialog::updateDisplay().

204 {
205 
206  double new_freq = QVariant(fc).toDouble();
207  if (qFuzzyCompare(new_freq, m_f_cutoff)) return; // nothing to do
208 
209  m_f_cutoff = new_freq;
210  initFilter();
212 }
void normed_setfilter_shelvelowpass(double freq)
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ cx

double Kwave::LowPassFilter::cx

Definition at line 85 of file LowPassFilter.h.

◆ cx1

double Kwave::LowPassFilter::cx1

Definition at line 85 of file LowPassFilter.h.

◆ cx2

double Kwave::LowPassFilter::cx2

Definition at line 85 of file LowPassFilter.h.

◆ cy1

double Kwave::LowPassFilter::cy1

Definition at line 85 of file LowPassFilter.h.

◆ cy2

double Kwave::LowPassFilter::cy2

Definition at line 85 of file LowPassFilter.h.

◆ m_buffer

Kwave::SampleArray Kwave::LowPassFilter::m_buffer
private

buffer for input

Definition at line 78 of file LowPassFilter.h.

Referenced by goOn(), and input().

◆ m_f_cutoff

double Kwave::LowPassFilter::m_f_cutoff
private

cutoff frequency [0...PI]

Definition at line 81 of file LowPassFilter.h.

Referenced by input(), and setFrequency().

◆ m_filter

struct { ... } Kwave::LowPassFilter::m_filter

structure with the filter coefficients

Referenced by at(), initFilter(), input(), and normed_setfilter_shelvelowpass().

◆ x

double Kwave::LowPassFilter::x

Definition at line 86 of file LowPassFilter.h.

◆ x1

double Kwave::LowPassFilter::x1

Definition at line 86 of file LowPassFilter.h.

◆ x2

double Kwave::LowPassFilter::x2

Definition at line 86 of file LowPassFilter.h.

◆ y

double Kwave::LowPassFilter::y

Definition at line 86 of file LowPassFilter.h.

◆ y1

double Kwave::LowPassFilter::y1

Definition at line 86 of file LowPassFilter.h.

◆ y2

double Kwave::LowPassFilter::y2

Definition at line 86 of file LowPassFilter.h.


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