We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Sawtooth wave
Page Index Toggle Pages: 1
Sawtooth wave (Read 1252 times)
Sawtooth wave
May 12th, 2010, 9:05am
 
Hi,

I want to know how to code/draw a sawtooth wave, I search for it at wikipedia, but I cant workout that formula, I could use toxi libs but I want to know how its done.

Any help will be much appreciated

Cheers
rS
Re: Sawtooth wave
Reply #1 - May 12th, 2010, 9:57am
 
Something like that?
Code:
int STEP_NB = 10;
int m = 75;
float ratio = 3.0;

void setup()
{
size(800, 200);
}

void draw()
{
background(255);
float stepSize = width / STEP_NB;
float step1 = stepSize / ratio;
for (int i = 0; i < STEP_NB; i++)
{
float step = i * stepSize;
line(step, m, step + step1, height - m);
line(step + step1, height - m, step + stepSize, m);
}
}
Re: Sawtooth wave
Reply #2 - May 13th, 2010, 1:27am
 
Exactly like that thanks PhiLho!

Now can this be achieve using the sin() method? I will have a play

rS
Re: Sawtooth wave
Reply #3 - May 13th, 2010, 3:44am
 
Here are some basic wave interpolations. For further research you should look after sound synthesis that heavy use wave generation. Sorry first post no link allowed.

Code:

/**
* waveform by GiL_TheB at yoyobafactory.net
*/


Waveform squa,tri,saw,ramp,hsb;

void setup(){
 size(256,256);
 frameRate(30);
 squa=new Waveform(Waveform.SQUARE,30,0);
 tri=new Waveform(Waveform.TRIANGLE,30,0);
 saw=new Waveform(Waveform.SAWTOOTH,30,0);
 ramp=new Waveform(Waveform.RAMP,30,0);
 hsb=new Waveform(Waveform.RAMP,120,0);

}

void draw(){
 colorMode(RGB, 255);

 fill(squa.next()*255.0f);
 rect(10,10,32,32);

 fill(tri.next()*255.0f);
 rect(52,10,32,32);


 fill(saw.next()*255.0f);
 rect(10,52,32,32);

 fill(ramp.next()*255.0f);
 rect(52,52,32,32);  

 colorMode(HSB, 255);

 fill(hsb.next()*255.0f,255.0f,255.0f);
 rect(10,94,74,32);  

}


class Waveform {
 static final int SINE =0;
 static final int SQUARE =1;
 static final int TRIANGLE =2;
 static final int SAWTOOTH =3;
 static final int RAMP =4;

 int period;
 int offset;
 int wave_type;

 Waveform (int wave_type0, int period0, int offset0) {
   wave_type=wave_type0;
   period=period0;
   offset=offset0;
 }

 float next(){
   float phase;

   switch(wave_type)
   {
   case 0: // sine
     phase=(frameCount+offset+0.5)%period;
     break;

   case 3: // sawtooth
     phase=(frameCount+offset)%(period+1);
     break;
   case 4: // ramp
     phase=(frameCount+offset)%(period+1);
     break;
   default :
     phase=(frameCount+offset)%period;
     break;

   }

   float fp=phase/period;
   float sample=0.0f;
   
   switch(wave_type)
   {
   case 0: // sine
     sample=(sin(fp*TWO_PI)+1)/2.0f;
     break;
   case 1: // square
     if(fp<0.5f)
       sample=0.0f;
     else
       sample=1.0f;
     break;
   case 2: // triangle
     if(fp<0.5f)
       sample=fp*2;
     else
       sample=1.0f-(fp-0.5f)*2.0f;
     break;
   case 3: // sawtooth
     sample=1.0f-fp;
     break;
   case 4: // ramp
     sample=fp;
     break;
   }
   return sample;
 }
}

-GiL_TheB,
Page Index Toggle Pages: 1