kwave  18.07.70
BandPass.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  BandPass.cpp - simple band pass
3  -------------------
4  begin : Sun Nov 18 2007
5  copyright : (C) 2007 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
7 
8  filter functions:
9  Copyright (C) 1998 Juhana Sadeharju <kouhia@nic.funet.fi>
10 
11  ***************************************************************************/
12 
13 /***************************************************************************
14  * *
15  * This program is free software; you can redistribute it and/or modify *
16  * it under the terms of the GNU General Public License as published by *
17  * the Free Software Foundation; either version 2 of the License, or *
18  * (at your option) any later version. *
19  * *
20  ***************************************************************************/
21 
22 #include "config.h"
23 #include <complex>
24 #include <math.h>
25 
26 #include "BandPass.h"
27 
28 //***************************************************************************
30  :Kwave::SampleSource(Q_NULLPTR), m_buffer(blockSize()),
31  m_frequency(0.5), m_bandwidth(0.1)
32 {
33  initFilter();
35 }
36 
37 //***************************************************************************
39 {
40 }
41 
42 //***************************************************************************
44 {
45  emit output(m_buffer);
46 }
47 
48 //***************************************************************************
49 double Kwave::BandPass::at(double f)
50 {
51  /*
52  * filter function as extracted from the aRts code:
53  *
54  * y[t] = cx*x[t] + cx1*x[t-1] + cx2*x[t-2]
55  * + cy1*y[t-1] + cy2*y[t-2];
56  *
57  * convert filter coefficients to our notation:
58  */
59  double a0, a1, a2, b1, b2;
60  a0 = m_filter.cx;
61  a1 = m_filter.cx1;
62  a2 = m_filter.cx2;
63  b1 = m_filter.cy1;
64  b2 = m_filter.cy2;
65 
66  /*
67  * a0*z^2 + a1*z + a2
68  * H(z) = ------------------ | z = e ^ (j*2*pi*f)
69  * z^2 - b1*z - b0
70  */
71  std::complex<double> h;
72  std::complex<double> w;
73  std::complex<double> j(0.0,1.0);
74  std::complex<double> z;
75 
76  w = f;
77  z = std::exp(j*w);
78 
79  // get h[z] at z=e^jw
80  h = 0.95 * (a0 * (z*z) + (a1*z) + a2) / ((z*z) - (b1*z) - b2);
81 
82  return sqrt(std::norm(h));
83 }
84 
85 //***************************************************************************
87 {
88  m_filter.x1 = 0.0;
89  m_filter.x2 = 0.0;
90  m_filter.y1 = 0.0;
91  m_filter.y2 = 0.0;
92  m_filter.y = 0.0;
93 }
94 
95 //***************************************************************************
96 /*
97  * As in ''An introduction to digital filter theory'' by Julius O. Smith
98  * and in Moore's book; I use the normalized version in Moore's book.
99  */
100 void Kwave::BandPass::setfilter_2polebp(double freq, double R)
101 {
102  m_filter.cx = 1.0 - R;
103  m_filter.cx1 = 0.0;
104  m_filter.cx2 = - (1.0 - R) * R;
105  m_filter.cy1 = 2.0 * R * cos(freq);
106  m_filter.cy2 = -R * R;
107 }
108 
109 //***************************************************************************
111 {
112  const Kwave::SampleArray &in = data;
113 
114  bool ok = m_buffer.resize(in.size());
115  Q_ASSERT(ok);
116  Q_UNUSED(ok);
117 
119 
120  Q_ASSERT(in.size() == m_buffer.size());
121 
122  for (unsigned i = 0; i < in.size(); ++i)
123  {
124  // do the filtering
125  m_filter.x = sample2double(in[i]);
126  m_filter.y =
127  m_filter.cx * m_filter.x +
128  m_filter.cx1 * m_filter.x1 +
129  m_filter.cx2 * m_filter.x2 +
130  m_filter.cy1 * m_filter.y1 +
131  m_filter.cy2 * m_filter.y2;
132  m_filter.x2 = m_filter.x1;
133  m_filter.x1 = m_filter.x;
134  m_filter.y2 = m_filter.y1;
135  m_filter.y1 = m_filter.y;
136  m_buffer[i] = double2sample(0.95 * m_filter.y);
137  }
138 }
139 
140 //***************************************************************************
141 void Kwave::BandPass::setFrequency(const QVariant fc)
142 {
143  double new_freq = QVariant(fc).toDouble();
144  if (qFuzzyCompare(new_freq, m_frequency)) return; // nothing to do
145 
146  m_frequency = new_freq;
147  initFilter();
149 }
150 
151 //***************************************************************************
152 void Kwave::BandPass::setBandwidth(const QVariant bw)
153 {
154  double new_bw = QVariant(bw).toDouble();
155  if (qFuzzyCompare(new_bw, m_bandwidth)) return; // nothing to do
156 
157  m_bandwidth = new_bw;
158  initFilter();
160 }
161 
162 //***************************************************************************
163 //***************************************************************************
void initFilter()
Definition: BandPass.cpp:86
struct Kwave::BandPass::@2 m_filter
Definition: App.h:33
double m_bandwidth
Definition: BandPass.h:99
void output(Kwave::SampleArray data)
virtual double at(double f) Q_DECL_OVERRIDE
Definition: BandPass.cpp:49
void setFrequency(const QVariant fc)
Definition: BandPass.cpp:141
virtual void goOn() Q_DECL_OVERRIDE
Definition: BandPass.cpp:43
Kwave::SampleArray m_buffer
Definition: BandPass.h:93
static double sample2double(const sample_t s)
Definition: Sample.h:73
void setBandwidth(const QVariant bw)
Definition: BandPass.cpp:152
static sample_t double2sample(const double f)
Definition: Sample.h:81
virtual ~BandPass() Q_DECL_OVERRIDE
Definition: BandPass.cpp:38
unsigned int size() const
void setfilter_2polebp(double freq, double R)
Definition: BandPass.cpp:100
void input(Kwave::SampleArray data)
Definition: BandPass.cpp:110
bool resize(unsigned int size) Q_REQUIRED_RESULT
double m_frequency
Definition: BandPass.h:96