Forum Discussion

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

Generating a Sine Wave

Hello Friends,

I am a newbie.. hope you dont mind if my query is odd..

i wanted to the know like how can we generate a SINE WAVE.. by writing a verilog or vhdl code.

i browsed through some online material, and what i got to know is, one best possible way is using look up table. but when i generate the look up table( i used excel sheet and using these values as a text file), i cant read that LUT as a text since it holds fractions(floating point values) how to avoid this problem and any other method for generating sine wave.

Thanks in advance :)

Pradeep

10 Replies

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

    I recommend reading up about fixed point rather than floating point. A fixed point number is essensially an integer with a 2^n bias, but it represents a fractional number.

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

    If you don't want to write your own code then you can use off-the-shelf altera's NCO to generate sine waves or you can google www.opencores.org to find if they have any ready to use core available for this.

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

    Thank you Ketan :) and Tricky :) for your valuable and prompt response, i would look into the info as what you have given me but if i dont get any clarity then can you help me furthur probably with any code..and before that can you clarify my furthur doubts please

    1) how can we read the values stored in text as DECIMALS into FPGA..i know how to read hex values from text using

    reg [31:0] Mem [0:3599];

    initial

    $readmemh("sine.txt",Mem); ==>> this reads values stored in sine.txt as only hex how can i read them in decimal format directly

    2) i have pasted the sine.txt which i generated using excel sheet..

    i have taken some 3600 samples of sine wave i.e, 2*pi/3600 and for each of that value i have taken sine values and stored here and i thought, i can read these sine values in to fpga and display out the values, only (3) is stored as sine.txt

    (1) (2) (3)

    S.No 2*Pi/3600 Sin((2*Pi/3600)*S.No)

    1 0.001745329 0.001745328

    2 0.003490659 0.003490651

    3 0.005235988 0.005235964

    4 0.006981317 0.00698126

    5 0.008726646 0.008726535

    6 0.010471976 0.010471784

    7 0.012217305 0.012217001

    8 0.013962634 0.01396218

    9 0.015707963 0.015707317

    10 0.017453293 0.017452406

    11 0.019198622 0.019197442

    12 0.020943951 0.02094242

    13 0.02268928 0.022687334

    14 0.02443461 0.024432178

    15 0.026179939 0.026176948

    16 0.027925268 0.027921639

    17 0.029670597 0.029666244

    18 0.031415927 0.031410759

    19 0.033161256 0.033155178

    20 0.034906585 0.034899497

    21 0.036651914 0.036643709

    22 0.038397244 0.038387809

    . . .

    . . .

    . . .

    extended to 3600 samples

    as i told you i cant read this fractional values into fpga.

    i guess i am wrong here but could please tell me how to read these(ex: 0.001745328) type of values into fpga which command is useful

    i hope i will get a reply soon :)
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    1. You can take one onchip memory component and write down hex file to initialize it with values you want or values you have in 'sine.txt'. You can refer to Quartus II handbook on how to initialize onchip memory using hex files.

    2. You will not be able to store float values directly in onchip memory so either you can multiply it with some proper value to have rounded integer values or store decimal and fraction parts separately in memory and use it in you application.

    I am not sure whether this has answer your question or not, but if you can decribe what you want to do with this sine values then may be I can have better answer.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thank you Ketan :) once again..

    i am working on this just for my understanding..as i am a newbie i wanted to plot or display a sine wave from Look Up Table..

    as you have said i have taken 2*pi/20 and completed 20 samples i converted them into integers upto 3 digits and took 2's compliment. these i generated in excel sheet

    2*pi/20 Sin((2*Pi/3600)*S.No) I2 * 1000 INT(J2) 2's Compliment

    1) 0.314159265 0.309016994 309.0169944 309 0100110101

    2) 0.628318531 0.587785252 587.7852523 587 1001001011

    3) 0.942477796 0.809016994 809.0169944 809 1100101001

    4) 1.256637061 0.951056516 951.0565163 951 1110110111

    5) 1.570796327 1 1000 1000 1111101000

    6) 1.884955592 0.951056516 951.0565163 951 1110110111

    7) 2.199114858 0.809016994 809.0169944 809 1100101001

    8) 2.513274123 0.587785252 587.7852523 587 1001001011

    9) 2.827433388 0.309016994 309.0169944 309 0100110101

    10)3.141592654 3.67523E-15 0 0 0000000000

    11)3.455751919 -0.309016994 -309.0169944 -310 0011001010

    12)3.769911184 -0.587785252 -587.7852523 -588 0110110100

    13)4.08407045 -0.809016994 -809.0169944 -810 0011010110

    14)4.398229715 -0.951056516 -951.0565163 -952 0001001000

    15)4.71238898 -1 -1000 -1000 0000011000

    16)5.026548246 -0.951056516 -951.0565163 -952 0001001000

    17)5.340707511 -0.809016994 -809.0169944 -810 0011010110

    18)5.654866776 -0.587785252 -587.7852523 -588 0110110100

    19)5.969026042 -0.309016994 -309.0169944 -310 0011001010

    20)6.283185307 -7.35046E-15 -7.35046E-12 0 0000000000

    does this work,

    also the kind of DAC what we r having is 12 bit DAC , but before that i just wanted to display (probabily plot sinewave) using these values.. so can i read the above binary values using $readmemb command

    and also later how can i tell simulator that i have negative numbers..please can you help me with this situation.. i think you can understand that i am a newbiein programming.

    if i am wrong in any way can you suggest me more.

    waiting for your reply

    Thanks in advance :)

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

    First of all, you can not use system task (e.g. $readmemb) in synthesis, system tasks and functions are basically for verification/simulation. So if your intention is to simulate your design you can use system task and function.

    You can use integer data type for your variable to let simulator know about negative numbers or add one more bit in at MSB to indicate sign of number (e.g. 1 at MSB represent negative value, 0 for positive). I think you should be able to load memory content using $readmemb from a text file. Make sure the format of text file is as mentioned in LRM of Verilog.

    I would suggest you to look at some good verilog programming book or LRM to have more idea on programming.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thank you Ketan..

    sure i will be looking into that..

    i just now found out from some of discussions, that..it will be a good idea to feed those values to DAC after storing them into memory to generate a sine wave.. so i wanted to ask you that can i load the values from sine.txt using $readmemb (what i got to know is some synthesis tools synthesize it if we keep under initialize block)

    or can we generate input values by any other means and store them and give them to DAC

    can you help me in this respect too.. please dontmind if my query is odd

    i hope to get a reply

    Thanks in advance :)

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

    --- Quote Start ---

    First of all, you can not use system task (e.g. $readmemb) in synthesis, system tasks and functions are basically for verification/simulation. So if your intention is to simulate your design you can use system task and function.

    --- Quote End ---

    Afaik Xilinx ISE lets you do it (at least they let you use the VHDL equivalent), and is mentioned in one of its code templates for initialising memories. Its a real shame that Quartus wont support it.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Sure you will be able to store values in FPGA's onchip memory, for that you need to instance one onchip memory component and initialize it's hex file with whatever data you want. So when you program FPGA; on-chip memory will be initialized with values you put in hex file and then you can read value from this memory and throw it to DAC and observe the sine wave on output of DAC.

    But I suggest to use off-the-shelf NCO component avaliable in Quartus II to generate sine wave, you may find some reference design on how to use NCO on altera's website. Output of NCO can be directly given to DAC or you may need to use one or two flop to synchronize everything properly at high speed.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thank you Ketan:) for all your help.. it indeed helped me a lot in understanding the right approach.

    i would be follwing your inputs..if i have any more quries i would revert back to you..

    Thanks a lot :)

    Pradeep