Simple C++ waveform generation

Discussion in 'Homework Help' started by JD Dyslexic, Nov 18, 2008.

  1. JD Dyslexic

    Thread Starter New Member

    Aug 7, 2008
    5
    0
    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
     
  2. veritas

    Active Member

    Feb 7, 2008
    167
    0
    Code ( (Unknown Language)):
    1.  
    2. for (n=0 ; n<256 ;n++) //this loop runs 256 times, with n having a value from 0 to 255
    3. {
    4.     angle = (n/256.0)*360; //angle in degrees (between 0 and 360)
    5.     angle = angle*PI/180.0; //angle in radians
    6.     sine = 127*sin(angle) + 128; //convert sin(angle) to a normalized value between 0 and 255
    7.     fwrite(&sine,sizeof(sine),1,fp); //print sin(angle)
    8. }  //you need a closing bracket
    9.  
    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: Nov 18, 2008
  3. JD Dyslexic

    Thread Starter New Member

    Aug 7, 2008
    5
    0
    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
     
  4. blazedaces

    Active Member

    Jul 24, 2008
    130
    0
    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
     
  5. veritas

    Active Member

    Feb 7, 2008
    167
    0
    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.
     
Loading...