通过傅里叶变换得到的振荡和频谱

4

我正在尝试找到用数据向量表示图像中一个像素运动生成的波形的振荡和频谱频率。

该数据被存储在一个 .txt 文件中,格式如下:

75.000000 
60.000000 
52.000000 
61.000000 
66.000000 
78.000000 
86.000000 
74.000000 
59.000000 
47.000000 
58.000000 
60.000000 
81.000000 
85.000000 
81.000000 
70.000000 
58.000000 
59.000000 
56.000000 
61.000000 
77.000000 
88.000000 
82.000000 
79.000000 
75.000000 
75.000000 
75.000000 
75.000000 
76.000000 
82.000000 
82.000000 

找到从数据中得到的图形的振荡频率(Hz)和频谱(幅度),下面是一个图形示例。
我已经阅读并讨论了使用fftw3库进行傅里叶分析的很多内容,我是使用C++的新手,对这个库更加陌生。
希望您能帮助我解决问题,提供代码或思路。
非常感谢您的帮助。
我使用Microsoft Visual C++ 2010(win32)。
代码:
#include "StdAfx.h"
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <fftw3.h>
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

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

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

std::ifstream myfile ("Vetor_Oscilacao2.txt");
if (myfile.is_open())
{
    std::vector<double> in;
std::string line;
    while (std::getline(myfile, line))
    {
        double value = std::stod(line);
        std::cout << value << '\n';
        in.push_back(value);
    }

    myfile.close();

    }
else
    std::cout << "Unable to open file";
std::cin.get();

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]=(10*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 fichero;
fichero.open("example2.txt",fstream::out);
fichero << "plot '-' using 1:2" << std::endl;

for(i = 0;i< ((N/2)-1); i++)
{ 
fichero << ff[i]<< " " << v[i]<< std::endl;
}
 fichero.close();
 fftw_destroy_plan (plan_forward);
 fftw_free (in);
 fftw_free (out);
 return 0;
}

欢迎来到stackoverflow。这里的理念是在提问之前先自己努力尝试解决问题。那么,你已经尝试了什么,遇到了哪些困难? - Walter
尝试利用以下提供的信息进行初步近似: https://dev59.com/cY7ea4cB1Zd3GeqPC5Jj 我已经编辑了问题,附加了到目前为止生成的代码。我认为输出v [i]的表示形式是对数,有什么方法可以线性表示它吗?我不知道自己是否走在正确的道路上,如果我的问题很愚蠢,请见谅,因为我是C++的新手。非常感谢。 - Jorge Cruz
1个回答

2
你的代码主要问题在于变量std::vector<double> in将在离开条件范围时被销毁:if (myfile.is_open())
此外,代码没有以任何方式使用表示输入文件曲线的值,因此当前布局下代码无法找到振荡频率

我是c++和fft的新手,如果我做了一些无意义的事情,请原谅,但我需要对这些数据进行转换...您能否用更简单的解释或提供实现所需的想法来帮助我?再次感谢。 - Jorge Cruz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接