Thread: need to generate sine wave using C

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    11

    Question need to generate sine wave using C

    I am working on a project to code a program that generates sum of two sine waves of different freq,and has a sampling freq of 100khz(1 lac samples).
    The following is what i could find out about generating sine wave,but don't know what values the variables should hold.

    C is very new for me,please help me.

    Code:
    Enter the initial step-size in degrees
     Enter the number of lines to be displayed in the graph
        
       current_step_size = initial_step_size
          
         for (line_count = 0; line_count < no_of_lines; line_count++)
         {
          result = sin(current_step_size);
          for (sine_count = -1; sine_count <= 1; sine_count += 0.01)
          {
           if (result == sine_count)
                }
       Increment step size 
          current_step_size += initial_step_size;         
         }
         
         return 0;

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well its more than that code you need to draw sin wave. If you want to display the numerical data in the form on graph you will have to start looking into graphical programming library such as the openGL.

    But from the code there are two variables which needs to be inputted, one of the step size. This depends on the application your program is designed. For example: assuming your drawing a sine wave of all even angle between 0 - 90. Your step size would be 2 starting from 0. And the other variable which you would need to initialise would be number of lines, which is in a complete contrast to the how big the graph you will like it to be.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How do you plot a sign wave on a piece of graph paper? Write out the steps to do that, and you'll understand how to do it here.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    11
    displaying the graph isn't important,mere generation will do. Since i am from electronics field, does the no. of lines means amplitude and step size means time period of the signal? and where shall i put the sampling frequency? I am struggling with coding, please help me. I ll be gratefull. thank you.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Why don't you rename the variables to be say "deltaTime" and "amplitude".

    As in say
    amplitude = sin(deltaTime)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    11
    Please can i get at least a rough idea of a code which would help me understand what i am supposed to do.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'd suggest some reading: Sine wave - Wikipedia, the free encyclopedia

    Like I said earlier, can you work out how to do one on paper? If not, you can't do this.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    11
    ok thank you. the wikipedia and the following page : Creating a Sine Wave gave me some idea to workout. will post my progress soon. thanks every1 for your valuable guidance

  9. #9
    Registered User
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    11
    i got up with this following code now. Could someone help me with displaying the output of this program because the last printf statement isnt getting displayed in the output screen.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    #define pi 3.414
    void main()
    {
     int freq,fs,amplitude,cycle;
     double angle,increment,sample;
     printf("enter the desired frequency of the signal:\n");
     scanf("%d",&freq);
     printf("enter the desired sampling frequency:\n");
     scanf("%d",&fs);
     printf("enter the amplitude of the signal:\n");
     scanf("%d",&amplitude);
     angle=0;
     increment=(2*pi)/(fs/freq);
        for (cycle=1;cycle<=freq;cycle++)
        {
            while (angle<=(2*pi))
            {
                sample=amplitude * sin(angle);
                angle=angle+increment;
            }
            printf("%lf/t",&sample);/*here the output is not getting displayed*/
        }
     }
    Last edited by saya407; 01-30-2012 at 10:10 AM.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Get a better compiler, one which can tell you when you mess up printf/scanf calls (by spraying random & all over the place for example)

    Eg.
    $ gcc -Wall -Wextra foo.c -lm
    foo.c:4: warning: return type of ‘main’ is not ‘int’
    foo.c: In function ‘main’:
    foo.c:20: warning: implicit declaration of function ‘sin’
    foo.c:20: warning: incompatible implicit declaration of built-in function ‘sin’
    foo.c:23: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘double *’
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Also, PI truncated to three decimal digits is "3.141", rounded to nearest is "3.142"

    Here it is
    .
    Devoted my life to programming...

  12. #12
    Registered User
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    11
    what am i suppposed to do to rectify these

    foo.c:20: warning: implicit declaration of function ‘sin’
    foo.c:20: warning: incompatible implicit declaration of built-in function ‘sin’
    foo.c:23: warning: format ‘%lf’ expects type ‘double’, but argument 2 has type ‘double *’





  13. #13
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    #include <math.h>

    remove the "&" from printf.
    Devoted my life to programming...

  14. #14
    Registered User
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    11
    ok as suggested, i removed the '&' included math.h, program is running but its going into infinite loop and terminating abruptly. i am presently using an ide called "dev c++"... please guide me to proceed..

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    You need to post your latest code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Efficient Sine Wave Generation
    By samGwilliam in forum C++ Programming
    Replies: 1
    Last Post: 05-24-2007, 06:37 PM
  2. Sine Wave Program
    By Stiks in forum C Programming
    Replies: 10
    Last Post: 10-15-2005, 12: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, 12:33 AM

Tags for this Thread