please I need help with calculation (logic problem)

Hi everyone,

I've been working on this thing for the last few days i'm suppose to get a number between 80 and 100 yet i get a number around 2. I'd appreciate if anyone could please tell me what is going on?

void linear ()
{
double xi, yi, ei, n=16.00, N= 1000.00, xi_sq=0.00, e_sq=0.00, rms_xi, rms_e, qsnr,test,test1=0.00;
for (int i=0; i<N; i++)
{
//first check if xi is x less than or equal to 1 or larger than or equall to -1
xi=((2*i)/ N)-1;
if ((xi>1) || (xi<-1))
cout << "not within a valid interval" <<endl;
else
{
xi_sq= xi_sq + pow(xi,2);
yi= xi * pow(2,n-1);
ei=fabs(yi-round(yi));
e_sq= e_sq + pow(ei,2);
}
}
test= fabs(xi_sq)/N;
rms_xi= sqrt(test);
rms_e=sqrt(e_sq/N);
qsnr= rms_xi/rms_e;
cout <<"linear result is "<<qsnr<<endl;
}
[904 byte] By [w1985] at [2007-11-20 11:16:21]
# 1 Re: please I need help with calculation (logic problem)
And how do you expect this forum to help you with the information you provide. What is the purpose of this function? Why are you expecting a number between 80 and 100?

And please format your code, i.e. indent properly and remove the many empty lines.
treuss at 2007-11-9 1:25:22 >
# 2 Re: please I need help with calculation (logic problem)
The function calculates the qsnr of 16 bit signal on a 1000 interval...I formatted the code thanks
w1985 at 2007-11-9 1:26:22 >
# 3 Re: please I need help with calculation (logic problem)
You could also give more information about the calculation's logic and try to inform the meaning of the variables. For example, the "e" means "error", the "i" means "iteration" and so on. :) It helps to turn you code more clear and find the problem faster.

By the way, the line

yi= xi * pow(2,n-1);

really means

yi = xi * 32768; // that is, xi * pow(2, 15.00)

isn't it ? Is this supposed to be a const value ? (n never changes)
thiagodp at 2007-11-9 1:27:26 >