Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
14 years ago

sine/cosine in fixed_pkg

is sine or cosine funtion is supported by fixed_pkg? i meanif i write c<= sin(k) using fixed_pkg, then the code is right?

5 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    No. There are no sin or cosine functions available in synthsisable VHDL.

    You will need to setup a lookup table of fixed point values but you can use the sin/cosine functions for the real type to set this up. You will need a function to setup your ROM, but this is a great example of a initialisation function:

    eg:

    
    type sin_table_t is array(0 to 1023) sfixed(0 downto -15);
    function set_sin_table return sin_table_t is
      variable ret_table : sin_table_t;
    begin
      for i in 0 to 1023 loop
        ret_table(i) := to_sfixed( (real(i) * MATH_PI) / 1024.0, 0, -15);
      end loop;
      return ret_table;
    end function set_sin_table;
    --This look up table has 1024 points of sin 0 - PI
    constant SIN_TABLE : sin_table_t := set_sin_table;
    
    Then you use the input to the table to be the fraction of pi for the value

    output <= SIN_TABLE(X);

    X here represents the fraction of PI, so setting X to 500 in real terms means sin(PIx500 / 1024).
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    PS. you need to include the ieee.math_real package to get the MATH_PI constant.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    (at the risk of being pedantic) You can save half of the table (or increase precision) by only initialising up to PI / 2 covering the first quadrant and express the other 3 quadrants in function of the first, e.g. sin (PI / 2 + alpha) == sin (PI / 2 - alpha)

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    @ tricky sir,following your susggesion i an trying to do a program,like below

    library IEEE;

    use IEEE.STD_LOGIC_1164.ALL;

    use IEEE.NUMERIC_STD.ALL;

    USE IEEE.MATH_REAL.ALL;

    library ieee_proposed;

    --------use ieee_proposed.numeric_std_additions .all;

    ---------use ieee_proposed.std_logic_1164_additions.all;

    use ieee_proposed.fixed_float_types.all;

    use ieee_proposed.fixed_pkg.all;

    entity sine_table is

    Port ( a : in sfixed(3 downto -3);

    c: out sfixed(0 downto -15));

    end sine_table;

    architecture Behavioral of sine_table is

    type sin_table_t is array(0 to 1023)of sfixed(0 downto -15);

    function set_sin_table return sin_table_t is

    variable ret_table : sin_table_t;

    begin

    for i in 0 to 1023 loop

    ret_table(i) := to_sfixed( (real(i) * MATH_PI) / 1024.0, 0, -15);

    end loop;

    return ret_table;

    end function set_sin_table;

    --This look up table has 1024 points of sin 0 - PI

    constant SIN_TABLE : sin_table_t := set_sin_table;

    begin

    c <= sin_table(a);

    end Behavioral;

    but there occurs an error :Line 61. Wrong index type for sin_table.

    plz fix the error sir.:cry: