Simple C++ waveform generation

Thread Starter

JD Dyslexic

Joined Aug 7, 2008
5
I have to program an Eprom to contain 16 different waveforms using C++. Examples of these waveforms are an AC square wave, positive going 10% duty cycle pulse (0 - +V) and symmetrical AC triangular waveform(+V--V)
I have been given an example of a sinewave in C++, but I have no experience with C++, could you please explain simply each line and how to manipulate it to create the different waveforms. Any advice is appreciated.

Sine wave example.
// sine wave
for (n=0 ; n<256 ;n++)
{
angle = (n/256.0)*360;
angle = angle*PI/180.0;
sine = 127*sin(angle) + 128;
fwrite(&sine,sizeof(sine),1,fp);

Thankyou
 

veritas

Joined Feb 7, 2008
167
Rich (BB code):
for (n=0 ; n<256 ;n++) //this loop runs 256 times, with n having a value from 0 to 255
{
    angle = (n/256.0)*360; //angle in degrees (between 0 and 360)
    angle = angle*PI/180.0; //angle in radians
    sine = 127*sin(angle) + 128; //convert sin(angle) to a normalized value between 0 and 255
    fwrite(&sine,sizeof(sine),1,fp); //print sin(angle)
}  //you need a closing bracket
To generalize, you want to compute the value f(angle) for each value of n in the loop, normalize it to your range, and then print it.
 
Last edited:

Thread Starter

JD Dyslexic

Joined Aug 7, 2008
5
Thanks very much for your help, if I understand correctly then to make a half rectified waveform it would it be:

for(n=0 ; n<128 ;n++)
{
angle = (n/256.0)*360;
angle = angle*PI/180.0;
sine = 127*sin(angle) + 128;
fwrite(&sine,sizeof(sine),1,fp);
}
for(n=128 ; n<256 ; n++)
{
fwrite("128",1,1,fp);
}

I am still having a little trouble understanding how to create a symmetrical AC triangular waveform(+v - -V). Can anyone point me in the right direction.
thanks
 

blazedaces

Joined Jul 24, 2008
130
Thanks very much for your help, if I understand correctly then to make a half rectified waveform it would it be:

for(n=0 ; n<128 ;n++)
{
angle = (n/256.0)*360;
angle = angle*PI/180.0;
sine = 127*sin(angle) + 128;
fwrite(&sine,sizeof(sine),1,fp);
}
for(n=128 ; n<256 ; n++)
{
fwrite("128",1,1,fp);
}

I am still having a little trouble understanding how to create a symmetrical AC triangular waveform(+v - -V). Can anyone point me in the right direction.
thanks
Do you mean how to create a triangular waveform at all, or a rectified or half-rectified one?

For making one at all, you need to look into fourier series.

Good luck,

-blazed
 

veritas

Joined Feb 7, 2008
167
I am still having a little trouble understanding how to create a symmetrical AC triangular waveform(+v - -V). Can anyone point me in the right direction.
thanks
You will do it just how you broke the square wave into two different segments.

For each segment, you have a linear function with a slope and a y-intercept. You just need to figure each of these.
 
Top