help needed in sine generation of vhdl code.

Discussion in 'VHDL' started by senthil, Feb 12, 2004.

  1. senthil

    senthil Guest

    hello friends,

    for the fft and ifft part of dsp, w.r to twiddle factor, we need the
    cosine and sine terms. for that i do my sine code w.r to sine series

    sin(x) = x - x3/3(fact) + x5/5(fact) _ ...

    i took only 3 terms.

    and my coding was given below

    term : =1
    sum := 1

    for n in 1 to 3 loop

    term := (-term)*input **2/real((2n*(2n+1)));
    sum := sum + term;
    end loop

    output <= input * sum;

    i found one problem , ie., if i give input = 3.534, i got an output =
    that is wrong answer.
    but actual value from the calculator, is sin(3.534) = -0.3818..

    what is the problem behind that.
    give some guidance.

    tool : modelsim 5.5 SE vhdl.
    senthil, Feb 12, 2004
    1. Advertisements

  2. senthil

    David Brown Guest

    That formula for calculating sin is fine for doing theoretical maths, but it
    is not good as a calculation method, since it converges very slowly. In
    particular, it gets worse the further you go from 0 - to converge sin(3.534)
    to four decimal places (to the correct value of -0.3824, not the one you
    gave below) takes 8 terms.

    If you move your input closer to 0, such as by using sin x == sin (pi - x),
    and try calculating sin(-0.3924), you get four decimal places after 3 terms.

    Where you go from here depends on what you actually want to do with the
    sines - if you are only looking for simulation and don't care about speed,
    then just pick a big enough number of terms. If you need high accuracy and
    can use floating point, then look for tchebychev (sp?) polynomials. Typical
    synthesisable solutions use lookup tables.
    David Brown, Feb 12, 2004
    1. Advertisements

  3. Maybe you can use the IEEE package math_real.
    This package contains often used constants (math_e, math_pi, ..) and
    functions (sqrt, exp, log, log2, sin, arccos, sinh, ..).
    (not synthesisable)

    ModelSim has this package.

    Here an example

    library ieee;
    use ieee.std_logic_1164.all;
    use ieee.math_real.all;
    entity math is
    end math;

    architecture demo of math is
    signal s, c, this_should_be_one : real := 0.0;
    constant delta : real := 10.0E-4;
    variable x : real := 0.0;
    while x < 4.0*MATH_PI loop
    s <= sin(x);
    c <= cos(x);
    wait for 1 ns;
    end loop;
    report "finished";
    end process;

    this_should_be_one <= s**2 + c**2;
    end demo;

    And if you use the macro file beneath you get a nice waveform.

    vsim math
    onerror {resume}
    quietly WaveActivateNextPane {} 0
    add wave -noupdate -format Analog-Step -offset -1.0 -scale 40.0 /math/s
    add wave -noupdate -format Analog-Step -offset -0.58 -scale 40.0 /math/c
    add wave -noupdate -format Analog-Step -offset -0.14 -scale 40.0
    run -all

    Egbert Molenkamp
    Egbert Molenkamp, Feb 12, 2004
  4. senthil


    Feb 25, 2010
    Likes Received:
    you can find a sine wave generator program here..
    hope this solves the problem.
    vipinlal, Mar 17, 2010
  5. senthil


    Mar 10, 2008
    Likes Received:
    I believe your looking for code which should be synthesized for hardware.
    Your able to use Excel as well.
    jeppe, Mar 17, 2010
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.