使用OpenCV dft的频域信号

时间:2018-10-13 12:55:02

标签: c++ opencv dft frequency-analysis

我正在试验cv::dft:会生成1HZ窦信号,并在频域中显示。但是由于某种原因,它没有在1Hz时获得最大分量。我的代码如下:

       const int FRAME_RATE = 20;             //!< sampling rate in [Hz]
       const int WINDOW_SIZE = 256;         

       double len = double(WINDOW_SIZE)/double(FRAME_RATE);   // signal length in seconds
       double Fb = 1./len;                                     // frequency bin in Hz

       // Constructing frequency vector
       std::vector<double> f;
       double freq_step = 0;
       for (int i = 0; i < WINDOW_SIZE; ++i)
       {
          f.push_back(freq_step);
          freq_step += Fb;
       }


   // Create time vector
   std::vector<double> t;
   double time_step = 0;
   for(int i = 0; i<WINDOW_SIZE; ++i)
   {
       t.push_back(time_step);
       time_step += 1./double(FRAME_RATE);
   }

   // Creating sin signal with 1Hz period
   std::vector<double> y;
   for(auto val : t)
   {
       y.push_back(sin(1*FRAME_RATE*val));
   }

   // Compute DFT
   cv::Mat fd;
   cv::dft(y, fd, cv::DFT_REAL_OUTPUT);
   fd = cv::abs(fd);

如果我在时域和频域中绘制信号:plot(t, y); plot(f, fd),结果如下:

enter image description here

时间信号很好,但频率信号的最大值约为6HZ,而不是1HZ。

我在哪里弄错了?

0 个答案:

没有答案