# help needed in sine generation of vhdl code.

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

1. ### senthilGuest

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
formulae.

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 =
-0.596
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

2. ### David BrownGuest

"senthil" <> wrote in message
news:...
> 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
> formulae.
>
> sin(x) = x - x3/3(fact) + x5/5(fact) _ ...
>
> i took only 3 terms.
>

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.

> 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 =
> -0.596
> 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.

David Brown, Feb 12, 2004

3. ### Egbert MolenkampGuest

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;
begin
stimulirocess
constant delta : real := 10.0E-4;
variable x : real := 0.0;
begin
x:=0.0;
while x < 4.0*MATH_PI loop
s <= sin(x);
c <= cos(x);
wait for 1 ns;
x:=x+delta;
end loop;
report "finished";
wait;
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
/math/this_should_be_one
run -all

Egbert Molenkamp

"senthil" <> wrote in message
news:...
> 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
> formulae.
>
> 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 =
> -0.596
> 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.

Egbert Molenkamp, Feb 12, 2004
4. ### vipinlal

Joined:
Feb 25, 2010
Messages:
38
0
you can find a sine wave generator program here..
vhdlguru.blogspot.com/2010/03/simple-sine-wave-generator-in-vhdl.html
hope this solves the problem.

vipinlal, Mar 17, 2010

Joined:
Mar 10, 2008
Messages:
348