DDS frequency formula and sin generating

Algorithm development and general DSP issues

Moderator: frank

gary00k
Posts: 26
Joined: Sun Jun 20, 2021 1:50 am

DDS frequency formula and sin generating

Post by gary00k »

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 :)
frank
Posts: 1244
Joined: Wed Oct 19, 2005 12:26 pm
Contact:

Re: DDS frequency formula and sin generating

Post by frank »

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
gary00k
Posts: 26
Joined: Sun Jun 20, 2021 1:50 am

Re: DDS frequency formula and sin generating

Post by gary00k »

Thank you, Frank :D
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
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:

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 
What's wrong?
frank
Posts: 1244
Joined: Wed Oct 19, 2005 12:26 pm
Contact:

Re: DDS frequency formula and sin generating

Post by frank »

Maybe different sample rates?
Frank Thomson
Experimental Noize
gary00k
Posts: 26
Joined: Sun Jun 20, 2021 1:50 am

Re: DDS frequency formula and sin generating

Post by gary00k »

No, the sample rate is constant all the time. You didn't understand my problem :)
It looks like this:

Code: Select all

sof	0.00028095, 0
isn't equal to this:

Code: Select all

sof	0.28095, 0
sof	0.1, 0
sof	0.1, 0
sof	0.1, 0
Maybe some problem with approximation?
frank
Posts: 1244
Joined: Wed Oct 19, 2005 12:26 pm
Contact:

Re: DDS frequency formula and sin generating

Post by frank »

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
Frank Thomson
Experimental Noize
gary00k
Posts: 26
Joined: Sun Jun 20, 2021 1:50 am

Re: DDS frequency formula and sin generating

Post by gary00k »

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?
frank
Posts: 1244
Joined: Wed Oct 19, 2005 12:26 pm
Contact:

Re: DDS frequency formula and sin generating

Post by frank »

gary00k wrote: Sun Oct 03, 2021 10:46 am Result should be the same
Nope, they should not. You are not considering bit width, as I said the coefficient in SOF is 16-bits.
Frank Thomson
Experimental Noize
gary00k
Posts: 26
Joined: Sun Jun 20, 2021 1:50 am

Re: DDS frequency formula and sin generating

Post by gary00k »

I meant, mathematically they should be the same.
Anyway, how can I control this LFO at low frequencies without losing accuracy?
frank
Posts: 1244
Joined: Wed Oct 19, 2005 12:26 pm
Contact:

Re: DDS frequency formula and sin generating

Post by frank »

You will have to do the LFO in code, there are examples in the forum.
Frank Thomson
Experimental Noize
gary00k
Posts: 26
Joined: Sun Jun 20, 2021 1:50 am

Re: DDS frequency formula and sin generating

Post by gary00k »

Frank, are you drunk? :D 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.
frank
Posts: 1244
Joined: Wed Oct 19, 2005 12:26 pm
Contact:

Re: DDS frequency formula and sin generating

Post by frank »

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.
Frank Thomson
Experimental Noize
gary00k
Posts: 26
Joined: Sun Jun 20, 2021 1:50 am

Re: DDS frequency formula and sin generating

Post by gary00k »

frank wrote: Mon Oct 04, 2021 9:01 am That was a reply intended for another person who is using the h/w LFOs.
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
I tried to do it without using SOF at all, but all I got is 0 at the output.
frank
Posts: 1244
Joined: Wed Oct 19, 2005 12:26 pm
Contact:

Re: DDS frequency formula and sin generating

Post by frank »

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
gary00k
Posts: 26
Joined: Sun Jun 20, 2021 1:50 am

Re: DDS frequency formula and sin generating

Post by gary00k »

Ok, I finally made it working as I want to :D
One more thing; how can I move starting value of generated signal, lets say, about 90 degrees left or right?
Post Reply