gpt4 book ai didi

创建具有特定顺序的旋转表?

转载 作者:行者123 更新时间:2023-11-30 14:56:09 27 4
gpt4 key购买 nike

我希望为特定目的创建一个具有特定顺序的旋转表,这是初始代码:

#define TWIDDLE_LIMIT 64
#define PI 3.1415927

float *twiddle_real;
float *twiddle_imag;

void main()
{
int N = 256;
int TW_size = TWIDDLE_LIMIT + (TWIDDLE_LIMIT>>2);

twiddle_real = malloc(TW_size * sizeof(float));
twiddle_imag = malloc(TW_size * sizeof(float));

int i;
for(i=0; i<TWIDDLE_LIMIT; i++)
{
twiddle_real[i] = (float) cos((float)i * 2.0 * PI / (float)N);
twiddle_imag[i] = (float) - sin((float)i * 2.0 * PI / (float)N);
}
for(int a=0; a<TWIDDLE_LIMIT; a++)
printf("RE = %f \t IM = %f \n",twiddle_real[a],twiddle_imag[a]);
}

我得到了这样的结果:

RE = 1.000000    IM = -0.000000 //64 lines
RE = 0.999699 IM = -0.024541
RE = 0.998795 IM = -0.049068
RE = 0.997290 IM = -0.073565
RE = 0.995185 IM = -0.098017
RE = 0.992480 IM = -0.122411
RE = 0.989177 IM = -0.146730
RE = 0.985278 IM = -0.170962
RE = 0.980785 IM = -0.195090
RE = 0.975702 IM = -0.219101
RE = 0.970031 IM = -0.242980
RE = 0.963776 IM = -0.266713
RE = 0.956940 IM = -0.290285
RE = 0.949528 IM = -0.313682
RE = 0.941544 IM = -0.336890
RE = 0.932993 IM = -0.359895
RE = 0.923880 IM = -0.382683
RE = 0.914210 IM = -0.405241
RE = 0.903989 IM = -0.427555
RE = 0.893224 IM = -0.449611
RE = 0.881921 IM = -0.471397
RE = 0.870087 IM = -0.492898
RE = 0.857729 IM = -0.514103
RE = 0.844854 IM = -0.534998
RE = 0.831470 IM = -0.555570
RE = 0.817585 IM = -0.575808
RE = 0.803208 IM = -0.595699
RE = 0.788346 IM = -0.615232
RE = 0.773010 IM = -0.634393
RE = 0.757209 IM = -0.653173
RE = 0.740951 IM = -0.671559
RE = 0.724247 IM = -0.689541
RE = 0.707107 IM = -0.707107
RE = 0.689541 IM = -0.724247
RE = 0.671559 IM = -0.740951
RE = 0.653173 IM = -0.757209
RE = 0.634393 IM = -0.773010
RE = 0.615232 IM = -0.788346
RE = 0.595699 IM = -0.803208
RE = 0.575808 IM = -0.817585
RE = 0.555570 IM = -0.831470
RE = 0.534998 IM = -0.844854
RE = 0.514103 IM = -0.857729
RE = 0.492898 IM = -0.870087
RE = 0.471397 IM = -0.881921
RE = 0.449611 IM = -0.893224
RE = 0.427555 IM = -0.903989
RE = 0.405241 IM = -0.914210
RE = 0.382683 IM = -0.923880
RE = 0.359895 IM = -0.932993
RE = 0.336890 IM = -0.941544
RE = 0.313682 IM = -0.949528
RE = 0.290285 IM = -0.956940
RE = 0.266713 IM = -0.963776
RE = 0.242980 IM = -0.970031
RE = 0.219101 IM = -0.975702
RE = 0.195090 IM = -0.980785
RE = 0.170962 IM = -0.985278
RE = 0.146730 IM = -0.989177
RE = 0.122411 IM = -0.992480
RE = 0.098017 IM = -0.995185
RE = 0.073565 IM = -0.997290
RE = 0.049068 IM = -0.998795
RE = 0.024541 IM = -0.999699

这只是我可以尽可能向您解释的一个最小示例。

我想要并尝试创建的是一个以较早的行集开头的表格,resdt 如下:

(之前的行集)表示为以下(idx:0 ---> 64)

    re[idx] = (float) cos((float)i * (2*pi)/(float)N);
im[idx] = (float)-sin((float)i * (2*pi)/(float)N);

(第二组线)表示为以下重复 4 次

    re[idx] = (float) cos(4 * (float)i * (2*pi)/(float)N);
im[idx] = (float)-sin(4 * (float)i * (2*pi)/(float)N);

(第三组线)表示为以下重复16次

    re[idx] = (float) cos(16 * (float)i * (2*pi)/(float)N);
im[idx] = (float)-sin(16 * (float)i * (2*pi)/(float)N);

预计结果如下:

//set 1 as above  repeated just 1 time
// ....
//set 2 repeated 4 times
RE = 1.000000 IM = -0.000000 //1st ligne of set 1
RE = 0.995185 IM = -0.098017 //4th ligne of set 1
RE = 0.980785 IM = -0.195090 //8th ligne of set 1
RE = 0.956940 IM = -0.290285 //12th ligne of set 1 ...
RE = 0.923880 IM = -0.382683
RE = 0.881921 IM = -0.471397
RE = 0.831470 IM = -0.555570
RE = 0.773010 IM = -0.634393
RE = 0.707107 IM = -0.707107
RE = 0.634393 IM = -0.773010
RE = 0.555570 IM = -0.831470
RE = 0.471397 IM = -0.881921
RE = 0.382683 IM = -0.923880
RE = 0.290285 IM = -0.956940
RE = 0.195090 IM = -0.980785
RE = 0.098017 IM = -0.995185
// set 3 repeated 16 times
RE = 1.000000 IM = -0.000000 //1st ligne of set 1
RE = 0.923880 IM = -0.382683 //16th ligne of set 1
RE = 0.707107 IM = -0.707107 //38th ligne of set 1
RE = 0.382683 IM = -0.923880 //64th ligne of set 1

我已经尝试了几次,但总是得到错误的结果,我不知道这是精度问题还是其他原因。

最佳答案

您可以在附加(外部)循环中维护因子并设置大小:

// you will need more than you calculated previously!
float twiddle_real[TWIDDLE_LIMIT * 3];
float twiddle_imag[TWIDDLE_LIMIT * 3];

// pointer arithmetic...
float* real = twiddle_real;
float* imag = twiddle_imag;

double factor = 1.0;
for(int size = TWIDDLE_LIMIT; size > 1; size /= 4)
{
for(int j = 0; j < 64; ++j)
{
*real++ = (float) cos((j % size) * factor * 2.0 * PI / N);
*imag++ = (float) -sin((j % size) * factor * 2.0 * PI / N);
}
factor *= 4.0;
}

顺便说一句,您不需要所有这些强制转换 - 因为因子是 double 型,所以 (j % size) 会隐式转换为 N ,之后也会转换为 N

建议:由于 re 和 img 属于同一类,我会这样表示它们:

struct Complex
{
double re;
double im;
};

然后你可以得到一个这样的数组:

struct Complex twiddle[TW_size]; // no need for malloc, by the way...

你可能已经注意到了:我也变成了双倍。没有理由使用浮点(精度更有限),除非您的可用内存有限(微 Controller )...

替代方案(为什么要重新发明轮子?):使用complex.h .

关于创建具有特定顺序的旋转表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44910411/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com