Page 1 of 1

Using dsp_math_sin to generate real-time sine waves.

Posted: Tue May 21, 2019 10:43 am
by notDilbert
Hi,

Can someone please help?

Would like to use dsp_math_sin function to generate long / continues sine frequencies that is not a multiple of the sample rate of 48kHz.

I have profiled dsp_math_sin and it seems to be fast enough.

My issue seems to be staying within the input limits of dsp_math_sin(q8_24 rad) rad "-MIN_Q8_24 + PI and MIN_Q8_24 - PI" for long / continues signals.

Have had success using lookup table's on frequencies that are multiple of the sample frequency.

Hope I made my self clear.

Kind regards

Re: Using dsp_math_sin to generate real-time sine waves.

Posted: Tue May 21, 2019 1:24 pm
by akp
If you constrain your input from 0 to 2*pi or -pi to +pi you will be fine with dsp_math_sin(). e.g.

while(1) {
output = dsp_math_sin(phase);
phase += phase_step;
phase %= 2*PI;
}

Re: Using dsp_math_sin to generate real-time sine waves.

Posted: Fri May 24, 2019 9:33 am
by notDilbert
Thank you.

This works for one frequency! but looks like it takes to long when calculating more than one frequency as witnessed by the I2S LRCLK streching.

I'm thinking of sharing the work between two cores. One services the DAC I2S and the other pre-calculates the next sample value for the DAC service task.

Am I on the right coarse with this approach?

Thank you

Re: Using dsp_math_sin to generate real-time sine waves.

Posted: Fri May 24, 2019 11:04 am
by akp
You might be able to do your own sin table faster. It all depends what resolution you need. You should google NCOs and the best sin implementations for them. You will need to balance speed, accuracy, and memory usage.

For instance I do a 32 bit unsigned number as the phase and map the range 0 to 2xPI onto 0 to 2^32 so just adding the phase step the 32 bit number rolls over every 2xPI anyway if you see what I mean. The input to the sin function is automatically constrained to 0 to 2xPI.

Re: Using dsp_math_sin to generate real-time sine waves.

Posted: Mon May 27, 2019 8:41 am
by notDilbert
Moving the dsp_math_sin() code to it's own core made all the difference. Managed to generate 7 individual sine's, mix then and be ready when I2S DAC needed data.

Thanks again

Re: Using dsp_math_sin to generate real-time sine waves.

Posted: Tue May 28, 2019 11:56 am
by akp
Great idea. Glad you had a spare core and it worked for you.