csignal-processingstm32trigonometrydac

Single cycle 44.1kHz sampled 1kHz sine wave in c


I am attempting to create a single cycle sine wave in c. The frequency needs to be approximately 1kHz and sampled at 44.1kHz. This is because the sine lookup table is being fed into a stm32f4 microcontroller that is sampling at 44.1kHz and then out to 5 independent DACs. I have been having issues figuring out how to get exactly 1 cycle of the wave.

Currently I am getting about 10-11 cycles.

for(int j = 0; j < 45; j++){ 
    arr[j] = MAXVOLUME*((sin(2.0*PI*sineFrequency*j/44100.00)+1.0)/2.0);
}

Solution

  • Your divisor is wrong -- you want to divide by the number of samples, not by the sampling frequency. Which bring up the problem -- to have exactly one cycle of 1KHz sampled at 44.1KHz, you need 44.1 samples, which is not a round number. So you have two choices: