Oscillation and frequency spectrum through a Fourier transform
Asked Answered
S

1

7

I'm trying to find the oscillation and spectrum frequency of the waveform generated by a vector of data representing the motion of a pixel in an image.

The data is stored in a .txt file, as follows:

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 

The idea is to find the frequency of oscillation (Hz) and frequency spectrum (amplitude) of the graph obtained from the data, an example of the graph is presented below.

example of the oscillation graph

I have read and talked a lot about the use of the fftw3 library for the Fourier analysis, I am new to using C ++ and even more of this library.

I hope you can help me with code or ideas to solve my problem.

Thank you very much for your help.

I work with Microsoft Visual C ++ 2010(win32)

Code:

#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;
}
Succor answered 25/4, 2017 at 14:57 Comment(2)
Welcome to stackoverflow. The idea here is that you try hard before asking. So, what have you tried so far and where did you get stuck?Johannesburg
Try to make my first approximation with the information provided in: #32277228 I have edited the question to attach the code generated so far. I think that the representation of output v [i] is in logarithm, some idea so that it can be represented linearly? I do not know if I'm on the right track, I'm sorry if my question is very silly but I'm new to C ++. Thank you very much.Succor
R
3

The main problem with your code is that the variable std::vector<double> in will be destroyed when you leave the scope of the conditional: if (myfile.is_open()).

Also the code is not in any way using the values representing the curve from the input file, so there's really no way the code could find frequency of oscillation given how it's currently laid out.

Rebeca answered 26/4, 2017 at 11:21 Comment(1)
I am new in c ++ and fft, sorry if I am doing a nonsense but I need to make this transform for these data .... could help me with a simpler explanation or with ideas to accomplish what I need. Again, thank you very much.Succor

© 2022 - 2024 — McMap. All rights reserved.