?? fir.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 + -