Announcement

Collapse
No announcement yet.

sine wave tables

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • sine wave tables

    I'm pretty lost here. What I need to do is build some sinewave tables. The end result is to use these tables in a PIC micro to send the values to a 14 bit DAC. Basically I want to input a frequency and build a table for it. For you people great in math the ability to mix two frequencies would be great but I think a summing opamp will solve that problem

    I just need pointed in the right direction, not asking for someone to write the code for me

    an example table:

    Code:
    ' table for DAC sine wave 8 bit I think
    DAC_Table: CData $80,$00,$81,$C9,$83,$92,$85,$5B,$87,$24,$88,$EC,_
    $8A,$B4,$8C,$7A,$8E,$40,$90,$04,$91,$C8,$93,$89,$95,$4A,$97,$08,$98,_
    $C5,$9A,$80,$9C,$39,$9D,$F0,$9F,$A4,$A1,$56,$A3,$05,$A4,$B2,$A6,$5B,_
    $A8,$02,$A9,$A6,$AB,$46,$AC,$E3,$AE,$7C,$B0,$12,$B1,$A4,$B3,$32,$B4,_
    $BD,$B6,$43,$B7,$C5,$B9,$42,$BA,$BB,$BC,$30,$BD,$9F,$BF,$0A,$C0,$71,_
    $C1,$D2,$C3,$2D,$C4,$84,$C5,$D5,$C7,$21,$C8,$68,$C9,$A8,$CA,$E3,$CC,_
    $18,$CD,$47,$CE,$71,$CF,$94,$D0,$B0,$D1,$C7,$D2,$D7,$D3,$E1,$D4,$E4,_
    $D5,$E0,$D6,$D6,$D7,$C5,$D8,$AD,$D9,$8F,$DA,$69,$DB,$3C,$DC,$08,$DC,_
    $CD,$DD,$8B,$DE,$42,$DE,$F1,$DF,$98,$E0,$39,$E0,$D1,$E1,$62,$E1,$EC,_
    $E2,$6E,$E2,$E8,$E3,$5B,$E3,$C6,$E4,$29,$E4,$84,$E4,$D7,$E5,$23,$E5,_
    $66,$E5,$A2,$E5,$D6,$E6,$02,$E6,$26,$E6,$42,$E6,$56,$E6,$62,$E6,$66,_
    $E6,$62,$E6,$56,$E6,$42,$E6,$26,$E6,$02,$E5,$D6,$E5,$A2,$E5,$66,$E5,_
    $23,$E4,$D7,$E4,$84,$E4,$29,$E3,$C6,$E3,$5B,$E2,$E8,$E2,$6E,$E1,$EC,_
    $E1,$62,$E0,$D1,$E0,$39,$DF,$98,$DE,$F1,$DE,$42,$DD,$8B,$DC,$CD,$DC,_
    $08,$DB,$3C,$DA,$69,$D9,$8F,$D8,$AD,$D7,$C5,$D6,$D6,$D5,$E0,$D4,$E4,_
    $D3,$E1,$D2,$D7,$D1,$C7,$D0,$B0,$CF,$94,$CE,$71,$CD,$47,$CC,$18,$CA,_
    $E3,$C9,$A8,$C8,$68,$C7,$21,$C5,$D5,$C4,$84,$C3,$2D,$C1,$D2,$C0,$71,_
    $BF,$0A,$BD,$9F,$BC,$30,$BA,$BB,$B9,$42,$B7,$C5,$B6,$43,$B4,$BD,$B3,_
    $33,$B1,$A4,$B0,$12,$AE,$7C,$AC,$E3,$AB,$46,$A9,$A6,$A8,$02,$A6,$5B,_
    $A4,$B2,$A3,$05,$A1,$56,$9F,$A4,$9D,$F0,$9C,$39,$9A,$80,$98,$C5,$97,_
    $08,$95,$4A,$93,$89,$91,$C8,$90,$04,$8E,$40,$8C,$7A,$8A,$B4,$88,$EC,_
    $87,$24,$85,$5B,$83,$92,$81,$C9,$80,$00,$7E,$36,$7C,$6D,$7A,$A4,$78,_
    $DB,$77,$13,$75,$4B,$73,$85,$71,$BF,$6F,$FB,$6E,$37,$6C,$76,$6A,$B5,_
    $68,$F7,$67,$3A,$65,$7F,$63,$C6,$62,$0F,$60,$5B,$5E,$A9,$5C,$FA,$5B,_
    $4D,$59,$A4,$57,$FD,$56,$59,$54,$B9,$53,$1C,$51,$83,$4F,$ED,$4E,$5B,_
    $4C,$CD,$4B,$42,$49,$BC,$48,$3A,$46,$BD,$45,$44,$43,$CF,$42,$60,$40,_
    $F5,$3F,$8E,$3E,$2D,$3C,$D2,$3B,$7B,$3A,$2A,$38,$DE,$37,$97,$36,$57,_
    $35,$1C,$33,$E7,$32,$B8,$31,$8E,$30,$6B,$2F,$4F,$2E,$38,$2D,$28,$2C,_
    $1E,$2B,$1B,$2A,$1F,$29,$29,$28,$3A,$27,$52,$26,$70,$25,$96,$24,$C3,_
    $23,$F7,$23,$32,$22,$74,$21,$BD,$21,$0E,$20,$67,$1F,$C6,$1F,$2E,$1E,_
    $9D,$1E,$13,$1D,$91,$1D,$17,$1C,$A4,$1C,$39,$1B,$D6,$1B,$7B,$1B,$28,_
    $1A,$DC,$1A,$99,$1A,$5D,$1A,$29,$19,$FD,$19,$D9,$19,$BD,$19,$A9,$19,_
    $9D,$19,$9A,$19,$9D,$19,$A9,$19,$BD,$19,$D9,$19,$FD,$1A,$29,$1A,$5D,_
    $1A,$99,$1A,$DC,$1B,$28,$1B,$7B,$1B,$D6,$1C,$39,$1C,$A4,$1D,$17,$1D,_
    $91,$1E,$13,$1E,$9D,$1F,$2E,$1F,$C6,$20,$67,$21,$0E,$21,$BD,$22,$74,_
    $23,$32,$23,$F7,$24,$C3,$25,$96,$26,$70,$27,$52,$28,$3A,$29,$29,$2A,_
    $1F,$2B,$1B,$2C,$1E,$2D,$28,$2E,$38,$2F,$4F,$30,$6B,$31,$8E,$32,$B8,_
    $33,$E7,$35,$1C,$36,$57,$37,$97,$38,$DE,$3A,$2A,$3B,$7B,$3C,$D2,$3E,_
    $2D,$3F,$8E,$40,$F5,$42,$60,$43,$CF,$45,$44,$46,$BD,$48,$3A,$49,$BC,_
    $4B,$42,$4C,$CC,$4E,$5B,$4F,$ED,$51,$83,$53,$1C,$54,$B9,$56,$59,$57,_
    $FD,$59,$A4,$5B,$4D,$5C,$FA,$5E,$A9,$60,$5B,$62,$0F,$63,$C6,$65,$7F,_
    $67,$3A,$68,$F7,$6A,$B5,$6C,$76,$6E,$37,$6F,$FB,$71,$BF,$73,$85,$75,_
    $4B,$77,$13,$78,$DB,$7A,$A4,$7C,$6D,$7E,$36,$7F,$FF,

    Thank you
    Doug
    Doug McDonald
    KD5NWK
    www.redforksoftware.com

  • #2
    Here's a thought,

    to mix the freq's is freq1 + freq2 per table index

    with the DAC at a 2.5v reference I'd adjust the sum of the freq's from full scale to produce the required RMS voltage (amplitude) above and below the 2.5v

    It can't be that simple is it?

    I still would like to know how to build the tables in PB

    BTW the above table is 16bit broken up into 8 bit chunks

    Just thinking out loud

    Doug
    Last edited by Doug McDonald; 31 Dec 2007, 07:00 PM. Reason: error in post
    Doug McDonald
    KD5NWK
    www.redforksoftware.com

    Comment


    • #3
      For a 14 bit DAC, I'd think you'd want limit the table to 14 bit values, even if they are stored as 2 bytes (16 bits).

      center (sine zero) = 10000000000000 binary, 2000 hex
      positive peak = 11111111111111 binary, 3FFF hex
      negative peak = 00000000000001 binary, 0001 hex
      (I originally put negative peak at 0000, but that wouldn't be symetrical.
      3FFF - 2000 = 1FFF; 2000 - 1FFF = 1, not zero)

      Amplitude domain for 2 frequencies:
      current sine for freq1 plus current sine for freq2 divided by 2
      Why divide by 2? Because when both positive peaks happen at the same time the sum is 15 bits.

      3FFF + 3FFF = 7FFE (111111111111110)
      then shift right 1 bit (same as divide by 2)

      Or, build the table to 13 bits in the first place, then no need to divide.

      Time domain for 2 frequencies:
      I've got thoughts going around in my head, but I've only had one cup of coffee so far this morning and can't organize them. Maybe later.

      Cheers,
      Last edited by Dale Yarker; 1 Jan 2008, 12:12 AM. Reason: correct negative peak
      Dale

      Comment


      • #4
        Computing Sine Table Values

        Doug;
        Computing the sine wave table values is pretty easy. You can use something like:

        TWOPI = 710/113
        FOR I = 0 TO (NrSamples -1)
        Sample(I) = INT(8191* SIN(TWOPI/NrSamples * I)) + 8191 (use for offset binary DAC)
        Sample(I) = INT(8191 * SIN(TWOPI/NrSamples * I)) (use for 2's complement DAC)
        NEXT I

        I know with a little bit of logic you could use the full span (0 to 16383) but I have found that it isn't worth the headaches to pick up one or two more counts.

        Just use the lower 14 bits of the result for either equation and you will be fine.

        Where your request gets tricky is how you supply these values to the DAC. Obviously, the rate the samples are sent will set the frequency of the DAC output. One of the simplest ways to approach the problem is to use a variation of DDS (direct digital synthesis) in the PIC code.

        The theory is something like this:
        You store the samples for one cycle of the sine wave in the PIC memory. It is easiest if you use some power of two for the number of samples. Let's assume that you use 128 samples for the sine wave.

        You then set up an accumulator (say 16 bits) whose upper byte will address the sinewave table. You also set up a 16 bit phase increment register that will hold a number that will ultimately set the output frequency.

        When you want to output a frequency, you clear the accumulator and add the value in the phase increment register to the accumulator. You then use the upper 8 bits of the accumulator to index into the sine table and send that value out to the DAC. You then loop back and repeat the addition to the accumulator and output to the DAC. If the value in the phase increment register is small, it will take longer to read the sine table out and thus you will obtain a lower frequency. Larger values will yield higher frequencies. Obviously, there will be an upper limit to the frequency and the sine wave will look worse as you start to use more bits in the upper byte of the phase increment register.

        The highest frequency will be set by how fast the PIC can loop through the "accumulate and send" code. If you are using a parallel DAC it should be pretty fast, but if you are using a SPI or I2C DAC, you may not be able to loop fast enough to get the frequency you want especially if you are bit-banging the communications. I don't know how you are using the output, but you should have at least 16 samples per cycle to keep things looking reasonable. I have seen as few as 8 samples per cycle, but the waveforms start looking pretty chunky.

        Also, you mentioned "mixing" frequencies. To those of us who have worked in the radio world this means heterodying two frequencies to obtain new frequencies. I assume you mean that you wish to combine or superimpose two frequencies similar to touch-tone codes.

        If this is what you want to do, then compute the sine wave samples to 13 bit resolution and run two accumulators in parallel. After they have both been updated, read one sample for each accumulator output, add them and then send that to the DAC.

        I have used this technique in microcontrollers and FPGAs and it works pretty well.
        Last edited by Jeff Burrell; 1 Jan 2008, 12:49 AM. Reason: corrected FOR loop upper limit
        Understanding is a three edged sword.
        (Old Vorlon saying)

        Comment


        • #5
          I counted the number of entries in sample while Jeff was posting and got 89. (I think there should be 90 but doesn't matter.) It would easier in PIC coding if the number of table entries is a power of 2, like 64, 128 or 256. 16384 is max because there is no point in having more values in table than steps in the DAC. The practical max table size will be limited by memory size of the PIC.

          (I think I just reworded part of what Jeff said.)

          For a 128 entry table, each step would be 90 / 127 = 0.708661 degrees.

          I do disagree with Jeff on storing a full cycle. You were right with entries for 0 to 90 degrees, because sine of 91 equals sine of 89, etc etc., sine of 181 equals 0 minus sine of one, etc etc. Table only needs to cover one quadrant. (Darn, that messes up what I said above about max table size. Change 16384 to 8192.)

          For a different approach to sine waves from a PIC, start here:
          http://www.tinaja.com/glib/msintro1.pdf
          "Magic Sine Waves" are generated by PWM instead of DAC.

          Cheers,
          Last edited by Dale Yarker; 1 Jan 2008, 01:53 AM.
          Dale

          Comment


          • #6
            Symmetries In The Sine Table

            Dale is correct about using symmetries in the sine function to reduce storage space. The question is whether the extra code is smaller/faster than using a full table. Sometimes it's better to sacrifice ROM for speed or vice-versa. Most of my programming now is in VHDL on FPGAs, so I can play more tricks and make more trade-offs than is usually possible with a microcontroller.
            Understanding is a three edged sword.
            (Old Vorlon saying)

            Comment


            • #7
              Thank you all Very much. great info and insight
              Doug McDonald
              KD5NWK
              www.redforksoftware.com

              Comment

              Working...
              X