DDS frequency formula and sin generating
Moderator: frank
DDS frequency formula and sin generating
Hi, I've found ramp dds generator in this thread:
http://www.spinsemi.com/forum/viewtopic ... =764#p4091
It works just fine, but I have two questions:
- What is the formula for generated wave frequency?
- How to modify this code to generate sinus? (I'm looking for something shorter than converting ramp to triangle and then triangle to sin)
I'll be grateful for any help
http://www.spinsemi.com/forum/viewtopic ... =764#p4091
It works just fine, but I have two questions:
- What is the formula for generated wave frequency?
- How to modify this code to generate sinus? (I'm looking for something shorter than converting ramp to triangle and then triangle to sin)
I'll be grateful for any help
Re: DDS frequency formula and sin generating
See this thread, the code in it show how the SIN algo works: http://www.spinsemi.com/forum/viewtopic.php?t=28
Frank Thomson
Experimental Noize
Experimental Noize
Re: DDS frequency formula and sin generating
Thank you, Frank
So far, I think I found the formulas for this ramp and sin DDS.
For ramp:
T[s] = [4194303/((1536 * potX) + 8 )]/R
And for sin:
T[s] = (2 * pi) / (potX * R)
potX - pot input, range 0 to 1
R - sample rate
But I found an issue too. Look at my example:
It should generate sinus of ~469ms period, but it generates ~540ms. But, if I change the coefficient multiplying to something like this, it works correct:
What's wrong?
So far, I think I found the formulas for this ramp and sin DDS.
For ramp:
T[s] = [4194303/((1536 * potX) + 8 )]/R
And for sin:
T[s] = (2 * pi) / (potX * R)
potX - pot input, range 0 to 1
R - sample rate
But I found an issue too. Look at my example:
Code: Select all
; Normal program starts here
rdax c, 1 ;Read COS
sof 0.00028095, 0
rdax s, 1 ;SIN + freq*COS
wrax s, -1 ;Save SIN and mult by -1
sof 0.00028095, 0
rdax c, 1 ;COS - freq*SIN
wrax c, 1 ;Save COS
Code: Select all
rdax c, 1 ;Read COS
sof 0.28095, 0
sof 0.1, 0
sof 0.1, 0
sof 0.1, 0
rdax s, 1 ;SIN + freq*COS
Re: DDS frequency formula and sin generating
Maybe different sample rates?
Frank Thomson
Experimental Noize
Experimental Noize
Re: DDS frequency formula and sin generating
No, the sample rate is constant all the time. You didn't understand my problem
It looks like this:
isn't equal to this:
Maybe some problem with approximation?
It looks like this:
Code: Select all
sof 0.00028095, 0
Code: Select all
sof 0.28095, 0
sof 0.1, 0
sof 0.1, 0
sof 0.1, 0
Re: DDS frequency formula and sin generating
SOF takes a 16-bit C value, 0.00028095 is 1,000 times smaller (about 11 bits) than 0.28095 so you lose 11-bits of resolution.
Just look at the assembled code:
0000 0004000D :sof 0.00028095, 0
0001 11FB000D :sof 0.28095, 0
Just look at the assembled code:
0000 0004000D :sof 0.00028095, 0
0001 11FB000D :sof 0.28095, 0
Frank Thomson
Experimental Noize
Experimental Noize
Re: DDS frequency formula and sin generating
Yup, but in both cases c is multiplied by 0.00028095; in the first case immediately by factor of 0.00028095 and in the second, first by 0.28095 and then 3 times by 0,1. Result should be the same, accuracy is not :/
Does it mean when I multiply c directly by very small value (i.e. 0.00028095) it's worse than first by 0.28095 and then 3x0.1?
Does it mean when I multiply c directly by very small value (i.e. 0.00028095) it's worse than first by 0.28095 and then 3x0.1?
Re: DDS frequency formula and sin generating
Nope, they should not. You are not considering bit width, as I said the coefficient in SOF is 16-bits.
Frank Thomson
Experimental Noize
Experimental Noize
Re: DDS frequency formula and sin generating
I meant, mathematically they should be the same.
Anyway, how can I control this LFO at low frequencies without losing accuracy?
Anyway, how can I control this LFO at low frequencies without losing accuracy?
Re: DDS frequency formula and sin generating
You will have to do the LFO in code, there are examples in the forum.
Frank Thomson
Experimental Noize
Experimental Noize
Re: DDS frequency formula and sin generating
Frank, are you drunk? This whole thread is about doing LFO in code (I don't want to use hardware LFOs). I was asking how to control this code LFO at low frequencies (low value of frequency variable) without losing accuracy. I'd be so grateful for your suggestions.
Re: DDS frequency formula and sin generating
That was a reply intended for another person who is using the h/w LFOs.
To answer you, don't use SOF. SOF only takes a S1.14 format 16-bit value and you are trying to put in too small a number.
So your choices are to use a larger value and shift the result down like you are with the SOF 0.1, 0 instructions or don't use SOF and load the value in a different manner.
Just because you write 0.00028095 in an SOF instruction does not mean that is the final value encoded into the instruction, what will be encoded will be the truncated value that fits the bit width of the instruction.
To answer you, don't use SOF. SOF only takes a S1.14 format 16-bit value and you are trying to put in too small a number.
So your choices are to use a larger value and shift the result down like you are with the SOF 0.1, 0 instructions or don't use SOF and load the value in a different manner.
Just because you write 0.00028095 in an SOF instruction does not mean that is the final value encoded into the instruction, what will be encoded will be the truncated value that fits the bit width of the instruction.
Frank Thomson
Experimental Noize
Experimental Noize
Re: DDS frequency formula and sin generating
Okay, just kidding
I tried something another. Using OR and mulx, but still doesn't work.
Code: Select all
equ c reg0
equ s reg1
equ div reg2
; Initialize SIN / COS
skp run, 4
clr
or 0x7FFFFF
wrax s, 0 ; initialize SIN to 1
wrax c, 0 ; initialize COS to 0
; Do control
clr
or 0x6E ; 0x7FFFFF * 0.00001316953
wrax div, 0 ; 0.00001316953 = 10s period
; Normal program starts here
rdax c, 1 ; Read COS
mulx div ; Scale COS
rdax s, 1 ; SIN + freq * COS
wrax s, -1 ; Save SIN and mult by -1
mulx div ; Scale -SIN
rdax c, 1 ; COS - freq*SIN
wrax c, 1 ; Save COS
wrax dacr, 1 ; write output
wrax dacl, 0
Re: DDS frequency formula and sin generating
Probably a bit width issue, MULX is a 16-bit coefficient so losing the lower 8-bits of the value used for the coefficient.
Frank Thomson
Experimental Noize
Experimental Noize
Re: DDS frequency formula and sin generating
Ok, I finally made it working as I want to
One more thing; how can I move starting value of generated signal, lets say, about 90 degrees left or right?
One more thing; how can I move starting value of generated signal, lets say, about 90 degrees left or right?