# 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. ## 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. 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.