Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sine wave generation in c++

I am trying to generate a set of points, which when plotted as a graph represent a sine wave of 1 cycle. The requirements are :

  • a sine wave of 1 cycle
  • lower limit = 29491
  • upper limit = 36043
  • no of points = 100
  • Amplitude = 3276
  • zero offset = 32767

Code :

int main()
{
    ofstream outfile;
    outfile.open("data.dat",ios::trunc | ios::out);
    for(int i=0;i<100;i++)
    {
        outfile << int(3276*sin(i)+32767) << "\n";
    }
    outfile.close();
    return 0;
}

I am generating and storing the points in a file. When these points are plotted I get the following graph.

enter image description here

But I only need one cycle. How can I do this?

like image 520
Pradeep Avatar asked May 16 '18 08:05

Pradeep


1 Answers

taking into the formula of sine wave:

y(t) = A * sin(2 * PI * f * t + shift)

where:

A = the amplitude, the peak deviation of the function from zero.
f = the ordinary frequency, the number of oscillations (cycles)
t = time
shift = phase shift

would be:

y[t] = AMPLITUDE * sin (2 * M_PI * 0.15 * t + 0) + ZERO_OFFSET;
                                   ^^^ f = 15 cycles / NUM_POINTS = 0.15 Hz

To have one full-cycle, loop from y[0:t) where t is the time or number of points it takes to have a full cycle (i.e. wavelength)

like image 65
Joseph D. Avatar answered Oct 03 '22 04:10

Joseph D.