如何了解DFT结果

长史密斯

我使用DFT的此实现:

/*
Direct fourier transform
*/
int DFT(int dir,int m,double *x1,double *y1)
{
    long i,k;
    double arg;
    double cosarg,sinarg;
    double *x2=NULL,*y2=NULL;

    x2 = malloc(m*sizeof(double));
    y2 = malloc(m*sizeof(double));
    if (x2 == NULL || y2 == NULL)
       return(FALSE);

    for (i=0;i<m;i++) {
       x2[i] = 0;
       y2[i] = 0;
       arg = - dir * 2.0 * 3.141592654 * (double)i / (double)m;
       for (k=0;k<m;k++) {
          cosarg = cos(k * arg);
          sinarg = sin(k * arg);
          x2[i] += (x1[k] * cosarg - y1[k] * sinarg);
          y2[i] += (x1[k] * sinarg + y1[k] * cosarg);
       }
    }

    /* Copy the data back */
    if (dir == 1) {
      for (i=0;i<m;i++) {
         x1[i] = x2[i] / (double)m;
         y1[i] = y2[i] / (double)m;
      }
   } else {
      for (i=0;i<m;i++) {
         x1[i] = x2[i];
         y1[i] = y2[i];
      }
   }

   free(x2);
   free(y2);
   return(TRUE);
}

放置在这里http://paulbourke.net/miscellaneous/dft/

第一个问题是为什么在应用直接transform(dir=1)之后我们应该缩放值?我阅读了有关DFT实施的一些想法,但没有发现任何相关信息。

作为输入,我使用具有1024采样频率的cos

#define SAMPLES 2048
#define ZEROES_NUMBER 512

double step = PI_2/(SAMPLES-2*ZEROES_NUMBER);
for(int i=0; i<SAMPLES; i++)
{
    /*
     * Fill in the beginning and end with zeroes 
     */
    if(i<ZEROES_NUMBER || i > SAMPLES-ZEROES_NUMBER)
    {
        samplesReal[i] = 0;
        samplesImag[i] = 0;
    }
    /*
     *  Generate one period cos with 1024 samples
     */
    else
    {
        samplesReal[i] = cos(step*(double)(i-ZEROES_NUMBER));
        samplesImag[i] = 0;
    }
}

对于绘图,我删除了上面我询问的缩放比例,因为输出值变得非常小,无法绘制图形。

我得到了这样的幅度和相位图: 在此处输入图片说明 在此处输入图片说明

如您所见,相位始终为0,幅度频谱相反。为什么?

以下是我更易读的版本,没有进行缩放,但产生的结果相同:

void DFT_transform(double complex* samples, int num, double complex*   res)
{
    for(int k=0; k<num; k++)
    {
        res[k] = 0;
        for(int n=0; n<num; n++)
        {
            double complex Wkn = cos(PI_2*(double)k*(double)n/(double)num) -
            I*sin(PI_2*(double)k*(double)n/(double)num);

            res[k] += samples[n]*Wkn;
        }
    }
}
长史密斯

Ok, guys. I'm glad to say that this implementation works. Problem was the wrong way of plotting graphs and a lack of understanding formulas.

How it works

DFT公式

As you can see there is k variable which is used to vary the frequency. So frequency is ν = k / T where T is a period of time taken to get samples. T = N/S where S is your sampling frequency. Then you can find your frequency as v = S*k/N

So when you get your result you should calculate frequencies for each point and remove everything that above S/2 and only then plot graph Magnitude = Magnitude(Frequency). This is what I didn't understand before. Hope it will be helpful to someone.

Some graphs I got.

Sin 100HZ.

罪过100Hz

Sin 100HZ + Cos 200HZ. sin(100x)+ cos(200x)

Sin 100HZ + (Cos 200HZ)/2 sin(100x)+ cos(200x)/ 2

如您所见,显示了频率和相关幅度。缩放存在问题,但是我们是否要确定信号中显示的频率并不重要。

感谢@PaulR

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章