2017-04-25 54 views
4

我試圖找到表示像素的圖像中的運動數據的矢量產生的波形的振盪和頻譜頻率。振盪和頻譜通過傅立葉變換

的數據被存儲在文本文件,如下所示:

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 

的想法是找到振盪(赫茲)和從數據中獲得的曲線圖的頻譜(振幅)的頻率,圖表的一個例子如下所示。

example of the oscillation graph

我已閱讀並談了很多關於使用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; 
} 
+0

歡迎使用stackoverflow。這裏的想法是,你在問之前努力嘗試。那麼,到目前爲止您嘗試過哪些方法?卡住了哪些方面? – Walter

+0

儘量讓我的第一次逼近中提供的信息: https://stackoverflow.com/questions/32276728/plotting-frequency-spectrum-with-c 我已經編輯附上到目前爲止生成的代碼的問題。 我認爲輸出V [I]的表示是在對數,一些想法,以便它可以被線性地表示? 我不知道如果我在正確的軌道上,我很抱歉,如果我的問題很愚蠢,但我是新的C++。 非常感謝。 –

回答

2

與您的代碼的主要問題是,當你離開的範圍變量std::vector<double> in將被銷燬有條件的:if (myfile.is_open())

此外,代碼沒有以任何方式使用代表輸入文件中曲線的值,因此如果給出當前佈局的代碼,代碼無法找到振盪頻率

+0

我是新的c + +和fft,抱歉,如果我做一個廢話,但我需要做這些數據的變換....可以幫助我一個更簡單的解釋或想法來完成我所需要的。再次,非常感謝。 –