kwave  18.07.70
Kwave::NotchFilter Class Reference

#include <NotchFilter.h>

Inheritance diagram for Kwave::NotchFilter:
Inheritance graph
Collaboration diagram for Kwave::NotchFilter:
Collaboration graph

Public Slots

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

Signals

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

Public Member Functions

 NotchFilter ()
 
virtual ~NotchFilter () Q_DECL_OVERRIDE
 
virtual void goOn () Q_DECL_OVERRIDE
 
virtual double at (double f) 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 setfilter_peaknotch2 (double freq, double bw)
 

Private Attributes

Kwave::SampleArray m_buffer
 
double m_f_cutoff
 
double m_f_bw
 
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 35 of file NotchFilter.h.

Constructor & Destructor Documentation

◆ NotchFilter()

Kwave::NotchFilter::NotchFilter ( )

Constructor

Definition at line 28 of file NotchFilter.cpp.

References initFilter().

30  m_buffer(blockSize()), m_f_cutoff(M_PI), m_f_bw(M_PI / 2)
31 {
32  initFilter();
33 }
Kwave::SampleArray m_buffer
Definition: NotchFilter.h:92
virtual unsigned int blockSize() const
Here is the call graph for this function:

◆ ~NotchFilter()

Kwave::NotchFilter::~NotchFilter ( )
virtual

Destructor

Definition at line 36 of file NotchFilter.cpp.

37 {
38 }

Member Function Documentation

◆ at()

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

Implements Kwave::TransmissionFunction.

Definition at line 47 of file NotchFilter.cpp.

References m_filter.

48 {
49  /*
50  * filter function as extracted from the aRts code:
51  *
52  * y[t] = cx*x[t] + cx1*x[t-1] + cx2*x[t-2]
53  * + cy1*y[t-1] + cy2*y[t-2];
54  *
55  * convert filter coefficients to our notation:
56  */
57  double a0, a1, a2, b1, b2;
58  a0 = m_filter.cx;
59  a1 = m_filter.cx1;
60  a2 = m_filter.cx2;
61  b1 = m_filter.cy1;
62  b2 = m_filter.cy2;
63 
64  /*
65  * a0*z^2 + a1*z + a2
66  * H(z) = ------------------ | z = e ^ (j*2*pi*f)
67  * z^2 - b1*z - b0
68  */
69  std::complex<double> h;
70  std::complex<double> w;
71  std::complex<double> j(0.0,1.0);
72  std::complex<double> z;
73 
74  w = f;
75  z = std::exp(j*w);
76 
77  // get h[z] at z=e^jw
78  h = 0.95 * (a0 * (z*z) + (a1*z) + a2) / ((z*z) - (b1*z) - b2);
79 
80  return sqrt(std::norm(h));
81 }
struct Kwave::NotchFilter::@14 m_filter

◆ goOn()

void Kwave::NotchFilter::goOn ( )
virtual

does the calculation

Implements Kwave::SampleSource.

Definition at line 41 of file NotchFilter.cpp.

References m_buffer, and output().

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

◆ initFilter()

void Kwave::NotchFilter::initFilter ( )
private

reset/initialize the filter coefficients

Definition at line 84 of file NotchFilter.cpp.

References m_filter.

Referenced by NotchFilter(), setBandwidth(), and setFrequency().

85 {
86  m_filter.x1 = 0.0;
87  m_filter.x2 = 0.0;
88  m_filter.y1 = 0.0;
89  m_filter.y2 = 0.0;
90  m_filter.y = 0.0;
91 }
struct Kwave::NotchFilter::@14 m_filter
Here is the caller graph for this function:

◆ input

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

receives input data

Definition at line 120 of file NotchFilter.cpp.

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

121 {
122  const Kwave::SampleArray &in = data;
123  bool ok = m_buffer.resize(in.size());
124  Q_ASSERT(ok);
125  Q_UNUSED(ok);
126 
128 
129  for (unsigned i = 0; i < in.size(); ++i)
130  {
131  // do the filtering
132  m_filter.x = sample2double(in[i]);
133  m_filter.y =
134  m_filter.cx * m_filter.x +
135  m_filter.cx1 * m_filter.x1 +
136  m_filter.cx2 * m_filter.x2 +
137  m_filter.cy1 * m_filter.y1 +
138  m_filter.cy2 * m_filter.y2;
139  m_filter.x2 = m_filter.x1;
140  m_filter.x1 = m_filter.x;
141  m_filter.y2 = m_filter.y1;
142  m_filter.y1 = m_filter.y;
143  m_buffer[i] = double2sample(0.95 * m_filter.y);
144  }
145 }
Kwave::SampleArray m_buffer
Definition: NotchFilter.h:92
struct Kwave::NotchFilter::@14 m_filter
static double sample2double(const sample_t s)
Definition: Sample.h:73
static sample_t double2sample(const double f)
Definition: Sample.h:81
void setfilter_peaknotch2(double freq, double bw)
Definition: NotchFilter.cpp:98
unsigned int size() const
bool resize(unsigned int size) Q_REQUIRED_RESULT
Here is the call graph for this function:

◆ output

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

emits a block with the filtered data

Referenced by goOn().

Here is the caller graph for this function:

◆ setBandwidth

void Kwave::NotchFilter::setBandwidth ( const QVariant  bw)
slot

Sets the bandwidth, normed to [0...2Pi]. The calculation is: bw = bandwidth [Hz] * 2 * Pi / f_sample [Hz]. The default setting is 0.1.

Definition at line 159 of file NotchFilter.cpp.

References initFilter(), m_f_bw, m_f_cutoff, and setfilter_peaknotch2().

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

160 {
161  double new_bw = QVariant(bw).toDouble();
162  if (qFuzzyCompare(new_bw, m_f_bw)) return; // nothing to do
163 
164  m_f_bw = new_bw;
165  initFilter();
167 }
void setfilter_peaknotch2(double freq, double bw)
Definition: NotchFilter.cpp:98
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setfilter_peaknotch2()

void Kwave::NotchFilter::setfilter_peaknotch2 ( double  freq,
double  bw 
)
private

set the coefficients for a given frequency

Parameters
freqnormed frequency
bwnormed bandwidth

Definition at line 98 of file NotchFilter.cpp.

References m_filter.

Referenced by input(), setBandwidth(), and setFrequency().

99 {
100  const double gdb = -100;
101  double k, w, bwr, abw, gain;
102 
103  k = pow(10.0, gdb / 20.0);
104  /* w = 2.0 * PI * freq / (double)SR; */
105  /* bwr = 2.0 * PI * bw / (double)SR; */
106  w = freq;
107  bwr = bw;
108  abw = (1.0 - tan(bwr / 2.0)) / (1.0 + tan(bwr / 2.0));
109  gain = 0.5 * (1.0 + k + abw - k * abw);
110  m_filter.cx = 1.0 * gain;
111  m_filter.cx1 = gain * (-2.0 * cos(w) * (1.0 + abw)) /
112  (1.0 + k + abw - k * abw);
113  m_filter.cx2 = gain * (abw + k * abw + 1.0 - k) /
114  (abw - k * abw + 1.0 + k);
115  m_filter.cy1 = 2.0 * cos(w) / (1.0 + tan(bwr / 2.0));
116  m_filter.cy2 = -abw;
117 }
struct Kwave::NotchFilter::@14 m_filter
Here is the caller graph for this function:

◆ setFrequency

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

Sets the center 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 148 of file NotchFilter.cpp.

References initFilter(), m_f_bw, m_f_cutoff, and setfilter_peaknotch2().

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

149 {
150  double new_freq = QVariant(fc).toDouble();
151  if (qFuzzyCompare(new_freq, m_f_cutoff)) return; // nothing to do
152 
153  m_f_cutoff = new_freq;
154  initFilter();
156 }
void setfilter_peaknotch2(double freq, double bw)
Definition: NotchFilter.cpp:98
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ cx

double Kwave::NotchFilter::cx

Definition at line 102 of file NotchFilter.h.

◆ cx1

double Kwave::NotchFilter::cx1

Definition at line 102 of file NotchFilter.h.

◆ cx2

double Kwave::NotchFilter::cx2

Definition at line 102 of file NotchFilter.h.

◆ cy1

double Kwave::NotchFilter::cy1

Definition at line 102 of file NotchFilter.h.

◆ cy2

double Kwave::NotchFilter::cy2

Definition at line 102 of file NotchFilter.h.

◆ m_buffer

Kwave::SampleArray Kwave::NotchFilter::m_buffer
private

buffer for input

Definition at line 92 of file NotchFilter.h.

Referenced by goOn(), and input().

◆ m_f_bw

double Kwave::NotchFilter::m_f_bw
private

bandwidth of the notch

Definition at line 98 of file NotchFilter.h.

Referenced by input(), setBandwidth(), and setFrequency().

◆ m_f_cutoff

double Kwave::NotchFilter::m_f_cutoff
private

cutoff frequency [0...PI]

Definition at line 95 of file NotchFilter.h.

Referenced by input(), setBandwidth(), and setFrequency().

◆ m_filter

struct { ... } Kwave::NotchFilter::m_filter

structure with the filter coefficients

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

◆ x

double Kwave::NotchFilter::x

Definition at line 103 of file NotchFilter.h.

◆ x1

double Kwave::NotchFilter::x1

Definition at line 103 of file NotchFilter.h.

◆ x2

double Kwave::NotchFilter::x2

Definition at line 103 of file NotchFilter.h.

◆ y

double Kwave::NotchFilter::y

Definition at line 103 of file NotchFilter.h.

◆ y1

double Kwave::NotchFilter::y1

Definition at line 103 of file NotchFilter.h.

◆ y2

double Kwave::NotchFilter::y2

Definition at line 103 of file NotchFilter.h.


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