# Direct Digital Frequency Synthesis (DDFS)

#### Basic Idea:

I want to show you what an DDFS is, how it works and how to implement it on an FPGA.
Imagine you want to create a Sinusoid at a DAC. The DAC is accepting a digital value and will convert it to a analogue value. If there are multiple values in a periodic manner we say its a signal, a signal is a periodic flow of values.
If we want to create a sinusoid signal we therefore need to send the discrete values of the sinusoid to the ADC.

Lets say our DAC is working with 10MHz, which means that it will generate a analogue value every 0,1µs (1/10MHz).

We want to create a sinus with a frequency of 1,25MHz. Which means that we will have to send 8 values per cycle to the DAC in order to meet the 10MHz of the DAC.
We now need to get those 8 values. We need to split one cycle of our desired signal (sinus) into 10 equally distributed values.
-1<x<8 which will give us

0 0
1 0,707106781
2 1
3 0,707106781
4 0
5 -0,707106781
6 -1
7 -0,707106781

If you plot those values you already can imagine what it is: All we do need to do know is to cycle through the values and send the corresponding value every 0,1µs

Implementation
Because the cycling etc would be very expensive on a microcontroller this structure is usually build in hardware/FPGA.

Our list with the values is stored in a Look up table (LUT) which is SRAM on a FPGA and ROM on an ASIC.
We do know how many entries we need, 8. How many bits do we need to address that? so log(8)/log(2)=3 bit.

We end up with something like this: So if the adress is 010b (2d) the output will be 1, 101b (5d) will give -0,707106781.
please note that there is a FlipFlop to make sure our output is synchronous with the clock. The clock in our case would have to be set to 10MHz to by synchronous with the DAC.

Next thing we need to do is to make the address cycle from 0 to 7.
How to do it?
Just thing of programming in c++ (for example)

int count=0;
while (true)
{
count=++count%8;
cout<<"value: "<<count<<endl;
}

this will give us:

value: 0
value: 1
value: 2
value: 3
value: 4
value: 5
value: 6
value: 7
value: 0
value: 1
value: 2
value: 3
...

and it will do so forever. This is the signal we do need. It will address our entries in the LUT one after another and send the discrete sinus to the DAC.
If you plot this signal you well see that: this is a sawtooth signal.

To generate this signal we need a FlipFlop which is triggered by a clock and on every cycle its value is incremented by 1. It will overflow if the maximum value of the FlipFlop is reached and start from 0 again.

That is the basic structure of an DDFS.
The problem with it is that everything is fixed there, the frequency, the amplitude and the phase shift.

#### Frequency control

The trick on how to change the frequency is to change the frequency of the address signal (sawtooth).
Remember that inside the address generator we used the previous values and incremented it by 1 every cycle. But what happens if we increment it by 2 every cycle? The frequency of the sawtooth signal will double and therefore also the frequency of the sinus signal. Please note that some of the values within the LUT are skipped now! Also note that you still need to take the Nyquist Frequency within consideration. As the DAC frequency and the LUT size are limited you can not infinitely increase the frequency.
The value we increment the counter with every cycle is called the "Frequency Tuning Word" FTW.

The method above will allow to set frequencies as multiple of the base frequency (FTW=1) but nothing in between. Fore some applications this is fine, for others its not.
You can solve this issue by introducing fractional bits.
Lets assume we stay with our 3 bit LUT and we want to introduce 2 fractional bits. We can express this using the Q-Format, in Q format this constellation would be Q3.2.
We now do need a 5bit FlipFlop to generate our sawtooth. The trick is that just the non fractional 3 bits of the FlipFlop are used to address the LUT. Its important that our FTW now need to be in the Q3.2 format too!
Imagine it would be our old 1, so in 5 bits 00001. Then we would need 4 cycles in order to reach the next entry in the LUT. Therefore the output frequency would be be 1/4th of the base frequency.

A few words about fractional bits in a fixed point arithmetic.
in our example we assume that we have 2 fractional bits.
To make a long story short, this is the representation of all 5 bits withing Q3.2, the bits are separated by a semicolon
2^3;2^2,2^1; . 2^-1;2^-2; which is 4;2;1; . 0,5;0,25

lets try it:
01001 is 2+0,25=2,25

this is giving us the possibility to adjust our frequency much more precise!
But there is also a downside. Imagine we half the base frequency, as the DAC is of course still working with its own frequency but is just getting a new value every 2 cycles the signal is distorted: #### Amplitude control

In the previous consideration we expected that the LUT will hold values that are equally distributed within the boundaries of the underling bit-width. This bit width is not necessary equal with the bit width of the DAC. Therefore, by placing a multiplicatior we can set the amplitude of the signal. #### Offset

We can apply an offset by adding a value before we send the signal to the DAC #### Phase Shift #### Using qudrature information

Currently we are storing the whole sinus in the LUT. This isn't necessary. We also can store just the values from 0 to 1/2pi (or 0° to 90°). And the first 2 bit can be used as quadrature information. Therefore the LUT size can be reduced by 2 bits. Or, we can use the old LUT size to reach a much higher precision, in this case we need 2 more bits for our sawtooth generator.

Our 3 bit LUT can hold 8 values, by interpreting the first 2 bits of the phase word (the signal from the sawtooth to the LUT) we can determine in which quadrature we are.

00 01 10 11 Now we can compare the old output with the new:
old new ### Formulas:

Phase resolution:
The Phase resolution will tell us the step-size between 2 values in the LUT. If we store a whole Sinusoid function within an N-bit LUT, the Phase resolution is
In degree: In radiant: If we use the quadrature information and store just 1/4 of the cycle the precision is 4 times higher:
In degree: In radiant: Frequency Tuning Word (FTW)
The FTW is the multiplier for the sawtooth signal generator which will determine its frequency and therefore the frequency of the output. The output frequency is: where N is the number of phasepoints.
If the whole cycle is stored in the LUT, N is the LUT Size.
If we use quadrature information N is the LUT size*4!

So if we want to have the FTW for a desired target frequency its: The problem is that we can just meet target frequencies which are a multiple of the clock frequency. Therefore (if we do not use fractional bits) we have to round to the closest integer.
Its obvious that we will have an frequency error at this point if the calculated FTW is not an integer

Therefore we first need to calculate the actual output frequency using for an absolute error we simply subtract the actual output frequency from our target frequency and get the absolute error
for an relative error we need the ratio between the desired and the actual frequency #### Using fractional Bits

As mentioned above we can use fractional bits to reach a higher precision. The only thing we need to do is to calculate the FTW with fractional bits.

Example:
Assume a Q2.4 FTW. So we have 2 bits for the integer and 4 bits for the fractional part. Note that the fractional bits are not used to adress the LUT.
We want a FTW of 1.44 our bitmask is xx.xxx Lets write the whole thing down again vertically

x 2^2 (2)
x 2^1 (1)
.
x 2^-1 (0,5)
x 2^-2 (0,25)
x 2^-3 (0,125)
x 2^-4 (0,0625)

we convert the integer and fractional part separately. For the integer part in this case its simply 01.xxx.
Now we need to convert the fractional part into binary representation. We compare the fractional part with our fractional bits
0,44 > (x2^-1 <0,5>) no, so this bit is 0
0,44 > (x2^-2 <0,25>) yes, so this bit is 1 we now need to subtract the 0,25 from 0,44 which will give 0,19
0,19 > (x2^-3 <0,125>) yes, so this bit is 1 subtract it from 0,19 will give 0,065
0,065> (x2^-4 <0,0625>) yes, so 0,065-0,0625= 0,0025 (our rest)

so we have 01.0111 for the FTW, but if you convert it back (0,25+0,125+0,0625) you will see that this number is 1,4375 which is closer to our desired frequency but still does not meet it exactly.
The frequency error calculation than can be used to determine the error.

### DDFS Verilog Implementation

The code below shows a basic DDFS implementation