Help Wanted: Computing the period of sine-wave input data

This is a discussion on Help Wanted: Computing the period of sine-wave input data within the C Programming forums, part of the General Programming Boards category; I'm in an intro C programming class, and I need to write a program that uses the output of a ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    2

    Help Wanted: Computing the period of sine-wave input data

    I'm in an intro C programming class, and I need to write a program that uses the output of a previous program I wrote as input to compute the period of a sine wave. We have an interface that reads the input for us, but here is the code I have written so far:

    Code:
    //estimates the period of given sine data
     
    #include <stdio.h>
    #include <math.h>
     
    int main(void)
    {
        
    //defining variables
    int s;
    double y1=0,y2=0,d,t1=0, t2=0;
           while(1) // until end-of-file or error
      {
        s = scanf("%lf",&d); // try to read one double
    t2=t1, t1++;
     if( s != 1) // could not read one double, must be end-of-file or error
          break;
     
    y1=y2,y2=d;
     
    if ((y1>0&&y2<=0)||(y1<=0&&y2>0)) //if the two data values have opp. signs
    printf("%5.2f\n",(t2+t1)/2);
      }
     
      return 0;
    }
    Now it's not like I expected this to print me the period, because I obviously haven't written any code to do anything of the sort. That being said, this prints me all of the values where zero-crossings occur. How can I change the program/write something that will take two of these consecutive values, and subtract one from the other?

    Right now my output is something like:

    9.50
    22.50
    36.50
    52.50
    etc.

    And I need to compute, for example, 2*(22.50-9.50)

  2. #2
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    If I understand correctly, the data are of the form y = k sin (x) + h , not y = sin x. This won't affect the period though. Also the data are not very granular. Here is one crude alg to figure out the period. Check for one y max , and store the x1 value. check for the next y max and store the x2 value. Your period will be x2 - x1. There is probably some other more elegant solution, which someone will post when daybreak comes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need to generate sine wave using C
    By saya407 in forum C Programming
    Replies: 18
    Last Post: 01-31-2012, 04:45 PM
  2. Sine Wave Program
    By Stiks in forum C Programming
    Replies: 10
    Last Post: 10-15-2005, 01:35 AM
  3. Help producing sine wave
    By ricky1981 in forum C++ Programming
    Replies: 9
    Last Post: 10-31-2003, 09:20 PM
  4. ...simple sine wave?
    By Sebastiani in forum C++ Programming
    Replies: 27
    Last Post: 12-15-2002, 02:12 PM
  5. Sine Wave Program Help
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-19-2001, 01:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21