## Using dsp_math_sin to generate real-time sine waves. Topic is solved

If you have a simple question and just want an answer.
notDilbert
Member
Posts: 9
Joined: Mon Feb 25, 2019 3:05 pm

### Using dsp_math_sin to generate real-time sine waves.

Hi,

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

View Solution
akp
XCore Expert
Posts: 566
Joined: Thu Nov 26, 2015 11:47 pm
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;
}
notDilbert
Member
Posts: 9
Joined: Mon Feb 25, 2019 3:05 pm
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
akp
XCore Expert
Posts: 566
Joined: Thu Nov 26, 2015 11:47 pm
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.
notDilbert
Member
Posts: 9
Joined: Mon Feb 25, 2019 3:05 pm
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
akp
XCore Expert
Posts: 566
Joined: Thu Nov 26, 2015 11:47 pm
Great idea. Glad you had a spare core and it worked for you.