⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fir.c

📁 dsp arm的很多讲义和说明。 关于怎样使用ccs bios以及matlab到dsp的代码转化
💻 C
字号:
/***************************************************************************
** $Id: BP.C 1.2 1998/09/01 18:20:18 trey Exp trey $
****************************************************************************
** Copyright (c) 1998 Analog Devices, Inc.    All Rights Reserved
****************************************************************************
**                                                 $DOCNUM:1125-01-001-8104$
** Revision History
** ----------------
** $Log: /Mainline/DebugTools/SHARC/EZLABZVLC/Demos/Bp/BP.C $
 * 
 * 2     6/25/01 2:08p Amehta
 * 6/25/2001 - Changed BP.c  to #include 21065L.h
 *                   - Changed Demorth.asm comment styles from ! to //
 *                   - Changed Buffer.asm comment styles from ! to //
 *                   - Checked in a new .dxe file after the above changes
 * 
 * 1     3/29/99 4:06p Nyee
 * 
 * 5     2/08/99 1:36p Kitbuilder
 * nmw: check in from build w90208a
 * 
 * 4     2/03/99 10:20a Kitbuilder
 * nmw: check in from doing  build w90203a
 * 
 * 3     1/25/99 2:15p Kitbuilder
 * 
 * 2     1/20/99 2:53p Nconlif
 * Paragom V1.0 stuff
** Revision 1.2  1998/09/01 18:20:18  trey
** Added run-time header file
** Revision 1.1  1998/09/23 16:00:46  trey
** Initial revision
**
****************************************************************************
**                       $TITLE: BP.C$
** BP.C
** ---------
**    Brings data in from the codec, runs it through a digital bandpass
**    filter, and then outputs the data through the codec line out.  The
**    filter coefficients are in the file: fir.h
**
****************************************************************************/
/*--------------------------------------------------------------------------
    INCLUDES
--------------------------------------------------------------------------*/
#include <def21060.h>
#include <21065l.h>
#include <signal.h>
#include <macros.h>
#include <math.h>
#include <filters.h>
#include <ezkit/1819regs.h>


/*--------------------------------------------------------------------------
   CONSTANT & MACRO DEFINITIONS
--------------------------------------------------------------------------*/
/* Codec tag word to send out left and right samples */
#define DOUT_TAG         0x9800

/* Codec tag word to send out address and data slots */
#define REGOUT_TAG       0xe000

/* This is the codec register setting for line input on both channels */
#define SOURCE_IS_LINE   0x0404
#define SOURCE_IS_MIC    0

/* Codec addreses */
#define SOURCE_ADDR      0x1a00
#define RECGAIN_ADDR     0x1c00

#define 	FIR_TAPS 	256//滤波器阶数
#define   	PI 			3.1415926
#define   	FC2   		0.05//以采样率Fs进行归一化的高频截止频率
#define   	FC1   		0.00//以采样率Fs进行归一化的低频截止频率
#define   	FL			0.2//移位量

#define CODEC_ISR_VECT 0X9001

/*--------------------------------------------------------------------------
   EXTERNAL DECLARATIONS
--------------------------------------------------------------------------*/
/* These compile to the same addresses as in the kernel if the .ldf is set
   up correctly  */
extern volatile int user_tx_buf[6];
extern volatile int user_tx_ready;
extern volatile int user_rx_buf[6];
extern volatile int user_rx_ready;

/*--------------------------------------------------------------------------
   GLOBAL DECLARATIONS
--------------------------------------------------------------------------*/
/* This variable lets the debugger know that this is the bandpass demo */
int BANDPASS;

/* This array holds the filter coefficients */
float pm h[FIR_TAPS];
float dm state[FIR_TAPS+1];

int Window;//加窗标志
int Shift;//频域移位标志
int Compress;//时域压缩标志

/* The host can set these 2 variables */
static int source=0;   /* 0 = codec, 1 = noise */
static int filter=1;  

/*--------------------------------------------------------------------------
   FUNCTION PROTOTYPES
--------------------------------------------------------------------------*/
void init_codec( void );
void Init_coeff(void);
void main (void);

/****************************************************************************
**
** Procedure:  main()
**
** Arguments:  None
**
** Returns:    None
**
** Desc:       main code
**
****************************************************************************/
void main ( void )
{
    int i;
    float filter_input;
    
    Window=1;
   	Shift=0;
   	Compress=0;    
    // Initialize state array for FIR filter.
    for( i=0 ; i<FIR_TAPS+1 ; i++ )
        state[i] = 0.0;
    // Initialize FIR's coeff.
    Init_coeff();
    
        // Initialize some codec registers.
    init_codec();
    // Loop forever.
   
   user_tx_ready = 0;
   for(;;)
   {
      user_rx_ready = 1;
      idle();
      while (user_rx_ready);
      while (user_tx_ready);
 
      // Get sample from Codec or random noise.    
      if( source == 0 )
      {
         filter_input = user_rx_buf[LEFT_CHANL];
      }
      else
      {
         filter_input = rand() & 0x00001fff;
      }

      // Filter sample and output.
      if( filter )
      {
         user_tx_buf[RIGHT_CHNL] = fir( filter_input, &h[0], &state[0], (int)FIR_TAPS );
         user_tx_buf[LEFT_CHANL] = user_tx_buf[RIGHT_CHNL];
      }
      else
      {
         user_tx_buf[RIGHT_CHNL] = filter_input;
         user_tx_buf[LEFT_CHANL] = filter_input;
      }
      user_tx_buf[TAG] = DOUT_TAG;
      user_tx_ready = 1;

   };
}

/****************************************************************************
**
** Procedure:  init_codec()
**
** Arguments:  None
**
** Returns:    None
**
** Desc:       Unmasks the sport1 interrupt and uses the kernel isr
**             to initialize the codec input source and record gain.
**
****************************************************************************/
void init_codec( void )
{
   asm("#include <def21065l.h>");
   interrupt(SIG_SPT1I,(void (*)(int))CODEC_ISR_VECT);
   asm("BIT SET IMASK SPT1I;");   /* unmasks sport interrupt */
    
   /* Set source to LINE */
   user_tx_buf[TAG] = REGOUT_TAG;
   user_tx_buf[ADDR] = SOURCE_ADDR;
   user_tx_buf[DATA] = SOURCE_IS_LINE;
   user_tx_ready = 1;                 /* Tell the isr that txbuf is ready */
   idle();
   idle();

   /* Set record gain */
   user_tx_buf[TAG] = REGOUT_TAG;
   user_tx_buf[ADDR] = RECGAIN_ADDR;
   user_tx_buf[DATA] = 0;
   user_tx_ready = 1;                 /* Tell the isr that txbuf is ready */
   idle();
   idle();
  
   return;
}

/****************************************************************************
**
** Procedure:  void Init_coeff(void)
**
** Arguments:  None
**
** Returns:    None
**
** Desc:       Initial the FIR's coeff.
**
****************************************************************************/
void Init_coeff(void)
{
float hd[FIR_TAPS];//理想低通滤波器的冲击响应
float W[FIR_TAPS];	//窗函数
float cosine[FIR_TAPS];//移位因子
int i;
//////////////滤波器系数产生///////////////
   	for(i=0;i<(FIR_TAPS-1)/2;i++)
	 	hd[i]=(1/PI)*(sin(2*FC2*PI*(i-(FIR_TAPS-1)/2))-sin(2*FC1*PI*(i-(FIR_TAPS-1)/2)))/(i-(FIR_TAPS-1)/2);
   	for(i=(FIR_TAPS-1)/2+1;i<FIR_TAPS;i++)
	 	hd[i]=(1/PI)*(sin(2*FC2*PI*(i-(FIR_TAPS-1)/2))-sin(2*FC1*PI*(i-(FIR_TAPS-1)/2)))/(i-(FIR_TAPS-1)/2);
	hd[(FIR_TAPS-1)/2]=2*(FC2-FC1);
///////////时域截取////////////
	for(i=0;i<FIR_TAPS;i++) h[i]=hd[i];
///////////时域加窗///////////////
if(Window)
	{
	for(i=0;i<FIR_TAPS;i++)	W[i]=0.54-0.46*cos(2*PI*i/(FIR_TAPS-1));
    for(i=0;i<FIR_TAPS;i++)	h[i]=h[i]*W[i];
	}
///////////时域压缩系数///////////
if(Compress)
	{
	for (i=0;i<FIR_TAPS/2;i++) hd[i]=h[i*2];
	for (i=0;i<FIR_TAPS/2;i++) h[i]=hd[i];
	for (i=FIR_TAPS/2;i<FIR_TAPS;i++) h[i]=0;//序列右边填零
	}
/////////////移位FL*fs///////////
if(Shift)
	{ 
	for (i=0;i<FIR_TAPS;i++)	cosine[i]=cos(2*PI*i*FL);
	for(i=0;i<FIR_TAPS;i++)     h[i]=h[i]*cosine[i];
	}
}

⌨️ 快捷键说明

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