iir.c

来自「DSK6713的IIR滤波器的程序 可以用来做为IIR滤波」· C语言 代码 · 共 157 行

C
157
字号

#include "short6713cfg.h"
#include "dsk6713.h"
#include "dsk6713_aic23.h"

#include <stdio.h>
#include <math.h>
/*
  ------------------------------------------------------------------------------
  Defines
  ------------------------------------------------------------------------------
*/
  #define pi 3.1415926
  #define Length 512 
    
  float fs,nlpass,nlstop,nhpass,nhstop,a[3],b[3],x,y,x_in;
  float in[Length],out[Length];
/*
  ------------------------------------------------------------------------------
  Function prototyping
  ------------------------------------------------------------------------------
*/

/* Codec configuration settings */
DSK6713_AIC23_Config config = { \
    0x0017,  /* 0 DSK6713_AIC23_LEFTINVOL  Left line input channel volume */ \
    0x0017,  /* 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume */\
    0x01f9,  /* 2 DSK6713_AIC23_LEFTHPVOL  Left channel headphone volume */  \
    0x01f9,  /* 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume */ \
    0x0011,  /* 4 DSK6713_AIC23_ANAPATH    Analog audio path control */      \
    0x0000,  /* 5 DSK6713_AIC23_DIGPATH    Digital audio path control */     \
    0x0000,  /* 6 DSK6713_AIC23_POWERDOWN  Power down control */             \
    0x0043,  /* 7 DSK6713_AIC23_DIGIF      Digital audio interface format */ \
    0x0081,  /* 8 DSK6713_AIC23_SAMPLERATE Sample rate control */            \
    0x0001   /* 9 DSK6713_AIC23_DIGACT     Digital interface activation */   \
};

/*
  ------------------------------------------------------------------------------
  Global variables
  ------------------------------------------------------------------------------
*/
	int k=0;
	int n, ch;
	float w2=0.0,w1=0.0,w0=0.0;
// Left and right signal samples
Uint32 xL, xR;
//================================================================       

void biir2lpdes(float fs, float nlpass, float nlstop, float a[], float b[]);
/*
 *  main() - Main code routine, initializes BSL and connects input samples
 *           to output samples in an infinite while loop.
 */

/* The main programme of IIR 2-order filter  */

void main()
{
    // Codec data handle structure
    DSK6713_AIC23_CodecHandle hCodec;
	//Define filter variables
    /* Initialize the board support library, must be called first */
    DSK6713_init();
     
    /* Start the codec */
    hCodec = DSK6713_AIC23_openCodec(0, &config);
    // Set sampling frequency via the number before KHZ in the define.
    // Choose from 8, 16, 24, 32, 44.1, 48, or 96 Khz.
    DSK6713_AIC23_setFreq(hCodec, DSK6713_AIC23_FREQ_32KHZ);

	ch = 0;

	fs = 32000;
	
/*  IIR LP  filter  */
	
	if (ch == 0)	
	{
		nlpass = 0.022;
		nlstop = 0.222;
	}
	
	else
	{
		nlpass = 0.29;
		nlstop = 0.49;
	}
		
	biir2lpdes(fs,nlpass,nlstop,a,b);
	
// Infinite loop so that we can continue to process samples forever 
    while(1)
    {
		// read A/D

		while (!DSK6713_AIC23_read(hCodec, &xL));
      	while (!DSK6713_AIC23_read(hCodec, &xR));

		x_in = (Int16) xL;
		in[k] = (Int16) xL;
		
		x = (float)x_in;
								
		w2 = x-a[1]*w1-a[2]*w0;
      	y = b[0]*w2+b[1]*w1+b[2]*w0;
      	w0 = w1;
      	w1 = w2;
      	
      	out[k] = y;
      	xL = (int)(y);
      	      		
		while (!DSK6713_AIC23_write(hCodec, xL));
        while (!DSK6713_AIC23_write(hCodec, xL));
		
		k++;
		k = k%Length;
		
		if (k == 0) 
		{
			n = 0;
		}	
    }

    /* Close the codec in theory, but unreachable with the above while loop*/
    //DSK6713_AIC23_closeCodec(hCodec);  
}	

//================================================================     
//================================================================     

  void biir2lpdes(float fs, float nlpass, float nlstop, float a[], float b[])
  
  {
    int i,u,v;
	float wp,omp,gsa,t;
    wp=nlpass*2*pi;
	omp=tan(wp/2.0);
	gsa=omp*omp;
	for (i=0; i<=2; i++)
	  {
		u=i%2;
		v=i-1;
		a[i]=gsa*pow(2,u)-sqrt(2)*omp*v+pow(-2,u);
	  }
	
	for (i=0; i<=2; i++)
	  {	u=i%2;
		b[i]=gsa*pow(2,u);
	  }
	t=a[0];
	for (i=0; i<=2; i++)
	  {	a[i]=a[i]/t;
		b[i]=b[i]/t;
	  }
  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?