# 2.1.22.3.8 ocmsp_iir_filter_sos

## Description

Design digital or analog IIR filters with given specifications. It returns a filter in second-order section representation: $H(z) = g \prod_{k=0}^{L-1} {H_{k(z)}} = g \prod_{k=0}^{L-1} \frac{b_{0k}+b_{1k}*z^{-1}+b_{2k}*z^{-2}}{1+a_{1k}*z^{-1}+a_{2k}*z^{-2}}$

The result is saved in an L-by-6 matrix sos, and a scalar gain. $sos = \begin{vmatrix} b_{01} & b_{11} & b_{21} & 1 & a_{11} & a_{21} \\ b_{02} & b_{12} & b_{22} & 1 & a_{12} & a_{22} \\ \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\ b_{0L} & b_{1L} & b_{2L} & 1 & a_{1L} & a_{2L} \end{vmatrix}$

Each row in sos is the numerator and denominator coefficients $b_{ik}$ and $a_{ik}$ of the second order sections of H(z). Section number L is the closest integer greater than or equal to n/2 (L = (n+1)/2).

## Syntax

int ocmsp_iir_filter_sos( UINT N, double Wn, double * pSOS, double * pg, int type, double Rp = 0.0, double Rs = 0.0, double Wn2 = -1.0 )

## Parameters

N
[input] filter order, should be even if is bandpass or bandstop type
Wn
[input] cutoff or passband/stopband-edge frequency, it's in radians/s if is analog type, and should be normalized to [0, 1] if is digital type, where 1 corresponds to half the sample rate
pSOS
[output] coefficients matrix of second-order sections H(z), size nsec-by-6, nsec = (N+1)/2
pg
[output] returned gain, scalar
type
[intput] filter type, it specifies the filter prototype(Butterworth, Chebyshev Type I, Chebyshev Type II, Elliptic or Bessel), bandtype(lowpass, highpass, bandstop or bandpass) and digital or analog type. For example
OMSP_BUTTERWORTH|OMSP_LOWPASS|OMSP_DIGITAL means a lowpass Butterworth digital filter.
Rp
[input][optional] passband ripple (dB), needed when it's Chebyshev Type I or Elliptic type
Rs
[input][optional] stopband attenuation (dB), needed when it's Chebyshev Type II or Elliptic type
Wn2
[input][optional] cutoff or passband/stopband-edge frequency. Needed when it's a bandpass or bandstop
filter. It's in radians/second if is analog type, and should be normalized to [0, 1] if is digital type (where 1 corresponds to half the sample rate).

## Return

Return OE_NOERROR if succeed, otherwise, non-zero error code is returned (OE_NULL_POINTER, OE_INVALID_FREQ, OE_INVALID_BANDFREQS, OE_INVLAID_RIPPLE, OE_INVALID_BANDRIPPLES or OE_INVALID_TYPE)

## Examples

EX1

#include <ocmsp.h>
void ocmsp_iir_filter_sos_ex1()
{
UINT N = 18; //even number if bandpass or bandstop
double Wn = {300.0/500, 400.0/500};

UINT nsec = (N+1)/2; //size L = (n+1)/2;
matrix sos(nsec, 6);
double gain;
int type = OMSP_BUTTERWORTH|OMSP_BANDPASS|OMSP_DIGITAL;

int nRet = 0;
if (0 != (nRet = ocmsp_iir_filter_sos(N, Wn, sos, &gain, type, 0.0, 0.0, Wn)))
{
printf("ocmsp_iir_filter_sos failed, error code=%d\n", nRet);
return;
}
}