//在DSP算法设计中常需要把浮点运算转化成定点运算,为什么在下面的代码中fresult和sresult的的运算结果不等 //请高手指教,谢谢!!!!!
void fixPiontTest(short *in_data,int n ) { int i; float fw1,fw2,fw3,fw0,fw; short sw0,sw1,sw2; short fresult,sresult; short w[4]; short fwf[4]; short x;
fw0=5625636.482914; fw1=16796184.027028; fw2=-16716106.802805; fw3=5545557.1237500 ; fw=1.0-((fw1+fw2+fw3)/fw0); // fsw0=2.985650; // fsw1=-2.971416; // fsw2=0.985765; sw0 =(short) ( fw1/fw0 * 8192 ); //Q13 sw1 =(short) ( fw2/fw0 * 8192 ); //Q13 sw2 = (short)( fw3/fw0 * 8192 ); //Q13 w[0]=w[1]=w[2]=in_data[0]; fwf[0]=fwf[1]=fwf[2]=in_data[0]; for(i=0;i<n;i++) { x=in_data; fresult=(short)(x*fw+(fw1*fwf[2]+fw2*fwf[1]+fw3*fwf[0])/fw0); fwf[0]=fwf[1]; fwf[1]=fwf[2]; fwf[2]=fresult; sresult=(short)(x + ((sw0*(w[2]-x)+sw1*(w[1]-x)+sw2*(w[0]-x)) >>13) ); w[0]=w[1]; w[1]=w[2]; w[2]=sresult; if(fresult!=sresult) printf("f %d,s %d
",fresult,sresult); } } |