CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Generating a Sum of Sines

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
danielz85



Joined: 22 Sep 2012
Posts: 37

View user's profile Send private message

Generating a Sum of Sines
PostPosted: Thu Nov 29, 2012 4:33 am     Reply with quote

Hi,

I'm trying to output a sum of several pure sine waves with the frequencies {17Khz, 17.2 Khz, 17.4Khz, etc}

I'm not quite sure what would be the best way to do it:
I thought I'd calculate in advance a 300-elements array, in which each element holds a sample of the sum, and then output it circularly using DAC. However, this method has it's downside which is calculating 300 elements, where each elements may require 8 (at worst) sine calculations, which is A LOT of calculation time...

Am I missing something here? or is this NOT the right way to generate this kind of a sum?

Thanks a lot in advance
Daniel
Ttelmah



Joined: 11 Mar 2010
Posts: 19559

View user's profile Send private message

PostPosted: Thu Nov 29, 2012 5:49 am     Reply with quote

Forget about calculating sines....

You only need bytes or words, corresponding to the DAC value you want for a sine. If you remember that values in a sine, are duplicated in the second quadrant in the opposite direction, and then (for a DAC), if you have +ve values going from (say - for a 10bit DAC), 0 to 511, then the two lower quadrants are the 512- each of these values, while the upper quadrants are 512+ each value. You can generate a DAC sine value, using a couple of tests, a lookup, and one addition or subtraction. Probably 1000* faster than generating these using normal arithmetic. You should be able to get a sine, in under thirty instructions. Further, if you then add just the 'offset' values together as integers (adding or subtracting each entry, from a total), then when finished, if you are adding (say) six sines, instead of dividing by 6 to get the required value for the DAC, multiply by 42 and divide by 256. Key here is that multiplication (for integers on a PIC18), takes typically 1/10th the time for a division. The exception is divisions by binary values (2,4,8,16 etc..), which can be done by simple rotations. You could then generate each sum value, suitably scaled, in something of the order of a 1/10000th second....

Best Wishes
temtronic



Joined: 01 Jul 2010
Posts: 9250
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Nov 29, 2012 5:52 am     Reply with quote

Since PICs weren't originally designed to do 'fancy math', one option if possible,is to 'offload' the math to a PC then just download the 'table of sums' values.
If it must be in the PIC, an option is to calculate only the first 90* worth of sums.The 2nd 90* is the reversed order of the 1st,the 3rd is the negative signed first90* ,the 4th 90* the negative signed,reversed ordered 1st90*.
This 'trick' reduces 75% of the calculations which WILL speed up the 'fancy math' operations.

Other 'tricks' include using sin tables and integer math.The first takes up space,both save time.

hth
jay
danielz85



Joined: 22 Sep 2012
Posts: 37

View user's profile Send private message

PostPosted: Thu Nov 29, 2012 8:07 am     Reply with quote

Ttelmah, temtronic

thank you very much for the quick replies.
I will try your approach.

best wishes,
Daniel
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group