funcs/func_resonance.c
static FILTER *create_filter(int freq)
{
	FILTER *iir;
	float	*coef;
	double fs, fc; /* Sampling frequency, cutoff frequency */
	double Q; /* Resonance > 1.0 < 1000 */
	unsigned nInd;
	double a0, a1, a2, b0, b1, b2;
	double k; /* overall gain factor */

	iir = ast_calloc(1, sizeof(*iir));

	if (!iir) {
		ast_log(LOG_WARNING, "Failed to allocate filter\n");
		return NULL;
	}

	/*
	* Setup filter s-domain coefficients
	*/
	/* Section 1 */ 
	ProtoCoef[0].a0 = 1.0;
	ProtoCoef[0].a1 = 0;
	ProtoCoef[0].a2 = 0;
	ProtoCoef[0].b0 = 1.0;
	ProtoCoef[0].b1 = 0.765367;
	ProtoCoef[0].b2 = 1.0;

	/* Section 2 */ 
	ProtoCoef[1].a0 = 1.0;
	ProtoCoef[1].a1 = 0;
	ProtoCoef[1].a2 = 0;
	ProtoCoef[1].b0 = 1.0;
	ProtoCoef[1].b1 = 1.847759;
	ProtoCoef[1].b2 = 1.0;

	iir->length = FILTER_SECTIONS; /* Number of filter sections */

	/*
	* Allocate array of z-domain coefficients for each filter section
	* plus filter gain variable
	*/
	iir->coef = (float *) ast_calloc(4 * iir->length + 1, sizeof(float));
	if (!iir->coef) {
		ast_free(iir);
		ast_log(LOG_WARNING, "Unable to allocate coef array, exiting\n");
		return NULL;
	}

	k = 1.0; /* Set overall filter gain */
	coef = iir->coef + 1; /* Skip k, or gain */

	Q = 1; /* Resonance */
	fc = freq; /* Filter cutoff (Hz) */
	fs = 8000; /* Sampling frequency (Hz) */

	/*
	* Compute z-domain coefficients for each biquad section
	* for new Cutoff Frequency and Resonance
	*/
	for (nInd = 0; nInd < iir->length; nInd++) {
		a0 = ProtoCoef[nInd].a0;
		a1 = ProtoCoef[nInd].a1;
		a2 = ProtoCoef[nInd].a2;

		b0 = ProtoCoef[nInd].b0;
		b1 = ProtoCoef[nInd].b1 / Q; /* Divide by resonance or Q */
		b2 = ProtoCoef[nInd].b2;
		szxform(&a0, &a1, &a2, &b0, &b1, &b2, fc, fs, &k, coef);
		coef += 4; /* Point to next filter section */
	}

	/* Update overall filter gain in coef array */
	iir->coef[0] = k;

	/* Display filter coefficients */
	for (nInd = 0; nInd < (iir->length * 4 + 1); nInd++)
		ast_debug(1, "C[%d] = %15.10f\n", nInd, iir->coef[nInd]);
	/*
	* To process audio samples, call function iir_filter()
	* for each audio sample
	*/
	return iir;
}
/*
* --------------------------------------------------------------------
* 
* main()
* 
* Example main function to show how to update filter coefficients.
* We create a 4th order filter (24 db/oct roloff), consisting
* of two second order sections.
* --------------------------------------------------------------------
*/
main()
Example main function to show how to update filter coefficients.
We create a 4th order filter (24 db/oct roloff), consisting
of two second order sections.
main () Example main function to show how to update filter coefficients . We create a 4th order filter ( 24 db / oct roloff ) consisting of two second order sections .
static FILTER *create_filter(int freq)
{
	FILTER *iir;
	float	*coef;
	double fs, fc; 
	double Q; 
	unsigned nInd;
	double a0, a1, a2, b0, b1, b2;
	double k; 
	iir = ast_calloc(1, sizeof(*iir));
	if (!iir) {
		ast_log(LOG_WARNING, "Failed to allocate filter\n");
		return NULL;
	}
	ProtoCoef[0].a0 = 1.0;
	ProtoCoef[0].a1 = 0;
	ProtoCoef[0].a2 = 0;
	ProtoCoef[0].b0 = 1.0;
	ProtoCoef[0].b1 = 0.765367;
	ProtoCoef[0].b2 = 1.0;
	ProtoCoef[1].a0 = 1.0;
	ProtoCoef[1].a1 = 0;
	ProtoCoef[1].a2 = 0;
	ProtoCoef[1].b0 = 1.0;
	ProtoCoef[1].b1 = 1.847759;
	ProtoCoef[1].b2 = 1.0;
	iir->length = FILTER_SECTIONS; 
	iir->coef = (float *) ast_calloc(4 * iir->length + 1, sizeof(float));
	if (!iir->coef) {
		ast_free(iir);
		ast_log(LOG_WARNING, "Unable to allocate coef array, exiting\n");
		return NULL;
	}
	k = 1.0; 
	coef = iir->coef + 1; 
	Q = 1; 
	fc = freq; 
	fs = 8000; 
	for (nInd = 0; nInd < iir->length; nInd++) {
		a0 = ProtoCoef[nInd].a0;
		a1 = ProtoCoef[nInd].a1;
		a2 = ProtoCoef[nInd].a2;
		b0 = ProtoCoef[nInd].b0;
		b1 = ProtoCoef[nInd].b1 / Q; 
		b2 = ProtoCoef[nInd].b2;
		szxform(&a0, &a1, &a2, &b0, &b1, &b2, fc, fs, &k, coef);
		coef += 4; 
	}
	iir->coef[0] = k;
	for (nInd = 0; nInd < (iir->length * 4 + 1); nInd++)
		ast_debug(1, "C[%d] = %15.10f\n", nInd, iir->coef[nInd]);
	return iir;
}
static FILTER * create_filter ( int freq ) { FILTER * iir ; float * coef ; double fs , fc ; double Q ; unsigned nInd ; double a0 , a1 , a2 , b0 , b1 , b2 ; double k ; iir = ast_calloc ( 1 , sizeof ( * iir ) ) ; if ( ! iir ) { ast_log ( LOG_WARNING , " \n " ) ; return NULL ; } ProtoCoef [ 0 ] . a0 = 1.0 ; ProtoCoef [ 0 ] . a1 = 0 ; ProtoCoef [ 0 ] . a2 = 0 ; ProtoCoef [ 0 ] . b0 = 1.0 ; ProtoCoef [ 0 ] . b1 = 0.765367 ; ProtoCoef [ 0 ] . b2 = 1.0 ; ProtoCoef [ 1 ] . a0 = 1.0 ; ProtoCoef [ 1 ] . a1 = 0 ; ProtoCoef [ 1 ] . a2 = 0 ; ProtoCoef [ 1 ] . b0 = 1.0 ; ProtoCoef [ 1 ] . b1 = 1.847759 ; ProtoCoef [ 1 ] . b2 = 1.0 ; iir -> length = FILTER_SECTIONS ; iir -> coef = ( float * ) ast_calloc ( 4 * iir -> length + 1 , sizeof ( float ) ) ; if ( ! iir -> coef ) { ast_free ( iir ) ; ast_log ( LOG_WARNING , " \n " ) ; return NULL ; } k = 1.0 ; coef = iir -> coef + 1 ; Q = 1 ; fc = freq ; fs = 8000 ; for ( nInd = 0 ; nInd < iir -> length ; nInd ++ ) { a0 = ProtoCoef [ nInd ] . a0 ; a1 = ProtoCoef [ nInd ] . a1 ; a2 = ProtoCoef [ nInd ] . a2 ; b0 = ProtoCoef [ nInd ] . b0 ; b1 = ProtoCoef [ nInd ] . b1 / Q ; b2 = ProtoCoef [ nInd ] . b2 ; szxform ( & a0 , & a1 , & a2 , & b0 , & b1 , & b2 , fc , fs , & k , coef ) ; coef += 4 ; } iir -> coef [ 0 ] = k ; for ( nInd = 0 ; nInd < ( iir -> length * 4 + 1 ) ; nInd ++ ) ast_debug ( 1 , " \n " , nInd , iir -> coef [ nInd ] ) ; return iir ; }
main()
Example main function to show how to update filter coefficients.
main () Example main function to show how to update filter coefficients .
/* Sampling frequency, cutoff frequency */ /* Resonance > 1.0 < 1000 */ /* overall gain factor */ /*
	* Setup filter s-domain coefficients
	*/ /* Section 1 */ /* Section 2 */ /* Number of filter sections */ /*
	* Allocate array of z-domain coefficients for each filter section
	* plus filter gain variable
	*/ /* Set overall filter gain */ /* Skip k, or gain */ /* Resonance */ /* Filter cutoff (Hz) */ /* Sampling frequency (Hz) */ /*
	* Compute z-domain coefficients for each biquad section
	* for new Cutoff Frequency and Resonance
	*/ /* Divide by resonance or Q */ /* Point to next filter section */ /* Update overall filter gain in coef array */ /* Display filter coefficients */ /*
	* To process audio samples, call function iir_filter()
	* for each audio sample
	*/