Insight of the some different methods applicable in sine wave oscillator LUT generation




#define WAVE_SAMPLES 512

#define dcoffset 2047 
// In this case is the same as the Amplitude. Will vary in more complicated
// approaches, if you you want to create complex sinusoids

// default int is 32 bit, in most cases its best to use uint32_t but for 
// large arrays its better to use smaller
// data types if possible, here we are storing 12 bit samples in 16 bit ints

uint16_t sin_data[WAVE_SAMPLES];
float w ;    // ψ phase increment
float yi ;  // Amplitude of the signal
float phase;
uint16_t sign_samp;
uint16_t i;

void create2nd_sine (){
  Serial.println(" ");
  Serial.println("Sine table");
  // phase increment 2 PI/NwavetableSamples * 1 Hz period =0.0122718463
  // This can also be calculated in a different way more 2expensive"
  // 2 PI / sampleRate) * 42 Hz=0.122718463 i.e. 21504 sr will fit 
  //42 periods of 512 NwavetableSamples in a 21504 Hz  generator !
  // where sampleRate=21504 Hz
  // This could have also been implemented that way
  /*
     Nsamples = duration * sampleRate;
   for (i = 0; n < Nsamples; i++)
   sample[i] = sin((2.00 *PI/sampleRate) * frequency * n);
   */
  // Since  2*PI/fs is constant through the loop, we can calculate it just once. 
  // Furthermore, we can improve it by replacing the multiplication of the phase 
  // with an accumulator by addition. 
  w=(2.0 * PI)/WAVE_SAMPLES;
  for (i = 0; i <= 511; i++)
  {
    yi= dcoffset*sin(phase); // dcoffset is the same as the Amplitude
    phase += w;
    sign_samp=dcoffset+yi;     // dc offset translated for a 12 bit DAC
    sin_data[i]=sign_samp; // write value into array
    Serial.println(i);
    Serial.println(sin_data[i]);
  }
}
//

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly: 
  uint16_t i;
  i=4095;
  while(i--){
    //
  }
  create2nd_sine ();
  delay(500);
}





* Check the synth building blocks section in the links page
-Synth building blocks related
and other projects
http://dubworks.blogspot.co.uk/p/my-self-documented-tutorials.html


No comments:

Post a Comment

Feel free to contact me with any suggestions, doubts or requests.

Bless