generate sine function with an amplitude from 0 to 10 in javascript

You can generate a sine function with an amplitude from 0 to 10 in JavaScript using the Math.sin() function. Here's an example code snippet:

index.tsx
function generateSineWithAmplitude(amplitude) {
  const frequency = 1; // Frequency of the sine wave

  // Generate the values for one complete cycle of the sine wave
  const values = [];
  for (let i = 0; i <= 360; i++) {
    const radians = (i * Math.PI) / 180;
    const value = Math.sin(frequency * radians) * amplitude;
    values.push(value);
  }

  return values;
}

// Example usage
const amplitude = 10;
const sineValues = generateSineWithAmplitude(amplitude);
console.log(sineValues);
495 chars
19 lines

In the generateSineWithAmplitude() function, we iterate from 0 to 360 degrees (or 2π radians), calculate the corresponding value of the sine function at each step, and scale it by the desired amplitude.

The sineValues array will contain the generated values for one complete cycle of the sine wave with the specified amplitude. You can modify the amplitude variable to adjust the range of the sine wave.

related categories

gistlibby LogSnag