c++ - 使用 C++ 的频谱图

标签 c++ gnuplot fft fftw spectrogram

我正在尝试使用 C++ 生成正弦信号的频谱图。生成频谱图:

  1. 我把真实的正弦信号分成了B block
  2. 对每个 block 应用汉宁窗(我假设没有重叠)。这应该给我 fft 的输入。
  3. 对每个 block 应用 fft 并根据频率系数 u[i][0]u[i][1] 计算幅度并将其放入 v[k][i] 其中k是 block 数,i是每个窗口的样本
  4. 绘制时间 tt[k]v[k][i]

这给了我以下情节:

enter image description here

但是,该图看起来并不正确。

有什么建议可以让它发挥作用吗?这是我的代码:

#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;
  int N=500;//Number of points acquired inside the window
  double Fs=200;//sampling frequency
  int windowsize=100;//Number of samples in each block
  double  T=1/Fs;//sample time 
  double f=50;//frequency(Hz)
  double *in;
  fftw_complex *out;
  double t[N];//time vector 
  double tt[5];
  double  v [5][249];
  fftw_plan plan_forward;
  in = (double*) fftw_malloc(sizeof(double) * N);
  out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * (N/2 + 1));

  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
  }

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

  for (int k=0; k< 5;k++){ 
    for (int i = 0; i<windowsize; i++){
      double multiplier = 0.5 * (1 - cos(2*M_PI*i/(windowsize-1)));//Hanning  Window
      in[i] = multiplier * in[i+k*windowsize];
      fftw_execute ( plan_forward );
      v[k][i]=(20*log10(sqrt(out[i][0]*out[i][0]+ out[i][1]*out[i][1])))/N;//The magnitude in dB
    }
  }

  for (int k=0; k< 5;k++){//Center time for each block

    tt[k]=(2*k+1)*T*(windowsize/2);

  }

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

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

  for (int k=0; k< 5;k++){ 
    myfile << v[k][i]<< " " << tt[k]<< std::endl;
  }
  myfile.close();
  fftw_destroy_plan ( plan_forward );
  fftw_free ( in );
  fftw_free ( out );
  return 0;
}

最佳答案

enter image description here

#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;
int N=500;
double Fs=200;//sampling frequency
int windowsize=100;//Number of points acquired inside the window
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];
double tt[5];
double  v [5][249];
fftw_plan plan_forward;
in = (double*) fftw_malloc(sizeof(double) * N);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * (N/2 + 1));

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
 }

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

for (int k=0; k< 5;k++){ 
  for (int i = 0; i<windowsize; i++){
  double multiplier = 0.5 * (1 - cos(2*M_PI*i/(windowsize-1)));//Hanning Window
 in[i] = multiplier * in[i+k*windowsize];
                                     }
fftw_execute(plan_forward);
for (int j = 0; j <= ((windowsize/2)-1); j++){
v[k][j] = 2*(out[j][0] * out[j][0] + out[j][1] * out[j][1])/(N*N);
v[k][0]   *= 0.5;
 v[k][N/2] *= 0.5;

for (int j = 0; j <= windowsize/2; j++){
   v[k][j] = 10 * log10(v[k][j] + 1e-5);
                                        }
                  }

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

//Center time for each block

for (int k=0; k< 5;k++){

tt[k]=(2*k+1)*T*(windowsize/2);
                        }

double b[6];
fstream myfile;

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

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

for (int k=0; k< 6;k++) {
                    b[0]=5;
                    b[k+1]=tt[k];
                     myfile <<b[k] << "\t";
                    }
 myfile<< std::endl;

 for (int j = 0; j <= windowsize/2; j++){  myfile << ff[j]<< "\t";

   for (int k=0; k< 5;k++){ myfile << v[k][j]<< "\t";

                          }
 myfile<< std::endl;
                                    }
 myfile.close();

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

关于c++ - 使用 C++ 的频谱图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32376149/

相关文章:

c++ - 同步工作线程

c++ - Mingw libgcc_s_sjlj-1.dll 丢失

C++ USB 编程

python - 使用 Python 2.6 对音频文件进行快速频谱分析?

fft - STFT 澄清(实时输入的 FFT)

c++ - 遍历 equal_range 结果集

Gnuplot 多图 : Convenient method for creating more complex layouts

plot - 将一个图的 xrange 固定到另一个图

plot - GNUPLot:将数据缩放并绘制到特定范围

python - numpy RFFT/IRFFT音量