Computer Code for Generating an Array of a Sine Wave

Here’s some simple computer code (in the C language) for generating an array with a 512 entry sine wave in it:

#define TABLE_SIZE 512

#define TWO_PI (3.14159 * 2)

float samples [TABLE_SIZE];

float phaseIncrement = TWO_PI/TABLE_SIZE;

float currentPhase = 0.0;

int i;

for (i = 0; i < TABLE_SIZE; i ++){

samples[i] = sin(currentPhase);

currentPhase += phaseIncrement;

}
					

What can you say about the frequency of the sine wave computed above? (Hint: You need to know the sampling rate, or how many samples we’re computing per second; typically, that might be 44.1 kHz.) How about the amplitude?—how would you take the code above and change the amplitude? Change the frequency? Maybe distort the sine wave?

How much do you remember from geometry class? Or better yet, how much do you remember from Chapter 3, where we talked about phasors?

What’s TWO_PI doing in there? Try to figure out what’s going on in this code! What would happen, for example, if you changed sin(currentPhase) to sin(currentPhase)/2, or 2*sin(currentPhase)? If you know a programming language, try writing a program that does a graphical printout of the wave.