Forum Discussion

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

How can i use CORDIC ?

Hello i am trying to use the cordic core { http://opencores.org/project,cordic }

with no sucess.

Say i have a complex number of 4 + 5i.

I have inserted 1000 as the Xi input and 1001 as the Yi input

But the r2p_corproc is returning me wrong R and A values. It should be

r = 6.4031 angle = 0.8961 {matlab}

but instead i get

r= 103

a= 146811

My test bench is :

SIGNAL real_i : signed(15 downto 0) := "00000000"& "00000100" ;
SIGNAL imag_i : signed(15 downto 0) := "00000000"& "00000101" ;
SIGNAL ena_r_i : std_logic := '1';
SIGNAL rst_negado : std_logic := '1';
SIGNAL result_ang_o : unsigned(19 downto 0);
SIGNAL result_mag_o :  signed(19 downto 0) ;
SIGNAL test_clk: std_logic := '0';
-- component instatiation
component rl131 is
	port(
		clk_i    : in std_logic;
        rst_i_n  : in std_logic;
		ena_i    : in std_logic;
		Real_i	: in signed(15 downto 0);
		Imag_i   : in signed(15 downto 0);
		Rout_o	: out unsigned(19 downto 0);
		Aout_o	: out signed(19 downto 0)
	);
	end component;
BEGIN
 dut : rl131
	port map(
	 clk_i => test_clk,
	 ena_i => ena_r_i,
	 rst_i_n => rst_negado,
	 Real_i => real_i, 
	 Imag_i => imag_i, 
	 Rout_o => result_ang_o, 
	 Aout_o => result_mag_o);
  
test_clk <= not test_clk after clkperiod/2;

15 Replies

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

    Also i am trying to represent these numbers in binary:

    -0.5

    -0.86

    How can i represent them? I am using fixed point and shifting my number 2^22 times;;
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The main problem with using arithmetic on std_logic_vectors, is that you don't know at first glance when reading the code if you are dealing with signed or unsigned numbers. Sure for know you know what you wrote, but your code could end up copy/pasted in another file, where std_logic_vectors are supposed to be unsigned instead, or you could even have to deal with unsigned and signed values in the same code. Using the unsigned and signed types wakes the code more readable, more reusable and can reduce mistakes.

    As for the binary numbers, even if real arithmetic operations aren't synthesizable, you can still use real constants in your code. Something like:

    --- Quote Start ---

    constant MyNum : integer := integer(0.85*(2**22));

    --- Quote End ---

    Or if using vectors and declaring more constants, something like (assuming you are using 23 bit signed numbers, to have a scaling factor of 2^22):

    --- Quote Start ---

    constant VectorsSize : natural := 23;

    constant ScalingFactor : real := real(2**(VectorsSize-1));

    constant MyVector : signed := to_signed(integer(0.85*ScalingFactor),VectorsSize);

    --- Quote End ---

    If you need to do a lot of conversions, you could also define a function to do that instead.

    There is also a new fixed point library in VHDL, but I haven't tried it yet.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thanks Daix. Now i understand.

    However i can't multipliy a std_logic_vector (or a signed) for an integer. Can i?

    And does this := to_signed(integer(0.85*ScalingFactor),VectorsSize) ; work? Is it sinthetizable? I think i saw this on the fixed_point lib

    i tried the following:

    
    constant um_terco     : signed (40 downto 0) := to_signed(integer(0.3333),41);
      signal Va1_real      : std_logic_vector (117 downto 0);
    signal temp_fasor_real8      : std_logic_vector (76 downto 0);
    Va1_real =  std_logic_vector(um_terco * signed(temp_fasor_real8))
    

    where temp-fasor_real8 = 1773 in binary

    however the result was 0..
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    You can multiply a signed/unsigned with an integer with the numeric_std library. Be careful with multiplying fixed point vectors though, as you will need to shift the result back.

    As for your 0 result, it is because with this assignment:

    --- Quote Start ---

    constant um_terco : signed (40 downto 0) := to_signed(integer(0.3333),41);

    --- Quote End ---

    0.3333 is converted to the integer 0. You forgot to multiply 0.3333 by the scaling factor.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    with the fixed point library, you dont need to worry about scaling factors:

    
    constant UM_TERCO : sfixed := to_sfixed(0.3333, N-1, -M); --N integer bits, M fractional
    signal temp_fasor_real8 : sfixed(A downto 0)
    signal Va_real : signed(N+A-1 downto -M);
    Va_real <= temp_fasor_real8 * UM_TERCO;