Page 81 - JavaScript
P. 81
amplitude of 2 in the range -1 to 1.
Graph of sine and cosine function: (courtesy Wikipedia)
They are both very handy for many types of periodic calculations, from creating sound waves, to
animations, and even encoding and decoding image data
This example shows how to create a simple sin wave with control over period/frequency, phase,
amplitude, and offset.
The unit of time being used is seconds.
The most simple form with control over frequency only.
// time is the time in seconds when you want to get a sample
// Frequency represents the number of oscillations per second
function oscillator(time, frequency){
return Math.sin(time * 2 * Math.PI * frequency);
}
In almost all cases you will want to make some changes to the value returned. The common terms
for modifications
• Phase: The offset in terms of frequency from the start of the oscillations. It is a value in the
range of 0 to 1 where the value 0.5 move the wave forward in time by half its frequency. A
value of 0 or 1 makes no change.
• Amplitude: The distance from the lowest value and highest value during one cycle. An
amplitude of 1 has a range of 2. The lowest point (trough) -1 to the highest (peak) 1. For a
wave with frequency 1 the peak is at 0.25 seconds, and trough at 0.75.
• Offset: moves the whole wave up or down.
To include all these in the function:
https://riptutorial.com/ 38

