6

请参阅此问题下方答案中的编辑。

我编写了一个脚本来用 c++ 绘制正弦信号的频谱。以下是步骤

  1. 应用汉宁窗
  2. 使用 fftw3 库应用 FFT

我有三个图:信号,信号何时乘以汉宁函数,以及频谱。频谱看起来不对。它应该在 50 Hz 处有一个峰值。任何建议将不胜感激。这是代码:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <fftw3.h>
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;

int main()
{
int i;
double y;
int N=50;
double Fs=1000;//sampling frequency
double  T=1/Fs;//sample time 
double f=50;//frequency
double *in;
fftw_complex *out;
double t[N];//time vector 
double ff[N];
fftw_plan plan_forward;

in = (double*) fftw_malloc(sizeof(double) * N);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);

 for (int i=0; i< N;i++)
 {
    t[i]=i*T;
    ff[i]=1/t[i];
    in[i] =0.7 *sin(2*M_PI*f*t[i]);// generate sine waveform
    double multiplier = 0.5 * (1 - cos(2*M_PI*i/(N-1)));//Hanning Window
    in[i] = multiplier * in[i];
  }

  plan_forward = fftw_plan_dft_r2c_1d ( N, in, out, FFTW_ESTIMATE );

  fftw_execute ( plan_forward );

  double v[N];

  for (int i = 0; i < N; i++)
    {

    v[i]=20*log(sqrt(out[i][0]*out[i][0]+ out[i][1]*out[i][1])/N/2);//Here I have calculated the y axis of the spectrum in dB

    }

   fstream myfile;

   myfile.open("example2.txt",fstream::out);

   myfile << "plot '-' using 1:2" << std::endl;

   for(i = 0; i < N; ++i)

    { 

      myfile << ff[i]<< " " << v[i]<< std::endl;

    }

 myfile.close();

 fftw_destroy_plan ( plan_forward );
 fftw_free ( in );
 fftw_free ( out );
 return 0;
  }

我必须补充一点,在将结果插入到 example2.txt 之后,我已经使用 gnuplot 绘制了图表。所以 ff[i] vs v[i] 应该给我频谱。

以下是图表:在此处输入图像描述 频谱和正弦时间窗口分别为: 在此处输入图像描述

4

3 回答 3

1

在此处输入图像描述我的频率间隔完全错误。根据http://www.ni.com/white-paper/3995/en/#toc1x轴上的频率范围和分辨率取决于采样率和N。频率轴上的最后一点应该是Fs/2-Fs/N和分辨率dF=FS/N。所以我已将脚本更改为:(因为频率分辨率为Fs/N,因为您增加了 smaples N的数量(或降低采样频率Fs)你会得到更小的频率分辨率和更好的结果。)

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <fftw3.h>
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;

int main()
{
int i;
double y;
int N=550;//Number of points acquired inside the window
double Fs=200;//sampling frequency
double dF=Fs/N;
double  T=1/Fs;//sample time 
double f=50;//frequency
double *in;
fftw_complex *out;
double t[N];//time vector 
double ff[N];
fftw_plan plan_forward;

in = (double*) fftw_malloc(sizeof(double) * N);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);

 for (int i=0; i<= N;i++)
 {
 t[i]=i*T;

in[i] =0.7 *sin(2*M_PI*f*t[i]);// generate sine waveform
double multiplier = 0.5 * (1 - cos(2*M_PI*i/(N-1)));//Hanning Window
in[i] = multiplier * in[i];
 }

 for (int i=0; i<= ((N/2)-1);i++)
{ff[i]=Fs*i/N;
}
plan_forward = fftw_plan_dft_r2c_1d ( N, in, out, FFTW_ESTIMATE );

fftw_execute ( plan_forward );

double v[N];

for (int i = 0; i<= ((N/2)-1); i++)
{

v[i]=(20*log(sqrt(out[i][0]*out[i][0]+ out[i][1]*out[i][1])))/N;  //Here   I  have calculated the y axis of the spectrum in dB

   }

fstream myfile;

myfile.open("example2.txt",fstream::out);

myfile << "plot '-' using 1:2" << std::endl;

for(i = 0;i< ((N/2)-1); i++)

{ 

myfile << ff[i]<< " " << v[i]<< std::endl;

}

 myfile.close();

 fftw_destroy_plan ( plan_forward );
 fftw_free ( in );
 fftw_free ( out );
 return 0;
}
于 2015-09-01T14:06:56.470 回答
0

我认为您可能没有足够的样本,特别是请参考此 Electronics.StackExhcange 帖子:https ://electronics.stackexchange.com/q/12407/84272 。

您正在对 50 个样本进行采样,即 25 个 FFT 箱。您以 1000 Hz 的频率进行采样,因此每个 FFT 箱 1000 / 2 / 25 == 250 Hz。您的 bin 分辨率太低。

我认为您需要降低采样频率或增加采样数。

于 2015-08-28T20:17:41.233 回答
-1

由于您对 SO 提出问题,因此您的代码可以使用一些缩进和样式改进来使其更易于阅读。

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <fftw3.h>
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;

int main(){
    // use meaningful names for all the variables
    int i;  
    double y;
    int N = 550; // number of points acquired inside the window
    double Fs = 200; // sampling frequency
    double dF = Fs / N;
    double  T = 1 / Fs; // sample time 
    double f = 50; // frequency
    double *in;
    fftw_complex *out;
    double t[N]; // time vector 
    double ff[N];
    fftw_plan plan_forward;

    in = (double*) fftw_malloc(sizeof(double) * N);
    out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);

    for (int i = 0; i <= N; i++){
        t[i]=i*T;
        in[i] = 0.7 * sin(2 * M_PI * f * t[i]); // generate sine waveform
        double multiplier = 0.5 * (1 - cos(2 * M_PI * i / (N-1))); // Hanning Window
        in[i] = multiplier * in[i];
    }

    for(int i = 0; i <= ((N/2)-1); i++){
        ff[i] = (Fs * i) / N;
    }

    plan_forward = fftw_plan_dft_r2c_1d(N, in, out, FFTW_ESTIMATE);

    fftw_execute(plan_forward);

    double v[N];
    // Here I have calculated the y axis of the spectrum in dB
    for(int i = 0; i <= ((N/2)-1); i++){
        v[i] = (20 * log(sqrt(out[i][0] * out[i][0] + out[i][1] * out[i][1]))) / N;  
    }

    fstream myfile;
    myfile.open("example2.txt", fstream::out);
    myfile << "plot '-' using 1:2" << std::endl;

    for(i = 0; i < ((N/2)-1); i++){ 
        myfile << ff[i] << " " << v[i] << std::endl;
    }
    myfile.close();

    fftw_destroy_plan(plan_forward);
    fftw_free(in);
    fftw_free(out);

    return 0;
    }

您的代码可以使用更多注释,尤其是在循环或函数调用之前指定它们的输入值(目的)和/或返回值(结果)。

于 2015-09-01T18:36:04.113 回答