/* Allocate sections to memory blocks.
Note:
codestart user defined section in DSP28_CodeStartBranch.asm used to redirect code
execution when booting to flash
ramfuncs user defined section to store functions that will be copied from Flash into RAM
*/
/* Allocate IQ math areas: */
IQmath : > FLASHC PAGE = 0 /* Math Code */
IQmathTables : > ROM PAGE = 0, TYPE = NOLOAD /* Math Tables In ROM */
IQmathTablesRam : > RAML1, PAGE = 1
/* .reset indicates the start of _c_int00 for C Code.
/* When using the boot ROM this section and the CPU vector
/* table is not needed. Thus the default type is set to
/* DESECT */
.reset : > RESET, PAGE = 0, TYPE = DSECT
vectors : > VECTORS PAGE = 0, TYPE = DSECT
/* ------------------------------------------------------------- */
/* The following allocations are required for the DSP28 Header file
examples. Each allocation maps a structure defined in the DSP28
header files to the memory location of those registers.
*/
//alocate the space for rfft
RFFT32 fft=RFFT32_512P_DEFAULTS;
#pragma DATA_SECTION(mag, "FFTmag");
long mag[N_input/2+1];
#pragma DATA_SECTION(mag_temp, "FFTmag1");
long mag_temp[N_input/2+1];
RFFT32_ACQ acq=FFTRACQ_DEFAULTS;
//end
#pragma DATA_SECTION(ipcb_ad0, "FFTipcb0");
long ipcb_ad0[N_input+2];
#pragma DATA_SECTION(ipcb_ad1, "FFTipcb1");
long ipcb_ad1[N_input+2];
#pragma DATA_SECTION(ipcb_ad2, "FFTipcb2");
long ipcb_ad2[N_input+2];
#pragma DATA_SECTION(ipcb_ad3, "FFTipcb3");
long ipcb_ad3[N_input+2];
#pragma DATA_SECTION(ipcb_ad4, "FFTipcb4");
long ipcb_ad4[N_input+2];
#pragma DATA_SECTION(ipcb_ad5, "FFTipcb5");
long ipcb_ad5[N_input+2];
#pragma DATA_SECTION(ipcb_ad6, "FFTipcb6");
long ipcb_ad6[N_input+2];
#pragma DATA_SECTION(ipcb_ad7, "FFTipcb7");
long ipcb_ad7[N_input+2];
unsigned int num_sampal;
//alocate the space for cfft
#pragma DATA_SECTION(ipcb_c, "FFTipcb_c");
CFFT32 fft_c=CFFT32_1024P_DEFAULTS;
long ipcb_c[2*N];
CFFT32_ACQ acq_c=CFFT32_ACQ_DEFAULTS;
//end
//function used to translate the Q31 format number to the Q30 format number
void Q31toQ30(long *temp,unsigned int n)
{
unsigned int i;
for(i=0;i
temp=temp>>1;
}
//function used to translate the Q31 format number to the Q30 format number
void Q30toQ31(long *temp,unsigned int n)
{
unsigned int i;
for(i=0;i
temp=(temp|0x80000000)|(temp<<1);
}
//function:FFT for real
//note:the input nad output are all the Q31 format
void FFT_real_initial(long *buffer,long *magnitude_buf,int numpoint)
{
//initial the acquire moudel
acq.buffptr=buffer;
acq.tempptr=buffer;
acq.size=numpoint;
acq.count=numpoint;
acq.acqflag=1;
//initial the FFT moudel
fft.ipcbptr=buffer;
fft.magptr=magnitude_buf;
fft.init(&fft);
}
//function:FFT for complex
//note:the input nad output are all the Q31 format
void CFFT_complex_initial(long *buffer,int numpoint)
{
//initial the acquire moudel
acq_c.buffptr=buffer;
acq_c.tempptr=buffer;
acq_c.size=numpoint;
acq_c.count=numpoint;
acq_c.acqflag=1;
//initial the FFT moudel
fft_c.ipcbptr=buffer;
fft_c.init(&fft_c);
}
//data format of rfft to data format of icfft
void rfft_to_icfft(long *data_rfft,long *data_cfft,int MM)
{
int i,temp;
temp=2*N_input;
for(i=0;i
{
data_cfft=data_rfft;
data_cfft[i+1]=-data_rfft[i+1];
// data_cfft=(data_cfft>>10)-((long)1023<<21);
}
data_cfft=data_rfft/4;
data_cfft[i+1]=data_rfft[i+1]/4;
i=i+2;
for(;i<(2*MM*N_input-N_input);i=i+1)
{
data_cfft=0;
}
data_cfft=data_rfft[N_input]/4;
data_cfft[i+1]=data_rfft[N_input+1]/4;
i=i+2;
for(;i<2*MM*N_input;i=i+2)
{
data_cfft=data_rfft[temp-(i-2*N_input)];
data_cfft[i+1]=data_rfft[temp+1-(i-2*N_input)];
}
}
//function:% this function is to use interpolation method
//to estimate the TDOA of signals.
//paramter: ch1:point to the buffer form ADC
// ch2:point to the buffer form ADC
// exp1:the normalized parameter
// MM:the number of M times interpolation,MM must be 2
// note:MM must be 2
int int_corr1(long *ch1,long *ch2,_iq24 exp1,int MM)
{
int i,max;
_iq30 temp_q30;
FFT_real_initial(ch1,mag,N_input);
FFT_real_compution(ch1);
Q31toQ30(ch1,N_input+2);
FFT_real_initial(ch2,mag_temp,N_input);
FFT_real_compution(ch2);
Q31toQ30(ch2,N_input+2);
//ch1*conjugate(ch2)
for(i=0;i
{
temp_q30=ch1;
ch1=_IQ30mpy(ch1,ch2)+_IQ30mpy(ch1[i+1],ch2[i+1]);
ch1[i+1]=_IQ30mpy(ch2,ch1[i+1])-_IQ30mpy(ch2[i+1],temp_q30);
i>>=1;
if(mag&&mag_temp)
{
mag=_IQ30mpy(_IQ30sqrt(mag),_IQ30sqrt(mag_temp));
mag=(long)qlogn((mag)>>14);//format 11
if(mag<0)
{
mag=(mag<<13)|0x80000000;////format 24
}
else
{
mag=(mag<<13);
}
mag=_IQ24exp(_IQ24mpy(exp1,mag));//format 24
mag=(mag<<6);//format 30
mag=mag+_IQ30(0.0001);
}
else
{
mag=_IQ30(0.0001);
}
i<<=1;
ch1=_IQ30div(ch1,mag[i>>1]);
ch1[i+1]=_IQ30div(ch1[i+1],mag[i>>1]);
}
// Q30toQ31(ch1,N+2);
rfft_to_icfft(ch1,ipcb_c,MM);
CFFT_complex_initial(ipcb_c,N_input*MM);
CFFT_complex_compution(ipcb_c);
//find the max
temp_q30=ipcb_c[0];
max=0;
for(i=2;i<2*N_input*MM;i=i+2)
{
if(temp_q30)
{
temp_q30=ipcb_c;
max=i;
}
}
max=max/2;
if(max<(N_input*MM/2))
max=max+N_input*MM/2;
else
max=max-N_input*MM/2;
return (max-N_input*MM/2);
}
//compute the noise of every channal
//the range of return value is between 0 and 4095
void get_noise(void)
{
}
//send data of long by RS232
void send_data_long(int data)
{
//unsigned char temp,i;
//for(i=0;i<4;i++)
//{
// temp=(unsigned char)(data&0x000000ff);
//data=data>>8;
while(ScibTx_Ready()==0);
ScibRegs.SCITXBUF =(data&0xff00>>8);
//}
}
void send_data(int data)
{
int tmp;
tmp=data;
while(ScibTx_Ready()==0);
ScibRegs.SCITXBUF =((tmp&0xff00)>>8);
while(ScibTx_Ready()==0);
ScibRegs.SCITXBUF =(data&0x00ff);
}
//compute the derection function
int compute_win(long *chn)//512points
{
int i;
long tmp;
for(i=0;i
{
tmp=tmp+chn;
}
tmp=tmp/512.0;
return(chn[0]);
}
void compute_direction(int index)
{
float tp;
long direction;
tp=index*c_TDOA/AD_sampling_freq;
tp=tp/(d_TDOA*MM_TDOA);
if(fabs(tp)<1.0)
tp=3.14/2-asin(tp);
direction=(int)(tp*180/3.14);
//the range of tp is between 0 and pi/2
//translate tp to Q29 foramt to send tp using uart
// direction=(long)(tp*0x20000000);
//send direction using uart
send_data(0xff);
send_data(direction);
//send_data_long(0xffffffff);
}