Class: Roctave::IirFilter
- Inherits:
-
Object
- Object
- Roctave::IirFilter
- Includes:
- Filter
- Defined in:
- lib/roctave/iir.rb,
ext/roctave/iir_filter.c
Class Method Summary collapse
-
.butter(*args) ⇒ Roctave::IirFilter
Returns a Butterworth IIR filter.
-
.cheby1(*args) ⇒ Roctave::IirFilter
Returns a Chebyshev type I IIR filter.
-
.cheby2(*args) ⇒ Roctave::IirFilter
Returns a Chebyshev type II IIR filter.
Instance Method Summary collapse
- #clone ⇒ Roctave::IirFilter
-
#denominator ⇒ Array<Float>
Return a copy of the denominator coefficients as an array.
-
#filter(sample) ⇒ Float+
A single or an array of processed samples.
- #freqz(*args) ⇒ Object
-
#get_den_coefficient_at(index) ⇒ Float
Get a denominator coefficient.
-
#get_num_coefficient_at(index) ⇒ Float
Get a numerator coefficient.
-
#impulse_response(len = 100, *args) ⇒ Object
Draw the impulse response.
- #initialize(*args) ⇒ Object constructor
-
#numerator ⇒ Array<Float>
Return a copy of the numerator coefficients as an array.
-
#order ⇒ Integer
The filter order.
-
#reset! ⇒ Object
Reset the filter state.
-
#set_den_coefficient_at(index, coef) ⇒ Float?
Set a denominator coefficient.
-
#set_num_coefficient_at(index, coef) ⇒ Float?
Set a numerator coefficient.
-
#step_response(len = 100, *args) ⇒ Object
Draw the step response.
- #zplane ⇒ Object
Methods included from Filter
Constructor Details
#initialize(n) ⇒ Object #initialize(b, a) ⇒ Object
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
# File 'ext/roctave/iir_filter.c', line 370
static VALUE iir_filter_initialize(int argc, VALUE *argv, VALUE self)
{
struct iir_filter *flt;
VALUE b_or_n;
VALUE a;
double dv;
int i, lenb, lena = 0, len;
rb_scan_args(argc, argv, "11", &b_or_n, &a);
TypedData_Get_Struct(self, struct iir_filter, &iir_filter_type, flt);
if (rb_class_of(b_or_n) == rb_cInteger) {
if (argc > 1)
rb_raise(rb_eArgError, "If the order is given as the first argument, no other argument is expected");
flt->nb_coefficients = NUM2INT(b_or_n) + 1;
if (flt->nb_coefficients < 1)
rb_raise(rb_eArgError, "Order cannot be less than 0");
if (flt->nb_coefficients > 1048577)
rb_raise(rb_eArgError, "Order cannot be more than 1048576");
flt->b = calloc(flt->nb_coefficients, sizeof(double));
flt->a = calloc(flt->nb_coefficients, sizeof(double));
if (flt->b == NULL || flt->a == NULL)
rb_raise(rb_eRuntimeError, "Failed to allocate memory for coefficient storage");
flt->a[0] = 1.0;
}
else if (rb_class_of(b_or_n) == rb_cArray) {
lenb = rb_array_len(b_or_n);
len = lenb;
if (argc > 1) {
if (rb_class_of(a) == rb_cArray)
lena = rb_array_len(a);
else
rb_raise(rb_eArgError, "Expecting denominator coefficients (Array<Float>) as second argument");
if (len < lena)
len = lena;
}
if (len > 1048577)
rb_raise(rb_eArgError, "Cannot be more than 1048577 coefficients");
if (len < 1)
rb_raise(rb_eArgError, "Coefficient arrays are empty");
flt->nb_coefficients = len;
flt->b = calloc(flt->nb_coefficients, sizeof(double));
flt->a = calloc(flt->nb_coefficients, sizeof(double));
if (flt->b == NULL || flt->a == NULL)
rb_raise(rb_eRuntimeError, "Failed to allocate memory for coefficient storage");
flt->a[0] = 1.0;
if (lena > 1)
dv = NUM2DBL(rb_ary_entry(a, i));
else
dv = 1.0;
if (dv == 0.0)
rb_raise(rb_eArgError, "The first denominator coefficient may no be null");
for (i = 0; i < lenb; i++)
flt->b[i] = (double)(NUM2DBL(rb_ary_entry(b_or_n, i))) / dv;
for (i = 1; i < lena; i++)
flt->a[i] = (double)(NUM2DBL(rb_ary_entry(a, i))) / dv;
}
else
rb_raise(rb_eArgError, "Expecting the filter order (Integer) or the filter coefficients (two Array<Float>)");
flt->state_length = flt->nb_coefficients - 1;
flt->state = calloc(flt->state_length, sizeof(double));
if (!flt->state)
rb_raise(rb_eRuntimeError, "Failed to allocate %lu bytes of state storage", flt->state_length*sizeof(double));
flt->stateim = NULL;
flt->filter_one_sample_func = &filter_one_sample_float;
return self;
}
|
Class Method Details
.butter(*args) ⇒ Roctave::IirFilter
Returns a Butterworth IIR filter
60 61 62 |
# File 'lib/roctave/iir.rb', line 60 def self.butter (*args) Roctave::IirFilter.new *Roctave.butter(*args) end |
Instance Method Details
#clone ⇒ Roctave::IirFilter
53 54 55 |
# File 'lib/roctave/iir.rb', line 53 def clone Roctave::IirFilter.new numerator, denominator end |
#denominator ⇒ Array<Float>
Return a copy of the denominator coefficients as an array.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'ext/roctave/iir_filter.c', line 218
static VALUE iir_filter_a_coefficients(VALUE self)
{
struct iir_filter *flt;
VALUE res;
int i;
TypedData_Get_Struct(self, struct iir_filter, &iir_filter_type, flt);
res = rb_ary_new_capa(flt->nb_coefficients);
for (i = 0; i < flt->nb_coefficients; i++)
rb_ary_store(res, i, DBL2NUM(flt->a[i]));
return res;
}
|
#filter(sample) ⇒ Float+
Returns a single or an array of processed samples.
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'ext/roctave/iir_filter.c', line 296
static VALUE iir_filter_filter(VALUE self, VALUE sample)
{
struct iir_filter *flt;
VALUE res = Qnil;
int i, len;
TypedData_Get_Struct(self, struct iir_filter, &iir_filter_type, flt);
if (RB_FLOAT_TYPE_P(sample) || rb_obj_is_kind_of(sample, rb_cNumeric))
res = (*flt->filter_one_sample_func)(flt, sample);
else if (rb_class_of(sample) == rb_cArray) {
len = rb_array_len(sample);
res = rb_ary_new_capa(len);
for (i = 0; i < len; i++)
rb_ary_store(res, i, (*flt->filter_one_sample_func)(flt, rb_ary_entry(sample, i)));
}
else
rb_raise(rb_eArgError, "Expecting a single Numeric or an Array of Numeric");
return res;
}
|
#freqz(*args) ⇒ Object
24 25 26 |
# File 'lib/roctave/iir.rb', line 24 def freqz (*args) Roctave.freqz(numerator, denominator, *args) end |
#get_den_coefficient_at(index) ⇒ Float
Get a denominator coefficient.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'ext/roctave/iir_filter.c', line 178
static VALUE iir_filter_get_a_coefficient_at(VALUE self, VALUE index)
{
struct iir_filter *flt;
int ind;
VALUE res = Qnil;
TypedData_Get_Struct(self, struct iir_filter, &iir_filter_type, flt);
ind = NUM2INT(index);
if (ind >= 0 && ind < flt->nb_coefficients)
res = DBL2NUM(flt->a[ind]);
else
res = DBL2NUM(0.0);
return res;
}
|
#get_num_coefficient_at(index) ⇒ Float
Get a numerator coefficient.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'ext/roctave/iir_filter.c', line 131
static VALUE iir_filter_get_b_coefficient_at(VALUE self, VALUE index)
{
struct iir_filter *flt;
int ind;
VALUE res = Qnil;
TypedData_Get_Struct(self, struct iir_filter, &iir_filter_type, flt);
ind = NUM2INT(index);
if (ind >= 0 && ind < flt->nb_coefficients)
res = DBL2NUM(flt->b[ind]);
else
res = DBL2NUM(0.0);
return res;
}
|
#impulse_response(len = 100, *args) ⇒ Object
Draw the impulse response.
31 32 33 34 35 36 |
# File 'lib/roctave/iir.rb', line 31 def impulse_response (len = 100, *args) x = Array.new([1, len.to_i].max){0.0} x[0] = 1.0 y = self.clone.filter x Roctave.stem(y, *args) end |
#numerator ⇒ Array<Float>
Return a copy of the numerator coefficients as an array.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'ext/roctave/iir_filter.c', line 199
static VALUE iir_filter_b_coefficients(VALUE self)
{
struct iir_filter *flt;
VALUE res;
int i;
TypedData_Get_Struct(self, struct iir_filter, &iir_filter_type, flt);
res = rb_ary_new_capa(flt->nb_coefficients);
for (i = 0; i < flt->nb_coefficients; i++)
rb_ary_store(res, i, DBL2NUM(flt->b[i]));
return res;
}
|
#order ⇒ Integer
Returns the filter order.
92 93 94 95 96 97 98 99 |
# File 'ext/roctave/iir_filter.c', line 92
static VALUE iir_filter_order(VALUE self)
{
struct iir_filter *flt;
TypedData_Get_Struct(self, struct iir_filter, &iir_filter_type, flt);
return INT2NUM(flt->nb_coefficients - 1);
}
|
#reset! ⇒ Object
Reset the filter state.
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
# File 'ext/roctave/iir_filter.c', line 343
static VALUE iir_filter_reset(VALUE self)
{
struct iir_filter *flt;
int i;
TypedData_Get_Struct(self, struct iir_filter, &iir_filter_type, flt);
for (i = 0; i < flt->state_length; i++)
flt->state[i] = 0.0f;
if (flt->stateim) {
for (i = 0; i < flt->state_length; i++)
flt->stateim[i] = 0.0f;
}
flt->filter_one_sample_func = &filter_one_sample_float;
return self;
}
|
#set_den_coefficient_at(index, coef) ⇒ Float?
Set a denominator coefficient.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'ext/roctave/iir_filter.c', line 154
static VALUE iir_filter_set_a_coefficient_at(VALUE self, VALUE index, VALUE coef)
{
struct iir_filter *flt;
int ind;
double val;
VALUE res = Qnil;
TypedData_Get_Struct(self, struct iir_filter, &iir_filter_type, flt);
ind = NUM2INT(index);
if (ind > 0 && ind < flt->nb_coefficients) {
val = NUM2DBL(coef);
flt->a[ind] = val;
res = DBL2NUM(val);
}
return res;
}
|
#set_num_coefficient_at(index, coef) ⇒ Float?
Set a numerator coefficient.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'ext/roctave/iir_filter.c', line 107
static VALUE iir_filter_set_b_coefficient_at(VALUE self, VALUE index, VALUE coef)
{
struct iir_filter *flt;
int ind;
double val;
VALUE res = Qnil;
TypedData_Get_Struct(self, struct iir_filter, &iir_filter_type, flt);
ind = NUM2INT(index);
if (ind >= 0 && ind < flt->nb_coefficients) {
val = NUM2DBL(coef);
flt->b[ind] = val;
res = DBL2NUM(val);
}
return res;
}
|