用c ++绘制频谱

时间:2015-08-28 17:34:48

标签: c++ frequency-distribution

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

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

  1. 应用汉宁窗口
  2. 使用fftw3库
  3. 应用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]应该给我频谱。

    以下是图表:enter image description here频谱和正弦时间窗口: enter image description here

3 个答案:

答案 0 :(得分:1)

enter image description here我的频率间隔完全错误。根据{{​​3}}; x 轴上的频率范围和分辨率取决于采样率和 N 。频率轴上的最后一点应该是 Fs / 2-Fs / N ,分辨率 dF = FS / N 。所以我将脚本改为:(因为频率分辨率为 Fs / N ,因为增加击打次数 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;
}

答案 1 :(得分:0)

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

您正在为50个样本进行采样,因此需要25个FFT分档。您的采样频率为1000 Hz,因此每个FFT频段需要1000/2/25 == 250 Hz。您的bin分辨率太低。

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

答案 2 :(得分:0)

由于您在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;
    }

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