I really suck at math, I admit it. Can someone please show me an equation for generating a sine wave?
This is a discussion on ...simple sine wave? within the C++ Programming forums, part of the General Programming Boards category; I really suck at math, I admit it. Can someone please show me an equation for generating a sine wave?...
I really suck at math, I admit it. Can someone please show me an equation for generating a sine wave?
Code:bool fun(bool value) { return std::pow(std::exp(1), std::complex<float>(0, 1) * std::complex<float>(std::atan(1)*(1 << (value + 2)))) .real() > 0; }
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 01:30 PM.
Code:bool fun(bool value) { return std::pow(std::exp(1), std::complex<float>(0, 1) * std::complex<float>(std::atan(1)*(1 << (value + 2)))) .real() > 0; }
Looks good, sorry I didn't see the question earlier I could have actually answered it. Oh well.
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:bool fun(bool value) { return std::pow(std::exp(1), std::complex<float>(0, 1) * std::complex<float>(std::atan(1)*(1 << (value + 2)))) .real() > 0; }
It depends upon what you are using for 'x'. Could you post your source or something?
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:bool fun(bool value) { return std::pow(std::exp(1), std::complex<float>(0, 1) * std::complex<float>(std::atan(1)*(1 << (value + 2)))) .real() > 0; }
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.
Interesting. Ok, thanks.
Code:bool fun(bool value) { return std::pow(std::exp(1), std::complex<float>(0, 1) * std::complex<float>(std::atan(1)*(1 << (value + 2)))) .real() > 0; }
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.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?
>..."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.
>It is a discrete sine.
Just tried the program. I see what is meant by discrete now. It is rendered it in discrete steps.
>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.
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.
I really appreciate your input, Davros, Shiro. And Davros, thank you for the informative equations. I will try them out.
I don't understand. Does this mean that if I were to output the equation on an ocillascope the pattern would be different?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.
Code:bool fun(bool value) { return std::pow(std::exp(1), std::complex<float>(0, 1) * std::complex<float>(std::atan(1)*(1 << (value + 2)))) .real() > 0; }
Thank you so much. It works beautifully now.
Code:bool fun(bool value) { return std::pow(std::exp(1), std::complex<float>(0, 1) * std::complex<float>(std::atan(1)*(1 << (value + 2)))) .real() > 0; }