首先,我们可以尝试在 C 上编写 pyton 代码
#include <stdio.h>
#include <math.h>
void main() {
double table[1000];
double feedback = 0.25;
double accumulator = -1;
int i;
for (i=0;i<1000;i++) {
double output = sin(M_PI*(accumulator + feedback*output));
table[i]=output;
accumulator += 0.005;
if (accumulator > 1)
accumulator = -1;
printf("%f\n",output);
}
}
下一步 - 使用 sin 的计算值
#include <stdio.h>
#include <math.h>
void main() {
double table[1000];
double feedback = 0.25;
double accumulator = 1;
int i;
double sinvalue[1024];
for (i=0;i<1024;i++) {
sinvalue[i]=sin(M_PI*i/512);
}
for (i=0;i<1000;i++) {
double output = sinvalue[(int)(512*(accumulator + feedback*output))%1024];
printf("%0.6f %0.6f %0.6f\t",accumulator,feedback,output);
table[i]=output;
accumulator += 0.005;
if (accumulator > 2)
accumulator = 0;
printf("%f\n",output);
}
}
下一步 - 使用 sin 和 output 的 16 位值。在这个版本中,“输出”中的值就像 XXXXXXQQ.IIIIIIII.XXXXXXXX.XXXXXXXX 一样,我们失去了一些准确性。
#include <stdio.h>
#include <math.h>
#define ONE ((int)(2*256*256*256/M_PI))
void main() {
double table[1000];
double feedback = 0.25;
double accumulator = 1;
double accumulatorDelta = 0.005;
unsigned int feedback_i = ONE*feedback/32768;
unsigned int accumulator_i = ONE*accumulator;
unsigned int accumulatorDelta_i = ONE*accumulatorDelta;
int i;
double sinvalue[1025];
short int sinvalue_i[1025];
for (i=0;i<1025;i++) {
sinvalue[i]=sin(M_PI*i/512);
sinvalue_i[i]=32786*sinvalue[i];
if (sinvalue[i]*32768>32768) sinvalue_i[i]=32768;
if (sinvalue[i]*32768<-32767) sinvalue_i[i]=-32767;
}
for (i=0;i<1000;i++) {
double output = sin(M_PI*(accumulator + feedback*output));
short int output_i = sinvalue_i[ ((unsigned int) ((accumulator_i + feedback_i*output_i)*M_PI)>>16)%1024 ];
table[i]=output;
accumulator += 0.005;
if (accumulator > 2)
accumulator = 0;
accumulator_i += accumulatorDelta_i;
if (accumulator_i > 2*ONE)
accumulator_i = 0;
printf("%f %f %04X\n",output,(float)output_i/32768,(unsigned short int)output_i);
}
}
但是我们在转换 int->double->int 上浪费了一些时间
#include <stdio.h>
#include <math.h>
#define ONE ((int)(2*256*256*256))
void main() {
short int table[1000];
unsigned int feedback_i = ONE*0.25/32768;
unsigned int accumulator_i = ONE*1;
unsigned int accumulatorDelta_i = ONE*0.005;
int i;
short int sinvalue_i[1025];
for (i=0;i<1025;i++) {
double sinvalue=sin(M_PI*i/512);
sinvalue_i[i]=32786*sinvalue;
if (sinvalue*32768>32768) sinvalue_i[i]=32768;
if (sinvalue*32768<-32767) sinvalue_i[i]=-32767;
}
for (i=0;i<1000;i++) {
short int output_i = sinvalue_i[ ( (accumulator_i + feedback_i*output_i)>>16)%1024 ];
table[i]=output_i;
accumulator_i += accumulatorDelta_i;
if (accumulator_i > 2*ONE)
accumulator_i = 0;
printf("%f %04X\n",(float)output_i/32768,(unsigned short int)output_i);
}
}