| 1 |
/***************************************************/ |
| 2 |
/*! \class Filter |
| 3 |
\brief STK filter class. |
| 4 |
|
| 5 |
This class implements a generic structure that |
| 6 |
can be used to create a wide range of filters. |
| 7 |
It can function independently or be subclassed |
| 8 |
to provide more specific controls based on a |
| 9 |
particular filter type. |
| 10 |
|
| 11 |
In particular, this class implements the standard |
| 12 |
difference equation: |
| 13 |
|
| 14 |
a[0]*y[n] = b[0]*x[n] + ... + b[nb]*x[n-nb] - |
| 15 |
a[1]*y[n-1] - ... - a[na]*y[n-na] |
| 16 |
|
| 17 |
If a[0] is not equal to 1, the filter coefficients |
| 18 |
are normalized by a[0]. |
| 19 |
|
| 20 |
The \e gain parameter is applied at the filter |
| 21 |
input and does not affect the coefficient values. |
| 22 |
The default gain value is 1.0. This structure |
| 23 |
results in one extra multiply per computed sample, |
| 24 |
but allows easy control of the overall filter gain. |
| 25 |
|
| 26 |
by Perry R. Cook and Gary P. Scavone, 1995 - 2005. |
| 27 |
*/ |
| 28 |
/***************************************************/ |
| 29 |
|
| 30 |
#include "Filter.h" |
| 31 |
#include <stdio.h> |
| 32 |
|
| 33 |
using namespace Nyq; |
| 34 |
|
| 35 |
Filter :: Filter() |
| 36 |
{ |
| 37 |
// The default constructor should setup for pass-through. |
| 38 |
gain_ = 1.0; |
| 39 |
b_.push_back( 1.0 ); |
| 40 |
a_.push_back( 1.0 ); |
| 41 |
|
| 42 |
inputs_.push_back( 0.0 ); |
| 43 |
outputs_.push_back( 0.0 ); |
| 44 |
} |
| 45 |
|
| 46 |
Filter :: Filter( std::vector<StkFloat> &bCoefficients, std::vector<StkFloat> &aCoefficients ) |
| 47 |
{ |
| 48 |
// Check the arguments. |
| 49 |
if ( bCoefficients.size() == 0 || aCoefficients.size() == 0 ) { |
| 50 |
errorString_ << "Filter: a and b coefficient vectors must both have size > 0!"; |
| 51 |
handleError( StkError::FUNCTION_ARGUMENT ); |
| 52 |
} |
| 53 |
|
| 54 |
if ( aCoefficients[0] == 0.0 ) { |
| 55 |
errorString_ << "Filter: a[0] coefficient cannot == 0!"; |
| 56 |
handleError( StkError::FUNCTION_ARGUMENT ); |
| 57 |
} |
| 58 |
|
| 59 |
gain_ = 1.0; |
| 60 |
b_ = bCoefficients; |
| 61 |
a_ = aCoefficients; |
| 62 |
|
| 63 |
inputs_ = std::vector<StkFloat> ( b_.size() ); |
| 64 |
outputs_ = std::vector<StkFloat> ( a_.size() ); |
| 65 |
this->clear(); |
| 66 |
} |
| 67 |
|
| 68 |
Filter :: ~Filter() |
| 69 |
{ |
| 70 |
} |
| 71 |
|
| 72 |
void Filter :: clear(void) |
| 73 |
{ |
| 74 |
unsigned int i; |
| 75 |
for (i=0; i<inputs_.size(); i++) |
| 76 |
inputs_[i] = 0.0; |
| 77 |
for (i=0; i<outputs_.size(); i++) |
| 78 |
outputs_[i] = 0.0; |
| 79 |
} |
| 80 |
|
| 81 |
void Filter :: setCoefficients( std::vector<StkFloat> &bCoefficients, std::vector<StkFloat> &aCoefficients, bool clearState ) |
| 82 |
{ |
| 83 |
// Check the arguments. |
| 84 |
if ( bCoefficients.size() == 0 || aCoefficients.size() == 0 ) { |
| 85 |
errorString_ << "Filter::setCoefficients: a and b coefficient vectors must both have size > 0!"; |
| 86 |
handleError( StkError::FUNCTION_ARGUMENT ); |
| 87 |
} |
| 88 |
|
| 89 |
if ( aCoefficients[0] == 0.0 ) { |
| 90 |
errorString_ << "Filter::setCoefficients: a[0] coefficient cannot == 0!"; |
| 91 |
handleError( StkError::FUNCTION_ARGUMENT ); |
| 92 |
} |
| 93 |
|
| 94 |
if ( b_.size() != bCoefficients.size() ) { |
| 95 |
b_ = bCoefficients; |
| 96 |
inputs_.clear(); |
| 97 |
inputs_ = std::vector<StkFloat> ( b_.size() ); |
| 98 |
} |
| 99 |
else { |
| 100 |
for ( unsigned int i=0; i<b_.size(); i++ ) b_[i] = bCoefficients[i]; |
| 101 |
} |
| 102 |
|
| 103 |
if ( a_.size() != aCoefficients.size() ) { |
| 104 |
a_ = aCoefficients; |
| 105 |
outputs_.clear(); |
| 106 |
outputs_ = std::vector<StkFloat> ( a_.size() ); |
| 107 |
} |
| 108 |
else { |
| 109 |
for ( unsigned int i=0; i<a_.size(); i++ ) a_[i] = aCoefficients[i]; |
| 110 |
} |
| 111 |
|
| 112 |
if ( clearState ) this->clear(); |
| 113 |
|
| 114 |
// Scale coefficients by a[0] if necessary |
| 115 |
if ( a_[0] != 1.0 ) { |
| 116 |
unsigned int i; |
| 117 |
for ( i=0; i<b_.size(); i++ ) b_[i] /= a_[0]; |
| 118 |
for ( i=1; i<a_.size(); i++ ) a_[i] /= a_[0]; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
void Filter :: setNumerator( std::vector<StkFloat> &bCoefficients, bool clearState ) |
| 123 |
{ |
| 124 |
// Check the argument. |
| 125 |
if ( bCoefficients.size() == 0 ) { |
| 126 |
errorString_ << "Filter::setNumerator: coefficient vector must have size > 0!"; |
| 127 |
handleError( StkError::FUNCTION_ARGUMENT ); |
| 128 |
} |
| 129 |
|
| 130 |
if ( b_.size() != bCoefficients.size() ) { |
| 131 |
b_ = bCoefficients; |
| 132 |
inputs_.clear(); |
| 133 |
inputs_ = std::vector<StkFloat> ( b_.size() ); |
| 134 |
} |
| 135 |
else { |
| 136 |
for ( unsigned int i=0; i<b_.size(); i++ ) b_[i] = bCoefficients[i]; |
| 137 |
} |
| 138 |
|
| 139 |
if ( clearState ) this->clear(); |
| 140 |
} |
| 141 |
|
| 142 |
void Filter :: setDenominator( std::vector<StkFloat> &aCoefficients, bool clearState ) |
| 143 |
{ |
| 144 |
// Check the argument. |
| 145 |
if ( aCoefficients.size() == 0 ) { |
| 146 |
errorString_ << "Filter::setDenominator: coefficient vector must have size > 0!"; |
| 147 |
handleError( StkError::FUNCTION_ARGUMENT ); |
| 148 |
} |
| 149 |
|
| 150 |
if ( aCoefficients[0] == 0.0 ) { |
| 151 |
errorString_ << "Filter::setDenominator: a[0] coefficient cannot == 0!"; |
| 152 |
handleError( StkError::FUNCTION_ARGUMENT ); |
| 153 |
} |
| 154 |
|
| 155 |
if ( a_.size() != aCoefficients.size() ) { |
| 156 |
a_ = aCoefficients; |
| 157 |
outputs_.clear(); |
| 158 |
outputs_ = std::vector<StkFloat> ( a_.size() ); |
| 159 |
} |
| 160 |
else { |
| 161 |
for ( unsigned int i=0; i<a_.size(); i++ ) a_[i] = aCoefficients[i]; |
| 162 |
} |
| 163 |
|
| 164 |
if ( clearState ) this->clear(); |
| 165 |
|
| 166 |
// Scale coefficients by a[0] if necessary |
| 167 |
if ( a_[0] != 1.0 ) { |
| 168 |
unsigned int i; |
| 169 |
for ( i=0; i<b_.size(); i++ ) b_[i] /= a_[0]; |
| 170 |
for ( i=1; i<a_.size(); i++ ) a_[i] /= a_[0]; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void Filter :: setGain(StkFloat gain) |
| 175 |
{ |
| 176 |
gain_ = gain; |
| 177 |
} |
| 178 |
|
| 179 |
StkFloat Filter :: getGain(void) const |
| 180 |
{ |
| 181 |
return gain_; |
| 182 |
} |
| 183 |
|
| 184 |
StkFloat Filter :: lastOut(void) const |
| 185 |
{ |
| 186 |
return outputs_[0]; |
| 187 |
} |
| 188 |
|
| 189 |
StkFloat Filter :: tick( StkFloat input ) |
| 190 |
{ |
| 191 |
unsigned int i; |
| 192 |
|
| 193 |
outputs_[0] = 0.0; |
| 194 |
inputs_[0] = gain_ * input; |
| 195 |
for (i=b_.size()-1; i>0; i--) { |
| 196 |
outputs_[0] += b_[i] * inputs_[i]; |
| 197 |
inputs_[i] = inputs_[i-1]; |
| 198 |
} |
| 199 |
outputs_[0] += b_[0] * inputs_[0]; |
| 200 |
|
| 201 |
for (i=a_.size()-1; i>0; i--) { |
| 202 |
outputs_[0] += -a_[i] * outputs_[i]; |
| 203 |
outputs_[i] = outputs_[i-1]; |
| 204 |
} |
| 205 |
|
| 206 |
return outputs_[0]; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
StkFrames& Filter :: tick( StkFrames& frames, unsigned int channel ) |
| 211 |
{ |
| 212 |
if ( channel >= frames.channels() ) { |
| 213 |
errorString_ << "Filter::tick(): channel and StkFrames arguments are incompatible!"; |
| 214 |
handleError( StkError::FUNCTION_ARGUMENT ); |
| 215 |
} |
| 216 |
|
| 217 |
if ( frames.channels() == 1 ) { |
| 218 |
for ( unsigned int i=0; i<frames.frames(); i++ ) |
| 219 |
frames[i] = tick( frames[i] ); |
| 220 |
} |
| 221 |
else if ( frames.interleaved() ) { |
| 222 |
unsigned int hop = frames.channels(); |
| 223 |
unsigned int index = channel; |
| 224 |
for ( unsigned int i=0; i<frames.frames(); i++ ) { |
| 225 |
frames[index] = tick( frames[index] ); |
| 226 |
index += hop; |
| 227 |
} |
| 228 |
} |
| 229 |
else { |
| 230 |
unsigned int iStart = channel * frames.frames(); |
| 231 |
for ( unsigned int i=0; i<frames.frames(); i++, iStart++ ) |
| 232 |
frames[iStart] = tick( frames[iStart] ); |
| 233 |
} |
| 234 |
|
| 235 |
return frames; |
| 236 |
} |