Showing posts with label Sine Wave. Show all posts
Showing posts with label Sine Wave. Show all posts

Wednesday, 6 November 2013

Quick pictures update on ADSR

 Exp.attack at 0.4 and sustain level at 7/8 ( 20 Ms div)




A quick update to show that first try, worked as expected. Spent a few good hours in the process of starting to optimize the different building blocks id gathered, and make them interact. Tried to keep it as readable as possible and document the little quirks, and possible variations, references, etc in the implementation methods.
We have officially an ADSR with variable exponential attack, variable "decay-sustain ratio" ( allows both bypassing the sustain stage, and also looping/extending the sustain stage ), and variable duration/period... For now !


Next comes LFO's from all the wavetables available, simple filters, and start implementing simple convolution reverb. Lets see how far can i  "push the envelope" ( No pun intended).





Square wave and exp.attack at 0.5 ( .2 s Div )


Monday, 30 September 2013

Symmetry of waves in wavetable generators and the differences and implications..


The reason why im sharing these wave table generators is because of the fact that some of the ones i found didnt comply with the symmetry of waveforms ( Take the above graph as the correct reference of how they should relate !).
Why is this so important ?! Hmmm, try a guess ?! * Ill leave it to the reader to guess why, as an exercise, as its quite obvious !
Instead they have the period of  the Sawtooth wave starting as 0, when it should be starting at the DC-offset value ( 2047 for a 12-bits DAC, for example).
So, it was starting at PI, instead of zero. Triangle wave were starting at 1.5 PI( which both differences are easy and dirty ways of doing it, but this time didnt satisfy my needs, where i need something correct according to the symmetry for interaction and calculation purposes.
So this was my quick solution to it. I hope to maybe improve on this as soon as i can spare a moment.
Also intend to  do a see-sawtooth ( with more decrement intermediate stages) generator.


// create the individual samples for our Sawtooth-wave table
void createSawTable()
{
  Serial.println(" ");
  Serial.println("Saw table");
  for(uint32_t nIndex = 0;nIndex < WAVE_SAMPLES;nIndex++)
  {
    // normalised to 12 bit range 0-4095
    nSawTable[nIndex] = nIndex * 8;  // == nIndex + (4096/512 == 8);because it   //  never reaches 512
    Serial.println(nSawTable[nIndex]);
  }
}
// create the individual samples for our triangle-wave table
void createTriangleTable()
{
  //Serial.println(" ");
  //Serial.println("triangle table");
  for (uint32_t nIndex = 0;  nIndex < WAVE_SAMPLES; ++ nIndex) 
  { 
    if( nIndex <=128 ){
      nTriangleTable[nIndex] = nIndex * 16 + 2047 ; //+offset
      //Serial.println(nTriangleTable[nIndex]);
      //Serial.println(nIndex);
    }
    else if(nIndex >128 && nIndex < 384 )
    {
      nTriangleTable[nIndex] = 4095-((nIndex-128)*16);
      //Serial.println(nTriangleTable[nIndex]);
      //Serial.println(nIndex);
    }
    else if (nIndex == 384){
      nTriangleTable[nIndex] = 0;
      //Serial.println(nTriangleTable[nIndex]);
      //Serial.println(nIndex);
    }     else if (nIndex > 384){       nTriangleTable[nIndex] = (nIndex-384)*16;        //Serial.println(nTriangleTable[nIndex]);       //Serial.println(nIndex);     }        } }
void createSq_WaveTable() {   Serial.println(" ");   Serial.println("Square Wave table");   for(uint32_t nIndex = 0;nIndex < WAVE_SAMPLES;nIndex++)   {     // despite using numbers directly here, keep in mind its related to     // WAVE_SAMPLES as a variable     // if(nIndex < (WAVE_SAMPLES/2))     if( nIndex <256 ){       nSq_WaveTable[nIndex] = 4095 ; //       Serial.println(nSq_WaveTable[nIndex]);     }     else if(nIndex >=256 && nIndex <= WAVE_SAMPLES )     {       nSq_WaveTable[nIndex] =0;       Serial.println(nSq_WaveTable[nIndex]);     }        } }

Sunday, 1 September 2013

Simple sine wave generator template in C


As i only used a limited amplitude on the sine wave generator for Arduino Due's DAC, im posting a template here that should help you make the necessary changes( someone asked me this).
This specific template code was written for C code in Codeblocks for Gcc, so you will have to be able to know the differences.

# include < stdio.h >
# include < stdlib.h >
# include < math.h >
# define n_points 32 //256 is ideal for microcontrollers or highers if youre computing complex sinusoids
int main ()
{
    float pi = 3.141592;
    float w ;    // ψ
    float yi ;
    float phase;
    int sign_samp;
    int sin_data[n_points];  // sine LUT Array
    int i;
    w= 2*pi;
    w= w/n_points;
    for (i = 0; i <= n_points; i++)
    {
        yi= 2047*sin(phase);
        phase=phase+w;
        sign_samp=2047+yi;     // dc offset translated for a 12 bit DAC
        //sign_samp+= b;         // Add adc value; Keep it at zero for pure sine
        sin_data[i]=sign_samp; // write value into array
        /*
        */
    }
    for (i = 0; i <= n_points; i++)
    {
        int k=sin_data[i];
        printf ("sine is %d\n", k);
        printf ("i is ... %d\n", i);
    }
    float x;
    printf ("Enter a number ... \n");
    scanf ("%f", &x); // keeps the debugging console on codeblocks open ;)
    return 0;
}





Sine plot with result of a 32 n_points as above using GNUmeric



Pseudo-code


"Two random variables were talking in a bar. They thought they were being discrete but I heard their chatter continuously "

(1) Signal Processing for Communications by Paolo Prandoni and Martin Vetterli
(2) http://dubworks.blogspot.co.uk/p/blog-page.html