C++ 通过傅里叶变换的振动和频谱

C++ 通过傅里叶变换的振动和频谱,c++,fftw,spectrum,frequency-analysis,spectrogram,C++,Fftw,Spectrum,Frequency Analysis,Spectrogram,我试图找到由代表图像中像素运动的数据向量生成的波形的振荡和频谱频率 数据存储在.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

我试图找到由代表图像中像素运动的数据向量生成的波形的振荡和频谱频率

数据存储在.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)

代码:

#包括“StdAfx.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
int i;
const int N=100;//在窗口内获取的点数
双Fs=200;//采样频率
双dF=Fs/N;
双T=1/Fs;//采样时间
双f=86;//频率
双*英寸;
fftw_复合体*out;
双ff[N];
fftw_计划向前;
in=(双*)fftw_malloc(sizeof(双)*N);
out=(fftw_复合体*)fftw_malloc(sizeof(fftw_复合体)*N);
std::ifstream myfile(“vetoroscilacao2.txt”);
如果(myfile.is_open())
{
std::中的向量;
std::字符串行;
while(std::getline(myfile,line))
{
双值=标准::stod(行);

std::cout代码的主要问题是,当您离开条件的作用域时,
中的变量
std::vector将被销毁:
if(myfile.is_open())


此外,代码没有以任何方式使用表示输入文件中曲线的值,因此根据当前的布局,代码确实无法找到振荡频率。

欢迎使用stackoverflow。这里的想法是,在询问之前,您会努力尝试。那么,到目前为止,您尝试了什么,在哪里遇到了问题?尝试e我对中提供的信息的第一个近似值:我已经编辑了问题,以附加到目前为止生成的代码。我认为输出v[I]的表示形式我不知道我是否在正确的轨道上,如果我的问题很愚蠢,我很抱歉,但我是C++的新手。非常感谢。我是C++和fft的新手,如果我做了一个无稽之谈,很抱歉,但我需要对这些数据进行此转换…可以帮我一个更简单的解释或我有很多想法来完成我需要的。再次,非常感谢。
#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;
}