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
|
/***************************************************************************
BandPass.cpp - simple band pass
-------------------
begin : Sun Nov 18 2007
copyright : (C) 2007 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
filter functions:
Copyright (C) 1998 Juhana Sadeharju <kouhia@nic.funet.fi>
***************************************************************************/
/***************************************************************************
* *
* 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 <complex>
#include <math.h>
#include "BandPass.h"
//***************************************************************************
Kwave::BandPass::BandPass()
:Kwave::SampleSource(nullptr), m_buffer(blockSize()),
m_frequency(0.5), m_bandwidth(0.1)
{
initFilter();
setfilter_2polebp(m_frequency, m_bandwidth);
}
//***************************************************************************
Kwave::BandPass::~BandPass()
{
}
//***************************************************************************
void Kwave::BandPass::goOn()
{
emit output(m_buffer);
}
//***************************************************************************
double Kwave::BandPass::at(double f)
{
/*
* filter function as extracted from the aRts code:
*
* y[t] = cx*x[t] + cx1*x[t-1] + cx2*x[t-2]
* + cy1*y[t-1] + cy2*y[t-2];
*
* convert filter coefficients to our notation:
*/
double a0, a1, a2, b1, b2;
a0 = m_filter.cx;
a1 = m_filter.cx1;
a2 = m_filter.cx2;
b1 = m_filter.cy1;
b2 = m_filter.cy2;
/*
* a0*z^2 + a1*z + a2
* H(z) = ------------------ | z = e ^ (j*2*pi*f)
* z^2 - b1*z - b0
*/
std::complex<double> h;
std::complex<double> w;
std::complex<double> j(0.0,1.0);
std::complex<double> z;
w = f;
z = std::exp(j*w);
// get h[z] at z=e^jw
h = 0.95 * (a0 * (z*z) + (a1*z) + a2) / ((z*z) - (b1*z) - b2);
return sqrt(std::norm(h));
}
//***************************************************************************
void Kwave::BandPass::initFilter()
{
m_filter.x1 = 0.0;
m_filter.x2 = 0.0;
m_filter.y1 = 0.0;
m_filter.y2 = 0.0;
m_filter.y = 0.0;
}
//***************************************************************************
/*
* As in ''An introduction to digital filter theory'' by Julius O. Smith
* and in Moore's book; I use the normalized version in Moore's book.
*/
void Kwave::BandPass::setfilter_2polebp(double freq, double R)
{
m_filter.cx = 1.0 - R;
m_filter.cx1 = 0.0;
m_filter.cx2 = - (1.0 - R) * R;
m_filter.cy1 = 2.0 * R * cos(freq);
m_filter.cy2 = -R * R;
}
//***************************************************************************
void Kwave::BandPass::input(Kwave::SampleArray data)
{
const Kwave::SampleArray &in = data;
bool ok = m_buffer.resize(in.size());
Q_ASSERT(ok);
Q_UNUSED(ok)
setfilter_2polebp(m_frequency, m_bandwidth);
Q_ASSERT(in.size() == m_buffer.size());
for (unsigned i = 0; i < in.size(); ++i)
{
// do the filtering
m_filter.x = sample2double(in[i]);
m_filter.y =
m_filter.cx * m_filter.x +
m_filter.cx1 * m_filter.x1 +
m_filter.cx2 * m_filter.x2 +
m_filter.cy1 * m_filter.y1 +
m_filter.cy2 * m_filter.y2;
m_filter.x2 = m_filter.x1;
m_filter.x1 = m_filter.x;
m_filter.y2 = m_filter.y1;
m_filter.y1 = m_filter.y;
m_buffer[i] = double2sample(0.95 * m_filter.y);
}
}
//***************************************************************************
void Kwave::BandPass::setFrequency(const QVariant fc)
{
double new_freq = QVariant(fc).toDouble();
if (qFuzzyCompare(new_freq, m_frequency)) return; // nothing to do
m_frequency = new_freq;
initFilter();
setfilter_2polebp(m_frequency, m_bandwidth);
}
//***************************************************************************
void Kwave::BandPass::setBandwidth(const QVariant bw)
{
double new_bw = QVariant(bw).toDouble();
if (qFuzzyCompare(new_bw, m_bandwidth)) return; // nothing to do
m_bandwidth = new_bw;
initFilter();
setfilter_2polebp(m_frequency, m_bandwidth);
}
//***************************************************************************
//***************************************************************************
#include "moc_BandPass.cpp"
|