Class: Roctave::IirFilter

Inherits:
Object
  • Object
show all
Includes:
Filter
Defined in:
lib/roctave/iir.rb,
ext/roctave/iir_filter.c

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Filter

#cascade

Constructor Details

#initialize(n) ⇒ Object #initialize(b, a) ⇒ Object

Overloads:

  • #initialize(n) ⇒ Object

    Parameters:

    • n (Integer)

      the filter order

  • #initialize(b, a) ⇒ Object

    Parameters:

    • b (Array<Float>)

      the array of numerator coefficients

    • a (Array<Float>)

      the array of denominator coefficients



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

Returns:

See Also:



60
61
62
# File 'lib/roctave/iir.rb', line 60

def self.butter (*args)
	Roctave::IirFilter.new *Roctave.butter(*args)
end

.cheby1(*args) ⇒ Roctave::IirFilter

Returns a Chebyshev type I IIR filter

Returns:

See Also:



67
68
69
# File 'lib/roctave/iir.rb', line 67

def self.cheby1 (*args)
	Roctave::IirFilter.new *Roctave.cheby1(*args)
end

.cheby2(*args) ⇒ Roctave::IirFilter

Returns a Chebyshev type II IIR filter

Returns:

See Also:



74
75
76
# File 'lib/roctave/iir.rb', line 74

def self.cheby2 (*args)
	Roctave::IirFilter.new *Roctave.cheby2(*args)
end

Instance Method Details

#cloneRoctave::IirFilter

Returns:



53
54
55
# File 'lib/roctave/iir.rb', line 53

def clone
	Roctave::IirFilter.new numerator, denominator
end

#denominatorArray<Float>

Return a copy of the denominator coefficients as an array.

Returns:

  • (Array<Float>)

    the filter a coefficients



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.

Parameters:

  • sample (Float, Array<Float>)

    a single sample or an array of samples to process

Returns:

  • (Float, Array<Float>)

    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

See Also:



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.

Parameters:

  • index (Integer)

    the coefficient index

Returns:

  • (Float)

    the coefficient at index



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.

Parameters:

  • index (Integer)

    the coefficient index

Returns:

  • (Float)

    the coefficient at index



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.

Parameters:

  • len (Integer) (defaults to: 100)

    the length of the displayed response

See Also:



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

#numeratorArray<Float>

Return a copy of the numerator coefficients as an array.

Returns:

  • (Array<Float>)

    the filter b coefficients



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;
}

#orderInteger

Returns the filter order.

Returns:

  • (Integer)

    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.

Returns:

  • self



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.

Parameters:

  • index (Integer)

    the coefficient index, must be > 0

  • coef (Float)

    the coefficient value

Returns:

  • (Float, nil)

    the coefficient if the index is valid, or nil otherwise



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.

Parameters:

  • index (Integer)

    the coefficient index

  • coef (Float)

    the coefficient value

Returns:

  • (Float, nil)

    the coefficient if the index is valid, or nil otherwise



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;
}

#step_response(len = 100, *args) ⇒ Object

Draw the step response.

Parameters:

  • len (Integer) (defaults to: 100)

    the length of the displayed response

See Also:



41
42
43
44
45
# File 'lib/roctave/iir.rb', line 41

def step_response (len = 100, *args)
	x = Array.new([1, len.to_i].max){1.0}
	y = self.clone.filter x
	Roctave.stem(y, *args)
end

#zplaneObject

See Also:



48
49
50
# File 'lib/roctave/iir.rb', line 48

def zplane
	Roctave.zplane(numerator, denominator)
end