# ...simple sine wave?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-09-2002
Sebastiani
...simple sine wave?
I really suck at math, I admit it. Can someone please show me an equation for generating a sine wave? :rolleyes:
• 12-10-2002
Sebastiani
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.

 oops...the equation:

y = amplitude * sin(frequency * x);

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

BTW: Is there a formal name for this equation?

[/edit]
• 12-10-2002
master5001
Looks good, sorry I didn't see the question earlier I could have actually answered it. Oh well.
• 12-10-2002
Sebastiani
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?
• 12-10-2002
master5001
It depends upon what you are using for 'x'. Could you post your source or something?
• 12-10-2002
Sebastiani
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(); }```
• 12-10-2002
master5001
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.
• 12-10-2002
Sebastiani
Interesting. Ok, thanks.
• 12-10-2002
Shiro
Quote:

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.
• 12-10-2002
Davros
>..."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.
• 12-10-2002
Davros
>It is a discrete sine.

Just tried the program. I see what is meant by discrete now. It is rendered it in discrete steps.
• 12-10-2002
Shiro
>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.
• 12-10-2002
Davros
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.
• 12-10-2002
Sebastiani
I really appreciate your input, Davros, Shiro. And Davros, thank you for the informative equations. I will try them out.

Quote:

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?
• 12-10-2002
Sebastiani
Thank you so much. It works beautifully now.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last