function/process to generate sine and cosine wave

Discussion in 'VHDL' started by FPGA, Feb 6, 2008.

  1. FPGA

    FPGA Guest

    I would like some guidline on writing a function or process to
    generate a sine wave and cosine wave. I want to include this into my
    library. Each time this function/process is called, I would like sine
    and cosines wave generated.

    Are there any functions in VHDL which would help us do so. I know
    there is a function UNIFORM to generate random numbers, not sure if
    there is anything available for sine and cosine.

    Your comments are appreciated.


    Anuja
    FPGA, Feb 6, 2008
    #1
    1. Advertising

  2. FPGA

    FPGA Guest

    I would like to use the CORDIC function available in math_real to
    generate the sine and cosine wave. Any inputs on if this is possible
    would be appreciated.
    FPGA, Feb 6, 2008
    #2
    1. Advertising

  3. FPGA wrote:
    > I would like some guidline on writing a function or process to
    > generate a sine wave and cosine wave. I want to include this into my
    > library. Each time this function/process is called, I would like sine
    > and cosines wave generated.
    >
    > Are there any functions in VHDL which would help us do so. I know
    > there is a function UNIFORM to generate random numbers, not sure if
    > there is anything available for sine and cosine.
    >
    > Your comments are appreciated.
    >
    >
    > Anuja


    what frequency?
    what resolution?
    synthesizable or testbench?

    andraka.com has a good cordic article on it among other things.

    -Jeff
    Jeff Cunningham, Feb 7, 2008
    #3
  4. FPGA

    Anuja Guest

    On Feb 6, 10:05 pm, Jeff Cunningham <> wrote:
    > FPGA wrote:
    > > I would like some guidline on writing a function or process to
    > > generate a sine wave and cosine wave. I want to include this into my
    > > library. Each time this function/process is called, I would like sine
    > > and cosines wave generated.

    >
    > > Are there any functions in VHDL which would help us do so. I know
    > > there is a function UNIFORM to generate random numbers, not sure if
    > > there is anything available for sine and cosine.

    >
    > > Your comments are appreciated.

    >
    > > Anuja

    >
    > what frequency?
    > what resolution?
    > synthesizable or testbench?
    >
    > andraka.com has a good cordic article on it among other things.
    >
    > -Jeff


    has to be synthesizable. No restriction on frequency or resolution as
    such.
    Anuja, Feb 7, 2008
    #4
  5. FPGA

    FPGA Guest

    On Feb 6, 10:05 pm, Jeff Cunningham <> wrote:
    > FPGA wrote:
    > > I would like some guidline on writing a function or process to
    > > generate a sine wave and cosine wave. I want to include this into my
    > > library. Each time this function/process is called, I would like sine
    > > and cosines wave generated.

    >
    > > Are there any functions in VHDL which would help us do so. I know
    > > there is a function UNIFORM to generate random numbers, not sure if
    > > there is anything available for sine and cosine.

    >
    > > Your comments are appreciated.

    >
    > > Anuja

    >
    > what frequency?
    > what resolution?
    > synthesizable or testbench?
    >
    > andraka.com has a good cordic article on it among other things.
    >
    > -Jeff


    has to be synthesizable. No restriction on frequency or resolution.
    FPGA, Feb 7, 2008
    #5
  6. FPGA

    Ray Andraka Guest

    FPGA wrote:
    > I would like some guidline on writing a function or process to
    > generate a sine wave and cosine wave. I want to include this into my
    > library. Each time this function/process is called, I would like sine
    > and cosines wave generated.
    >
    > Are there any functions in VHDL which would help us do so. I know
    > there is a function UNIFORM to generate random numbers, not sure if
    > there is anything available for sine and cosine.
    >
    > Your comments are appreciated.
    >
    >
    > Anuja



    Is this for a testbench or for sometihng that is going into hardware?
    If for a testbench, just use the math_real sin and cos functions,
    converting the resulting reals to the format you need to drive your
    hardware. If it is to be synthesized into hardware, the math_real
    library isn't going to help you much, as reals don't map directly into
    synthesizable hardware.
    Ray Andraka, Feb 7, 2008
    #6
  7. FPGA

    FPGA Guest

    On Feb 6, 11:42 pm, Ray Andraka <> wrote:
    > FPGA wrote:
    > > I would like some guidline on writing a function or process to
    > > generate a sine wave and cosine wave. I want to include this into my
    > > library. Each time this function/process is called, I would like sine
    > > and cosines wave generated.

    >
    > > Are there any functions in VHDL which would help us do so. I know
    > > there is a function UNIFORM to generate random numbers, not sure if
    > > there is anything available for sine and cosine.

    >
    > > Your comments are appreciated.

    >
    >
    > Is this for a testbench or for sometihng that is going into hardware?
    > If for a testbench, just use the math_real sin and cos functions,
    > converting the resulting reals to the format you need to drive your
    > hardware.  If it is to be synthesized into hardware, the math_real
    > library isn't going to help you much, as reals don't map directly into
    > synthesizable hardware.


    Using the math_real library can do for now. I have had a look at the
    sin and cos functions there. I am also aware on how to conver the real
    format to the format I want. I intend to generate a wave. sin and cos
    in math_real would just generate a value for a particular input. How
    can i generate a sine or cos waveform. I wish to make this function
    parametrized as follows (not sure if this should go into a function or
    process)

    function sin (x: signed, bw : integer) return signed is

    x: signed input with variable bit width
    bw : desired bit width of output

    I can convert signed to real and then use "sin" function in math_real.
    I am still not sure on how I would generate a wave for each call.

    Your comments would be appreciated
    FPGA, Feb 7, 2008
    #7
  8. FPGA

    Ray Andraka Guest

    You need to run a count or phase accumulation to feed into the sin/cos
    function.

    signal phase: real;
    constant scale: real:= 2.0**bw;


    for n in 0 to 10000 loop
    sig <= std_logic_vector(to_signed(integer(scale*sin(phase)),bw);
    phase <= phase + phase_increment;
    wait until clk='1';
    end loop;





    FPGA wrote:

    > On Feb 6, 11:42 pm, Ray Andraka <> wrote:
    >
    >>FPGA wrote:
    >>
    >>>I would like some guidline on writing a function or process to
    >>>generate a sine wave and cosine wave. I want to include this into my
    >>>library. Each time this function/process is called, I would like sine
    >>>and cosines wave generated.

    >>
    >>>Are there any functions in VHDL which would help us do so. I know
    >>>there is a function UNIFORM to generate random numbers, not sure if
    >>>there is anything available for sine and cosine.

    >>
    >>>Your comments are appreciated.

    >>
    >>
    >>Is this for a testbench or for sometihng that is going into hardware?
    >>If for a testbench, just use the math_real sin and cos functions,
    >>converting the resulting reals to the format you need to drive your
    >>hardware. If it is to be synthesized into hardware, the math_real
    >>library isn't going to help you much, as reals don't map directly into
    >>synthesizable hardware.

    >
    >
    > Using the math_real library can do for now. I have had a look at the
    > sin and cos functions there. I am also aware on how to conver the real
    > format to the format I want. I intend to generate a wave. sin and cos
    > in math_real would just generate a value for a particular input. How
    > can i generate a sine or cos waveform. I wish to make this function
    > parametrized as follows (not sure if this should go into a function or
    > process)
    >
    > function sin (x: signed, bw : integer) return signed is
    >
    > x: signed input with variable bit width
    > bw : desired bit width of output
    >
    > I can convert signed to real and then use "sin" function in math_real.
    > I am still not sure on how I would generate a wave for each call.
    >
    > Your comments would be appreciated
    Ray Andraka, Feb 7, 2008
    #8
  9. Bresenhams algorithm provides better results with far less accumulator
    bits
    compared to a phase accumulator.
    Also, the input parameters (M waves in N cycles) are very convenient
    in many cases.


    If the OP wants a sine, one option that avoids the sine computation is
    an oscillator:
    next_sin <= cos*k;
    next_cos <= sin*k;
    The constant k determines the frequency.

    Kolja Sulimma

    On 7 Feb., 09:00, Ray Andraka <> wrote:
    > You need to run a count or phase accumulation to feed into the sin/cos
    > function.
    >
    > signal phase: real;
    > constant scale: real:= 2.0**bw;
    >
    > for n in 0 to 10000 loop
    > sig <= std_logic_vector(to_signed(integer(scale*sin(phase)),bw);
    > phase <= phase + phase_increment;
    > wait until clk='1';
    > end loop;
    comp.arch.fpga, Feb 7, 2008
    #9
  10. FPGA

    John Guest

    On Feb 7, 11:00 am, Ray Andraka <> wrote:
    > You need to run a count or phase accumulation to feed into the sin/cos
    > function.
    >
    > signal phase: real;
    > constant scale: real:= 2.0**bw;
    >
    > for n in 0 to 10000 loop
    >         sig <= std_logic_vector(to_signed(integer(scale*sin(phase)),bw);
    >         phase <= phase + phase_increment;
    >         wait until clk='1';
    > end loop;
    >


    That's good, but in math_real library sin/cos generate by Tailor
    formula. And type real have very small size. All of this get not good
    sin. These can give that noise of your system will grow.
    John, Feb 7, 2008
    #10
  11. FPGA

    Tricky Guest


    > Using the math_real library can do for now. I have had a look at the
    > sin and cos functions there. I am also aware on how to conver the real
    > format to the format I want. I intend to generate a wave. sin and cos
    > in math_real would just generate a value for a particular input. How
    > can i generate a sine or cos waveform. I wish to make this function
    > parametrized as follows (not sure if this should go into a function or
    > process)
    >



    Are you sure you want to go down this path? If you eventually need it
    synthesizable, you will have to throw away ALL of the work you have
    done with the math_real package, unless you're using it as a model of
    the final block.

    The easiest way to implement sin and cosine in and FPGA is using a
    look up table (normally a ROM), with the address formed from the angle
    coefficient.
    Tricky, Feb 8, 2008
    #11
  12. FPGA

    FPGA Guest

    On Feb 8, 4:17 am, Tricky <> wrote:
    > > Using the math_real library can do for now. I have had a look at the
    > > sin and cos functions there. I am also aware on how to conver the real
    > > format to the format I want. I intend to generate a wave. sin and cos
    > > in math_real would just generate a value for a particular input. How
    > > can i generate a sine or cos waveform. I wish to make this function
    > > parametrized as follows (not sure if this should go into a function or
    > > process)

    >
    > Are you sure you want to go down this path? If you eventually need it
    > synthesizable, you will have to throw away ALL of the work you have
    > done with the math_real package, unless you're using it as a model of
    > the final block.
    >
    > The easiest way to implement sin and cosine in and FPGA is using a
    > look up table (normally a ROM), with the address formed from the angle
    > coefficient.


    I want to do a simulation only version right now (some requirements
    change on the fly :) ). The sin/cos wave has to be implemented in such
    a way that it can be used as an input to ant UUT. I am not yet sure if
    this would work by using a function. What other options do I have? I
    cannot use it as a component. I am making a library of functions so I
    want to make this as generic as possible. If i implement this using a
    process, where can this go in a package?
    FPGA, Feb 8, 2008
    #12
  13. FPGA a écrit :

    > I want to do a simulation only version right now (some requirements
    > change on the fly :) ). The sin/cos wave has to be implemented in such
    > a way that it can be used as an input to ant UUT. I am not yet sure if
    > this would work by using a function. What other options do I have? I
    > cannot use it as a component. I am making a library of functions so I
    > want to make this as generic as possible. If i implement this using a
    > process, where can this go in a package?


    Hello
    You can not generate a waveform using a function only. A function
    computes an output value based on input values.
    I think a procedure is what you want.

    Nicolas
    Nicolas Matringe, Feb 9, 2008
    #13
  14. On 9 Feb., 11:05, Nicolas Matringe <> wrote:

    >
    > Hello
    > You can not generate a waveform using a function only. A function
    > computes an output value based on input values.
    > I think a procedure is what you want.


    The input value could be the frequency and the number of samples,
    the output value could be an array of real contining the waveform.

    Not very useful for synthesis, but a good solution for a testbench.

    type real_array is array(natural range<>) of real;
    function sinewave(f: real; n: natural) return real_array;

    Kolja Sulimma
    comp.arch.fpga, Feb 9, 2008
    #14
  15. FPGA

    FPGA Guest

    On Feb 9, 5:40 am, "comp.arch.fpga" <> wrote:
    > On 9 Feb., 11:05, Nicolas Matringe <> wrote:
    >
    >
    >
    > > Hello
    > > You can not generate a waveform using a function only. A function
    > > computes an output value based on input values.
    > > I think a procedure is what you want.

    >
    > The input value could be the frequency and the number of samples,
    > the output value could be an array of real contining the waveform.
    >
    > Not very useful for synthesis, but a good solution for a testbench.
    >
    > type real_array is array(natural range<>) of real;
    > function sinewave(f: real; n: natural) return real_array;
    >
    > Kolja Sulimma


    Thank you so much for your help. I appreciate it. I will try your
    suggestions and see how it works out.
    FPGA, Feb 9, 2008
    #15
  16. On Sat, 9 Feb 2008 02:40:45 -0800 (PST), "comp.arch.fpga"
    <> wrote:

    >On 9 Feb., 11:05, Nicolas Matringe <> wrote:
    >
    >>
    >> Hello
    >> You can not generate a waveform using a function only. A function
    >> computes an output value based on input values.
    >> I think a procedure is what you want.

    >
    >The input value could be the frequency and the number of samples,
    >the output value could be an array of real contining the waveform.
    >
    >Not very useful for synthesis, but a good solution for a testbench.


    Actually quite useful for synthesis; the array returned can be the lookup table
    in a sine wave generator.

    (although Xilinx XST may be unbelievably slow in evaluating the function)

    >type real_array is array(natural range<>) of real;
    >function sinewave(f: real; n: natural) return real_array;



    - Brian
    Brian Drummond, Feb 9, 2008
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    4,648
    Jim Lewis
    Nov 28, 2005
  2. red floyd
    Replies:
    5
    Views:
    1,030
    Nathan Wijnia
    Jun 21, 2007
  3. FPGA
    Replies:
    8
    Views:
    1,250
  4. rara10
    Replies:
    0
    Views:
    926
    rara10
    Jan 30, 2009
  5. Shiva
    Replies:
    2
    Views:
    1,625
    Chris Maryan
    Feb 2, 2009
Loading...

Share This Page