element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Forum Adding 4 sinewaves of different phase and amplitude
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 8 replies
  • Answers 1 answer
  • Subscribers 46 subscribers
  • Views 890 views
  • Users 0 members are here
  • Code Exchange
  • programming
Related

Adding 4 sinewaves of different phase and amplitude

Former Member
Former Member over 11 years ago

Hello,

My project is basically modified SPWM which involves adding 4 sinewaves each of different frequency and amplitude to generate a reference wave. I am using MP Lab IDE v8.76 to program a dsPIC30f4011.

How do i add these using the software mentioned above?

I have a lookup table for the sinewaves.

signed  int lookup[256] ={0,807,1614,2420,3224,4027,4827,5624,6417,7207,7992,8773,9548,

   10317,11080,11837,12586,13328,14061,14786,15502,16208,16905,

   17592,18267,18932,19585,20226,20855,21472,22075,22665,23241,

   23803,24351,24883,25401,25903,26390,26860,27315,27752,28173,

   28577,28963,29332,29683,30016,30330,30627,30904,31163,31403,

   31624,31826,32008,32171,32315,32439,32543,32627,32692,32737,

   32761,32766,32751,32717,32662,32587,32493,32379,32246,32092,

   31920,31728,31516,31286,31036,30768,30481,30175,29851,29510,

   29150,28772,28377,27965,27536,27090,26627,26149,25654,25144,

   24619,24079,23524,22955,22372,21775,21165,20542,19907,19260,

   18601,17931,17250,16558,15856,15145,14425,13695,12958,12212,

   11459,10700,9933,9161,8383,7600,6813,6021,5226,4427,3626,2822,

   2017,1211,404,-404,-1211,-2017,-2822,-3626,-4427,-5226,-6021,

   -6813,-7600,-8383,-9161,-9933,-10700,-11459,-12212,-12958,-13695,

   -14425,-15145,-15856,-16558,-17250,-17931,-18601,-19260,-19907,

   -20542,-21165,-21775,-22372,-22955,-23524,-24079,-24619,-25144,

   -25654,-26149,-26627,-27090,-27536,-27965,-28377,-28772,-29150,

   -29510,-29851,-30175,-30481,-30768,-31036,-31286,-31516,-31728,

   -31920,-32092,-32246,-32379,-32493,-32587,-32662,-32717,-32751,

   -32766,-32761,-32737,-32692,-32627,-32543,-32439,-32315,-32171,

   -32008,-31826,-31624,-31403,-31163,-30904,-30627,-30330,-30016,

   -29683,-29332,-28963,-28577,-28173,-27752,-27315,-26860,-26390,

   -25903,-25401,-24883,-24351,-23803,-23241,-22665,-22075,-21472,

       -20855,-20226,-19585,-18932,-18267,-17592,-16905,-16208,-15502,

   -14786,-14061,-13328,-12586,-11837,-11080,-10317,-9548,-8773,

   -7992,-7207,-6417,-5624,-4827,-4027,-3224,-2420,-1614,-807, 0};

 

But i am a little confused as to how to take into account the phase difference and add them.

Thanks in advance.

  • Sign in to reply
  • Cancel
  • shabaz
    0 shabaz over 11 years ago

    Hi Dilip,

     

    Yes, it's a bit tricky, but hopefully not too hard. If you have four lookup tables containing your input signals (sinewaves) sampled at the same rate, then the four lookup arrays will be of different lengths (i.e. one will be of length 256 as in your example) but others will be of different length if you store up to a non-truncated signal (sinewave).

    Then, you could use four variables to store the current index into each lookup array, and increment each one up to the length of the array and then reset the index vars to zero independantly. Initial index value for any array would adjust your phasing.

    There are probably other ways to do it too (and I'm not familiar with your target device), this method should work with any processor.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to shabaz

    Hi shabaz, thanks for your idea.

    The thing is i'm having difficulty in finding the syntax that could get me to add them. Given that i will have to take into account the phase difference too. And i'm not too familiar coding in MP Lab sadly.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 11 years ago in reply to Former Member

    It looks like your data is 16-bit with full amplitude, so it may need scaling to a lower amplitude before you store the lookup tables, if you want to stay in 16-bit arithmetic. Then, your four signals can literally be summed (i.e. dac_out=lookup0[idx0]+lookup1[idx1]+lookup2[idx2]+lookup3[idx[3] ). Then increment the idxn variables and set to zero if any have reached the limit.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to shabaz

    This is a part of my code where i calculate the amplitude values and duty cycle values for each wave.

     

    void __attribute__((__interrupt__)) _PWMInterrupt(void)

    {

      shift_0 = shift_0+freq;

      sample1 = shift_0>>8;

      sine_R1 = lookup1[sample1];

      REFERENCE1 = (sine_R1*(long)amp1)>>15;

      PDC1 = REFERENCE1+2000;

     

      shift_45 = shift_45+freq;

      sample2 = shift_45>>8;

      sine_R2 = lookup2[sample2];

      REFERENCE2 = (sine_R2*(long)amp3)>>15;

      PDC2 = REFERENCE2+2000;

     

      shift_90 = shift_90+freq;

      sample3 = shift_90>>8;

      sine_R3 = lookup3[sample3];

      REFERENCE2 = (sine_R2*(long)amp3)>>15;

      PDC3 = REFERENCE3+2000;

     

      shift_180 = shift_180+freq;

      sample4 = shift_180>>8;

      sine_R4 = lookup4[sample4];

      REFERENCE2 = (sine_R2*(long)amp4)>>15;

      PDC4 = REFERENCE4+2000;

     

    now i need to add these individual amplitudes to get my final amplitude which i will be controlling at run time.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to Former Member

    The last two paras are

     

    shift_90 = shift_90+freq;

      sample3 = shift_90>>8;

      sine_R3 = lookup3[sample3];

      REFERENCE3 = (sine_R3*(long)amp3)>>15;

      PDC3 = REFERENCE3+2000;

     

      shift_180 = shift_180+freq;

      sample4 = shift_180>>8;

      sine_R4 = lookup4[sample4];

      REFERENCE4 = (sine_R4*(long)amp4)>>15;

      PDC4 = REFERENCE4+2000;

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 11 years ago in reply to Former Member

    Scale the lookup tables beforehand appropriately so that you don't need to convert to long and right-shift by 15. I don't know what the 2000 offset is for by the way.

    Secondly, not sure what the code is doing to be honest - you'd need four sets of shift_90, shift_180 etc., one per lookup array since the array lengths will be different. Also not sure what freq is, and why the right-shift. Maybe it's simpler just incrementing the four variables as I mentioned.

    Finally, all you'd need to do is do an arithmetic sum of PDC1+PDC2.. etc.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to shabaz

    The shift_45, shift_90 and shift_180 represent the different sinewaves. And i am using four different lookup tables. 2000 is to set the duty cycle. if you refer to my earlier code, i'v made a small change.Not sure if its right.

    REFERENCE=REFERENCE1+REFERENCE2+...+REFERENCE4

    PDC1=REFERENCE+2000

     

    REFERENCE is my final resultant.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Former Member
    0 Former Member over 11 years ago in reply to shabaz

    The lookup table is the same. I'm just storing it in four different locations. One for each individual wave.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube