/// <summary> /// Designs an IIR filter with given specifications /// </summary> /// <param name="filter">filter type (butter, cheby etc.)</param> /// <param name="band">filter passband type(Lowpass, Highpass etc.)</param> /// <param name="format">output format (SOS/TF)</param> /// <param name="order">order of the filter</param> /// <param name="fc">cut-off frequency</param> /// <param name="f0">center frequency</param> /// <param name="Ap">passband ripple(dB)</param> /// <param name="As">stopband ripple(dB)</param> /// <returns>IIRFilter object containing filter co-efficients</returns> public static Filters.IIRFilter DesignIIR(Filters.IIR_FilterType filter, Filters.IIR_BandType band, int order, float fc, float f0, float Ap, float As) { // derived values : compute filter length int N = order; // effective order // filter order effectively doubles for band-pass, band-stop // filters due to doubling the number of poles and zeros as // a result of filter transformation if (band == Filters.IIR_BandType.BANDPASS || band == Filters.IIR_BandType.BANDSTOP) { N *= 2; } int r = N % 2; // odd/even order int L = (N - r) / 2; // filter semi-length float[] num = { }; float[] den = { }; // validate input if (fc <= 0 || fc >= 0.5) { throw new Exception("cutoff frequency out of range"); } else if (f0 < 0 || f0 > 0.5) { throw new Exception("center frequency out of range"); } else if (Ap <= 0) { throw new Exception("pass-band ripple out of range"); } else if (As <= 0) { throw new Exception("stop-band ripple out of range"); } unsafe { int h_len = N + 1; float *_B = stackalloc float[h_len]; float *_A = stackalloc float[h_len]; libliquid.liquid_iirdes(filter, band, IIR_Format.TF, order, fc, f0, Ap, As , _B, _A); num = new float[h_len]; den = new float[h_len]; num = libliquid.Create <float>(_B, h_len); den = libliquid.Create <float>(_A, h_len); } IIRFilter f = new IIRFilter(num, den); return(f); }
internal static extern void liquid_iirdes(Filters.IIR_FilterType _ftype, Filters.IIR_BandType _btype, Filters.IIR_Format _format, int _n, float _fc, float _f0, float _Ap, float _As, float *_B, float *_A);