C 创建具有特定顺序的旋转表?
我希望为特定目的创建一个具有特定顺序的twiddle表,以下是初始代码:C 创建具有特定顺序的旋转表?,c,arrays,C,Arrays,我希望为特定目的创建一个具有特定顺序的twiddle表,以下是初始代码: #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 *
#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]);
}
这只是一个很小的例子,我可以尽可能多地向你们解释
我想要并尝试创建的是一个以前面的一组行开始的表,resdt如下所示:
//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
以前的一组行表示为以下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);
re[idx] = (float) cos(16 * (float)i * (2*pi)/(float)N);
im[idx] = (float)-sin(16 * (float)i * (2*pi)/(float)N);
第3组线,如下所示,重复16次
re[idx] = (float) cos(4 * (float)i * (2*pi)/(float)N);
im[idx] = (float)-sin(4 * (float)i * (2*pi)/(float)N);
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;
}
然后,您可以拥有以下内容的数组:
struct Complex twiddle[TW_size]; // no need for malloc, by the way...
你可能已经注意到了:我也变成了替身。没有理由使用精度更有限的浮点运算,除非您的可用内存有限微控制器
为什么要重新发明轮子?使用。因为你的数学错了,所以它不起作用。第一个表仅涵盖复杂平面的第一个象限。当你为你想要的输出的第二段乘以4时,你不会得到第一个象限的4次重复,你会得到所有四个象限 这里有一些更接近您指定的内容。它解决了使用C语言时的一些问题:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N_ROOTS_OF_UNITY 64
#define PI 3.14159265358979323846264338327950
float *twiddle_real;
float *twiddle_imag;
int main()
{
size_t table_size = N_ROOTS_OF_UNITY * 3;
twiddle_real = malloc(table_size * sizeof(float));
twiddle_imag = malloc(table_size * sizeof(float));
size_t i, incr, repeat, k = 0;
for (incr = 1; incr <= 16; incr *= 4) {
for (repeat = 0; repeat < incr; ++repeat) {
for(i = 0; i < N_ROOTS_OF_UNITY; i += incr) {
twiddle_real[k] = (float) cos(i * (PI / 2) / N_ROOTS_OF_UNITY);
twiddle_imag[k] = (float) -sin(i * (PI / 2) / N_ROOTS_OF_UNITY);
++k;
}
}
}
for (int a = 0; a < table_size; a++)
printf("%d: RE = %f\tIM = %f\n", a, twiddle_real[a], twiddle_imag[a]);
return 0;
}
预期的结果是什么?哪一个是错误的?好的,请稍等,我将进行编辑,向您显示与您的问题相关的预期结果,但是如果您想使用float,为什么不使用呢?您的问题可能是FFT交错非常复杂。我曾经写过一些代码来处理任意大小的窗口,但我现在无法不费吹灰之力地复制它。至于您的问题,您希望前64个条目与现在相同吗?那么应该是四行还是256行加上第二组行?最后一盘是什么?您能否为我们展示一个较小数据集所需输出的示例?