Originally Posted by
chunlee
hi there:
i have been experimenting with simple saw-tooth oscillator emulation before i actually do the DSP code. its essentially working out the phase increment value and just accumulate that into a float. repeat the process once the accumulator (phase) is larger than 1.
my question is that, in theory, once i know how much phase should increase by each time, i should be able to know in advance the last value of phase before it goes over 1, right?
but, as it turns out in my code, the perdicted last phase value is different from the actual value of the accumulator.
is this the precision problem with floats or am i doing something wrong in my code?
cheers
CHUN
PHP Code:
main()
{
float freq = 200;
float phaseinc = freq * (1./44100);
float phase;
float last_value;
printf("freq ->%f\n", freq);
printf("phase increased by -> %f\n",phaseinc);
printf("perdict last phase value -> %f\n", 1-phaseinc);
for(phase = 0; phase < 1.; phase += phaseinc)
{
//printf("%f\n", phase);
last_value = phase;
}
printf("actual last phase value -> %f\n", last_value);
}
Your problem is not roundoff error (If you change floats to doubles you will see that the error is about the same.)
You have defined phaseinc so that it takes 220.5 steps to get from 0 to 1.
Try this with different frequencies and phaseinc definitions:
Code:
#include <stdio.h>
int main()
{
double freq = 200;
double phaseinc = freq * (1./44100);
double phase;
double last_value;
int i;
printf("freq -> %f\n", freq);
printf("phase increased by -> %.8e\n",phaseinc);
printf("predict last phase value -> %.8e\n\n", 1-phaseinc);
printf("Number of steps to get to 1 = %f\n\n", 1.0/phaseinc);
i = 0;
for(phase = 0; phase < 1.; phase += phaseinc)
{
//printf("%f\n", phase);
last_value = phase;
i++;
}
printf("Number of steps = %d\n", i);
printf("actual last phase value -> %15.8e\n", last_value);
printf("actual - predicted -> %15.8e\n", last_value - (1.0 - phaseinc));
return 0;
}
Regards,
Dave