Author Topic: Sine Wave on FPGA  (Read 2996 times)

0 Members and 1 Guest are viewing this topic.

Offline knightTopic starter

  • Regular Contributor
  • *
  • Posts: 50
  • Country: sg
Sine Wave on FPGA
« on: March 18, 2022, 10:47:41 am »
I want to make a sine wave using the approximation shown in this video,
I have an 8-bit phase accumulator which goes from 0 to 255 and then overflows. I want to use this 0-255 output and create the sine wave. Can anyone give an idea how to do it using the equations in the video?
Thanks :-+
 

Offline woofy

  • Frequent Contributor
  • **
  • Posts: 366
  • Country: gb
    • Woofys Place
Re: Sine Wave on FPGA
« Reply #1 on: March 18, 2022, 11:42:31 am »
I have an 8-bit phase accumulator which goes from 0 to 255 and then overflows. I want to use this 0-255 output and create the sine wave. Can anyone give an idea how to do it using the equations in the video?
Thanks :-+

If you have a RAM block available, use the output as an address to the RAM which will be loaded with a pre-calculated sine table.

Offline knightTopic starter

  • Regular Contributor
  • *
  • Posts: 50
  • Country: sg
Re: Sine Wave on FPGA
« Reply #2 on: March 18, 2022, 11:57:45 am »
I have an 8-bit phase accumulator which goes from 0 to 255 and then overflows. I want to use this 0-255 output and create the sine wave. Can anyone give an idea how to do it using the equations in the video?
Thanks :-+

If you have a RAM block available, use the output as an address to the RAM which will be loaded with a pre-calculated sine table.
I want to avoid using RAM as i plan to increase bit width of phase accumulator to 16 bits.
 

Offline emece67

  • Frequent Contributor
  • **
  • !
  • Posts: 614
  • Country: 00
Re: Sine Wave on FPGA
« Reply #3 on: March 18, 2022, 12:12:25 pm »
.
« Last Edit: August 19, 2022, 05:20:27 pm by emece67 »
 
The following users thanked this post: knight

Offline knightTopic starter

  • Regular Contributor
  • *
  • Posts: 50
  • Country: sg
Re: Sine Wave on FPGA
« Reply #4 on: March 18, 2022, 12:43:12 pm »
    AFAIK there are 3 recent threads here about how to compute sine/cosine values in embedded, so I advice on looking for them.

    There seem to be 4 basic approaches:
    • Table lookup
    • Polynomial approximations (with around 4 multiplications may be enough for you, using a 5rd order odd polynomial and Horner's scheme)
    • The "state variable" method, useful to generate the next sample based on the previous one (not such useful in this case)
    • The Bhaskara's formula (needs a division)

In your case, Bhaskara's is:
Code: [Select]
y = x*(128 - x)
sinx = 4*y/(20480 - y)
    Valid for [0 <= x <= 128] (meaning 0-180ยบ). You must manage the [128 < x < 256] case before applying the formula.

    Regards.
    For 128<x<256, it would be just the negative/complement of the original formula, right?
     

    Offline emece67

    • Frequent Contributor
    • **
    • !
    • Posts: 614
    • Country: 00
    Re: Sine Wave on FPGA
    « Reply #5 on: March 18, 2022, 01:22:55 pm »
    .
    « Last Edit: August 19, 2022, 05:20:37 pm by emece67 »
     

    Online Berni

    • Super Contributor
    • ***
    • Posts: 5023
    • Country: si
    Re: Sine Wave on FPGA
    « Reply #6 on: March 18, 2022, 01:37:13 pm »
    The most efficient method for generating high resolution sine waves in hardware is CORDIC:
    https://zipcpu.com/dsp/2017/08/30/cordic.html

    This is commonly used as a way of generating sine wave signals in the digital world. But it is not quite all that simple to do.

    Easier method that should still work well is a lookup table with interpolation between samples. If you stick to power of 2 numbers for your table size, then you can simplify the interpolation math down into just bitshit and addition operations. So it should be pretty compact and fast.
     

    Offline SiliconWizard

    • Super Contributor
    • ***
    • Posts: 15246
    • Country: fr
    Re: Sine Wave on FPGA
    « Reply #7 on: March 18, 2022, 06:09:44 pm »
    Using a table storing one period is definitely one easy approach and doesn't necessarily require a huge amount of samples. You just need to implement some interpolation on top of that, and the degree of interpolation + size of the table will determine your accuracy. A reasonably-sized table with even just linear interpolation is often more than good enough (but we know nothing about the OP's requirements.)

    You don't need a lot of memory. Even a small FPGA with a few KBytes of EBR will absolutely do the trick. One thousand samples is plenty for most cases. But you need interpolation!
    The benefit of the table-based approach with interpolation is that it can be reused for any kind of periodic signal, whereas implementations specific to sine functions will just generate sine waves. So unless you're VERY short on memory (like an ultra-small CPLD), I'll favor that.

    The other approach is CORDIC as said above. Actually, the base idea is a rotation matrix, something that I also use in software for generating continuous sine waves (much faster, and usually more accurate than implementing an approximation of sin(x) for any x, when you only need successive values with a fixed step in x.)

    « Last Edit: March 18, 2022, 06:12:13 pm by SiliconWizard »
     

    Online pcprogrammer

    • Super Contributor
    • ***
    • Posts: 4292
    • Country: nl
    Re: Sine Wave on FPGA
    « Reply #8 on: March 18, 2022, 06:55:41 pm »
    When it is only a sine that is needed one can suffice with just a quarter sine in a lookup table.

    See: https://www.fpga4fun.com/DDS.html

    Offline ejeffrey

    • Super Contributor
    • ***
    • Posts: 3881
    • Country: us
    Re: Sine Wave on FPGA
    « Reply #9 on: March 18, 2022, 07:27:50 pm »
    I have an 8-bit phase accumulator which goes from 0 to 255 and then overflows. I want to use this 0-255 output and create the sine wave. Can anyone give an idea how to do it using the equations in the video?
    Thanks :-+

    If you have a RAM block available, use the output as an address to the RAM which will be loaded with a pre-calculated sine table.
    I want to avoid using RAM as i plan to increase bit width of phase accumulator to 16 bits.

    You don't need the lookup table width to match your accumulator.  In fact it rarely does.  The accumulator width sets your frequency resolution while the lookup table width is normally close to your DAC resolution and contributes to spurs.  So a common approach is to have a 32-48 bit accumulator for very fine frequency resolution but only a 10-12 bit lookup table.

    As mentioned your lookup table only needs to hold 1/4 cycle, the rest can be calculated from that.  Interpolation helps, a lot and dithering can spread out the spurs to make them less objectionable.
     

    Offline BrianHG

    • Super Contributor
    • ***
    • Posts: 8073
    • Country: ca
    Re: Sine Wave on FPGA
    « Reply #10 on: March 18, 2022, 07:32:10 pm »
    Unless you store your table in logic gates, careful that you do not needlessly over-minimize.

    For example, Altera's M9K blockram has the smallest possible size of 1k X 8, or 256word x 32, or 512word x16. 

    Each of these will utilize 1 block ram chunk.

    Even if you think you are smart and say I will squeeze my quarter sine table into 64words by 8 bits, this will still consume the same 1 blockram chunk as making a table of 1024words by 8 bits, or 256words by 32 bits.  Only the extra bits are thrown in the garbage and cannot be used by any other HDL code.
    « Last Edit: March 18, 2022, 07:34:10 pm by BrianHG »
     

    Offline SiliconWizard

    • Super Contributor
    • ***
    • Posts: 15246
    • Country: fr
    Re: Sine Wave on FPGA
    « Reply #11 on: March 18, 2022, 07:47:18 pm »
    Even if you think you are smart and say I will squeeze my quarter sine table into 64words by 8 bits, this will still consume the same 1 blockram chunk as making a table of 1024words by 8 bits, or 256words by 32 bits.  Only the extra bits are thrown in the garbage and cannot be used by any other HDL code.

    Indeed.
     

    Offline NorthGuy

    • Super Contributor
    • ***
    • Posts: 3242
    • Country: ca
    Re: Sine Wave on FPGA
    « Reply #12 on: March 18, 2022, 07:52:42 pm »
    This is FPGA, so you don't need to use tricks to do things fast. With few pipelined DSPs you can calculate sine values with Taylor series at very high frequency. 400-500 MHz may be feasible. In reality, you don't really need it that fast.

    You can use various tricks to optimize your caluclations, but this won't really make it faster, but rather save the area. That is your efforts will lead to using less DSPs, or less logic, but they will not lead to increased speed.

    Thus, unless you're really concerned with using few extra DSPs, I would use Taylor series. Since you don't need high precision, you'll need only 2-3 members.
     

    Offline emece67

    • Frequent Contributor
    • **
    • !
    • Posts: 614
    • Country: 00
    Re: Sine Wave on FPGA
    « Reply #13 on: March 18, 2022, 09:01:29 pm »
    .
    « Last Edit: August 19, 2022, 05:20:51 pm by emece67 »
     

    Offline free_electron

    • Super Contributor
    • ***
    • Posts: 8550
    • Country: us
      • SiliconValleyGarage
    Re: Sine Wave on FPGA
    « Reply #14 on: March 18, 2022, 09:14:30 pm »
    Using a table storing one period
    you only need a quarter period. count up, count down, change sign, count up count down , change sign repeat ...
    Professional Electron Wrangler.
    Any comments, or points of view expressed, are my own and not endorsed , induced or compensated by my employer(s).
     

    Offline BrianHG

    • Super Contributor
    • ***
    • Posts: 8073
    • Country: ca
    Re: Sine Wave on FPGA
    « Reply #15 on: March 18, 2022, 09:42:33 pm »
    Using a table storing one period
    you only need a quarter period. count up, count down, change sign, count up count down , change sign repeat ...
    More like use the top 2 most significant bits of your position counter to decide the direction of addressing the table & if the output should be set to a negative value while all the least significant bits address your FPGA blockram.  At least with this, you can randomly jump to any angle within your sine table.

    Then look at the minimum FPGA blockram size and strategically choose you final resolution of your sine table.
     

    Offline hamster_nz

    • Super Contributor
    • ***
    • Posts: 2812
    • Country: nz
    Re: Sine Wave on FPGA
    « Reply #16 on: March 19, 2022, 06:11:20 am »
    I've added an new thread on how CORDIC works,, including a VHDL implementation.

    Hopefully those interested find it of value.

    CORDIC is really pretty neat, and can also be used to calculate atan2(y,x) and k*sqrt(x^2+y^2) too.
    « Last Edit: March 19, 2022, 06:20:40 am by hamster_nz »
    Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
     
    The following users thanked this post: knight


    Share me

    Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
    Smf