Thread: ...simple sine wave?

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Question ...simple sine wave?

    I really suck at math, I admit it. Can someone please show me an equation for generating a sine wave?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Wow. Math is wierd. So I just try this random equation and though it doesn't give me a sine wave, it creates fascinating patterns. If you run Windows try it out:

    Some interesting frequencies to try are 16, 11, 22, 400, 98, 99, 77, 3.1, 3.14, to name just a few.

    [edit] oops...the equation:

    y = amplitude * sin(frequency * x);

    ..."frequency" is just a name for the variable. I have no idea what it truly represents.

    BTW: Is there a formal name for this equation?






    [edit]

    I have posted a newer version at the bottom of this page.
    Please try it out.

    [/edit]
    Last edited by Sebastiani; 12-11-2002 at 02:30 PM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Looks good, sorry I didn't see the question earlier I could have actually answered it. Oh well.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, actually, I don't think it's the basic sine-wave equation (though it seems to generate them as a side-effect). Do you know that one?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    It depends upon what you are using for 'x'. Could you post your source or something?

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    It's just the x coord.

    Code:
    void DrawEquation(double a_frequency, double an_amplitude){
     double
      x = 1,
      y = 1,
      cx = box.Left(),
      cy = box.VerticalCenter(),
      maxX = box.Right(),
      maxY = box.Top();
    
     BrushFill();
     GetPen(box_color, 4); // ...get a 4-pixel wide pen...
     DrawRectangle(box);
     box.Inflate(5);
     DrawRectangle(box);
     box.Inflate(-5);
     GetPen(line_color);
    
     MoveTo(cx, cy);
    
     for( ; ((x + cx) < maxX); ++x){
      y = an_amplitude * sin(a_frequency * x);
      y = -y;
       if((y + cy) > maxY)
        LineTo(x + cx, y + cy);
     }
     DrawText();
     Invalidate();
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The code looks sound. ANy of the side-effects you are referring to are common in all sine wave programs. You can force the user to keep their amplitude within a fixed range to avoid making the sine wave get out of control.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Interesting. Ok, thanks.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    y = amplitude * sin(frequency * x);

    ..."frequency" is just a name for the variable. I have no idea what it truly represents.

    BTW: Is there a formal name for this equation?
    It is a discrete sine. A property of discrete sines is that they do not necessarily need to be periodic. That is why you get your patterns.

  10. #10
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >..."frequency" is just a name for the variable. I have no idea what it truly represents.

    The input parameter for sin is an angle in radians. You get a quarter oscillation every pi/2 radians.

    In other words, if sin(0) will give you 0, sin(pi/2) will give you +1, sin(pi) gives you 0 and sin(3pi/2) gives you -1.

    Therefore if you wanted your sine wave to render a quarter osicallation every 100 pixels, you would calculate your frequency to be:

    f = pi / 100 / 2

    so that you would get a complete oscillation every 400 pixels along the x-axis. In otherwords, you frequency is expressed as oscillations per 400 pixels.

    You may also want to extend your equation with x/y offsets to give you positioning control over the sine wave. I.e.

    y = yoffset + (amp*sin(f* (x + xoffset)))


    >It is a discrete sine. A property of discrete sines is that they do not necessarily need to be periodic. That is why you get your patterns.

    I've no idea what this means. As far as I am aware it's just an equation for a sine wave.

  11. #11
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >It is a discrete sine.

    Just tried the program. I see what is meant by discrete now. It is rendered it in discrete steps.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  12. #12
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    >I've no idea what this means. As far as I am aware it's just an
    >equation for a sine wave.

    Sebastiani used the equation to calculate numbers for certain values of x, he probably used a loop to let x vary from a start value to an end value and found patterns in the output which were not sine waves. This is because the sine equation is discrete, a computer can't work with continue signals, only with discrete signals. The output Sebastiani has seen is the output of a discrete sine.

  13. #13
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    At the risk of sounding pedantic, I beg to differ.

    >This is because the sine equation is discrete

    The equation is not discrete. However, the algorithm Sebastiani uses to render the wave is. There is a subtle difference.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I really appreciate your input, Davros, Shiro. And Davros, thank you for the informative equations. I will try them out.

    Sebastiani used the equation to calculate numbers for certain values of x, he probably used a loop to let x vary from a start value to an end value and found patterns in the output which were not sine waves. This is because the sine equation is discrete, a computer can't work with continue signals, only with discrete signals. The output Sebastiani has seen is the output of a discrete sine.
    I don't understand. Does this mean that if I were to output the equation on an ocillascope the pattern would be different?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #15
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Thank you so much. It works beautifully now.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compression/Decompression Wave File and MP3
    By cindy_16051988 in forum Projects and Job Recruitment
    Replies: 51
    Last Post: 04-29-2006, 06:25 AM
  2. writing a wave file from my wave samples
    By Queatrix in forum Windows Programming
    Replies: 2
    Last Post: 12-31-2005, 11:54 PM
  3. Sine Wave Program
    By Stiks in forum C Programming
    Replies: 10
    Last Post: 10-15-2005, 12:35 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Processing a sine wave file
    By subdene in forum C++ Programming
    Replies: 2
    Last Post: 07-12-2004, 03:04 PM