DDS generating variable frequency(1Hz - 20kHz) sinewave using DAC

Thread Starter

zazas321

Joined Nov 29, 2015
936
Hey. I need to generate a variable frequency sinewave using DAC. I have implement sine wave look up table and phase accumulator:

C:
void SINE_LOOKUP_TABLE(){

const int BUFF_SIZE = 4096;  // size of output buffer (samples)
const int Fs = 32.768;       // sample rate (Hz)
const int LUT_SIZE = 1024;  // lookup table size

int16_t LUT[LUT_SIZE];      // our sine wave LUT

for (int i = 0; i < LUT_SIZE; ++i)
{
    LUT[i] = (int16_t)sinf(((float)i *360)/1024) *2047;
}                           // fill LUT with 16 bit sine wave sample values

}

//INPUT WHAT FREQUENCY YOU WANT
void SINE_VARIABLE_FREQUENCY(int frequency){

int16_t buff[BUFF_SIZE];     // output buffer
const int BUFF_SIZE = 4096;  // size of output buffer (samples)
const int frequency = 1000;          // frequency we want to generate (Hz)
const int LUT_SIZE = 1024;  // lookup table size
const int Fs = 32.768;       // sample rate (Hz)
const float delta_phi = (float) frequency / (float) Fs * LUT_SIZE;
                             // phase increment

float phase = 0.0f;          // phase accumulator

// generate buffer of output
  for (int i = 0; i < BUFF_SIZE; ++i)
  {
    int phase_i = (int)phase;        // get integer part of our phase
    buff[i] = LUT[phase_i];          // get sample value from LUT
    phase += delta_phi;              // increment phase
    if (phase >= (float)LUT_SIZE)    // handle wraparound
        phase -= (float)LUT_SIZE;
  }
}
I havent implemented it as I do not have my hardware yet.

I have one concern though - The microprocessor that I am using is stm32f412 and the DAC chip -
http://www.ti.com/general/docs/supp...&gotoUrl=http://www.ti.com/lit/gpn/dac121s101.

1. What clock frequency would I need to choose to generate 1Hz - 20Khz sine wave?
The DAC chip is connected to my microcontroller through SPI interface and the clock is set to 50Mhz.

2. I am also unsure about phase accumulator resolution. Does it have to match DAC resolution? Most websites say that most common phase accumulator use 24bit or 48bit resolution and I chose to use 12bit because thats my DAC resolution.
 

Attachments

danadak

Joined Mar 10, 2018
4,057
Hey. I need to generate a variable frequency sinewave using DAC. I have implement sine wave look up table and phase accumulator:

C:
void SINE_LOOKUP_TABLE(){

const int BUFF_SIZE = 4096;  // size of output buffer (samples)
const int Fs = 32.768;       // sample rate (Hz)
const int LUT_SIZE = 1024;  // lookup table size

int16_t LUT[LUT_SIZE];      // our sine wave LUT

for (int i = 0; i < LUT_SIZE; ++i)
{
    LUT[i] = (int16_t)sinf(((float)i *360)/1024) *2047;
}                           // fill LUT with 16 bit sine wave sample values

}

//INPUT WHAT FREQUENCY YOU WANT
void SINE_VARIABLE_FREQUENCY(int frequency){

int16_t buff[BUFF_SIZE];     // output buffer
const int BUFF_SIZE = 4096;  // size of output buffer (samples)
const int frequency = 1000;          // frequency we want to generate (Hz)
const int LUT_SIZE = 1024;  // lookup table size
const int Fs = 32.768;       // sample rate (Hz)
const float delta_phi = (float) frequency / (float) Fs * LUT_SIZE;
                             // phase increment

float phase = 0.0f;          // phase accumulator

// generate buffer of output
  for (int i = 0; i < BUFF_SIZE; ++i)
  {
    int phase_i = (int)phase;        // get integer part of our phase
    buff[i] = LUT[phase_i];          // get sample value from LUT
    phase += delta_phi;              // increment phase
    if (phase >= (float)LUT_SIZE)    // handle wraparound
        phase -= (float)LUT_SIZE;
  }
}
I havent implemented it as I do not have my hardware yet.

I have one concern though - The microprocessor that I am using is stm32f412 and the DAC chip -
http://www.ti.com/general/docs/suppproductinfo.tsp?distId=10&gotoUrl=http://www.ti.com/lit/gpn/dac121s101.

1. What clock frequency would I need to choose to generate 1Hz - 20Khz sine wave?
The DAC chip is connected to my microcontroller through SPI interface and the clock is set to 50Mhz.

2. I am also unsure about phase accumulator resolution. Does it have to match DAC resolution? Most websites say that most common phase accumulator use 24bit or 48bit resolution and I chose to use 12bit because thats my DAC resolution.
DAC does not have to match same bit depth as phase accumulator. Thats typically not the case.

The ap note also discusses freq calculation.

https://www.analog.com/media/en/training-seminars/tutorials/MT-085.pdf


Regards, Dana.
 

jjw

Joined Dec 24, 2013
823
Hey. I need to generate a variable frequency sinewave using DAC. I have implement sine wave look up table and phase accumulator:

C:
void SINE_LOOKUP_TABLE(){

const int BUFF_SIZE = 4096;  // size of output buffer (samples)
const int Fs = 32.768;       // sample rate (Hz)
const int LUT_SIZE = 1024;  // lookup table size

int16_t LUT[LUT_SIZE];      // our sine wave LUT

for (int i = 0; i < LUT_SIZE; ++i)
{
    LUT[i] = (int16_t)sinf(((float)i *360)/1024) *2047;
}                           // fill LUT with 16 bit sine wave sample values

}

//INPUT WHAT FREQUENCY YOU WANT
void SINE_VARIABLE_FREQUENCY(int frequency){

int16_t buff[BUFF_SIZE];     // output buffer
const int BUFF_SIZE = 4096;  // size of output buffer (samples)
const int frequency = 1000;          // frequency we want to generate (Hz)
const int LUT_SIZE = 1024;  // lookup table size
const int Fs = 32.768;       // sample rate (Hz)
const float delta_phi = (float) frequency / (float) Fs * LUT_SIZE;
                             // phase increment

float phase = 0.0f;          // phase accumulator

// generate buffer of output
  for (int i = 0; i < BUFF_SIZE; ++i)
  {
    int phase_i = (int)phase;        // get integer part of our phase
    buff[i] = LUT[phase_i];          // get sample value from LUT
    phase += delta_phi;              // increment phase
    if (phase >= (float)LUT_SIZE)    // handle wraparound
        phase -= (float)LUT_SIZE;
  }
}
I havent implemented it as I do not have my hardware yet.

I have one concern though - The microprocessor that I am using is stm32f412 and the DAC chip -
http://www.ti.com/general/docs/suppproductinfo.tsp?distId=10&gotoUrl=http://www.ti.com/lit/gpn/dac121s101.

1. What clock frequency would I need to choose to generate 1Hz - 20Khz sine wave?
The DAC chip is connected to my microcontroller through SPI interface and the clock is set to 50Mhz.

2. I am also unsure about phase accumulator resolution. Does it have to match DAC resolution? Most websites say that most common phase accumulator use 24bit or 48bit resolution and I chose to use 12bit because thats my DAC resolution.
1. In theory the dds clock frequency should be at least 2x highest output frequency. In practice to ease the low pass filtering, as high as practical.
The SPI transfer time should be less than than the dds clock cycle.

2. The resolution of the output frequency depends on the accumulator size.
fout= dPhi x fc/2^N where N is the number of bits in the accumulator.
for example: fc=120kHz, N=24, resolution or the smallest frequency step is 120000/ 2^24 ~ 0.007 Hz
 
Top