Plot sine wave using files

C program to plot the sine wave using files.

Introduction: If its needed to plot the sine wave, you should take values of sine function at different time instants and store them using file, and then using that file, you can plot the wave in "gnu plot" like tools.
Program:

/* c program to plot the sine wave using files */
#include<stdio.h>
#include<stdlib.h>
#include<math.h>                                                  /* for sin() function */
#define PI 3.141414                                               /* assigning PI to its value */
int main()
{
              int am,fm;                                                 /* amplitude and frequency */
              float t,temp;                                              /* t for time scale */
              float mt;
              FILE *pf;     /* file declaration */
              printf("enter am,fm: ");
              scanf("%d%d",&am,&fm);                      /* read am, fm from user */
              pf=fopen("lmn.txt","w");                         /* opening file on your disk to write the data */
              temp=2/(float)fm;
              for(t=0;t<temp;t=t+(temp/200))               /* loop for taking different values of sine function*/
             {
                     mt=am*sin(2*PI*fm*t);
                     fprintf(pf,"%f\t%f\n",t,mt);               /*  print to file */
             }
             fclose(pf);                                                  /* close the file*/
             return 0;
}

Sample input:
enter am,fm: 5 1000

Output:
To see the output open the folder that contains your program, there you can see the file named "lmn.txt" and open it. The file contains different values of sine function for different t(time).
 

No comments: