# Output a sine wave from samples-limits?

Using a micro-processor of 48MHz, I am trying to output a sine wave from the DAC (digital to analog ), and I am not sure if my Nyquist and calculations are good.

1. I have to output sine in frequencies range of 1KHz to 3MHz

2. I have created an array of a sine samples that has 1000 points(for 1 period )

3. I am running a timer between samples, and the frequency calculation

float timerFrequency=sineFrequency*1000; //(timerT*1000=finalSineT)

But then I was thinking that for 3MHz sine wave, I will need a timer frequency of 3GHz with this approach(!). (3Mhz signal * 1000 sine samples)

Something here is very wrong with everything, because if I have a processor that is 48MHz, I can probably output a 3MHz , isn't it ?

1. Whats wrong with my approach, and what are my limits with these assumptions ?

2. Can I reduce the sine samples array to lets say 100 ? what would it do to the Nyqvist at 3MHz ?

3. How many samples should my sine array needs? (the min), and my timer frequency?

• There are a few pieces of information missing: what SNR do you need? How many (effective) bits is the DAC? Are you running bare metal or is there an (RT)OS involved?
– Peter K.
Apr 26, 2016 at 18:14
• Are you sure your micro is up to a 3MHz sine output? This project uses a 20MHz PIC and only claims up to 60kHz clean sine waves. FWIW, it uses a 256-entry lookup table
– Peter K.
Apr 26, 2016 at 18:57
• I repeat my earlier questions: What SNR do you need? Are you running bare metal or is there an (RT)OS involved?
– Peter K.
Apr 26, 2016 at 19:04
• @Curnelious While you're at it, please also include in your question the sampling rate of your DAC. If it is embedded in a small microcontroller, then it is unlikely to be able to produce a 3 MHz signal.
– MBaz
Apr 26, 2016 at 20:00
• @Fat32 Fire away!
– Peter K.
Apr 26, 2016 at 22:33

Eventhough your problem is quite a practical one, based on a particular chip to produce a sine wave that requres a solid understanding of its blocks, I would, nevertheless, like to provide you a general framework for producing a continuous time sine wave from a set of discrete time samples, from which you can deduce the details of implementation for your particular architecture.

Now for this particular problem of producing a sine wave, the fundamental factor which will determine your analog output capability from a DAC is the quality of the analog "Anti-Imaging" filter (also known as interpolation or reconstruction filter)

When that filter is an ideal one (or a close enough approximation) it can reject all the distorting image frequency bands that exist due to the DAC output being an ideal impulse based sampled representation of the signal to be reconstructed. (A sine wave in this case).

In such a case (of close to ideal) of operating conditions, you can generate a perfect sine wave at any desired frequency from just "2" samples of it, by merely adjusting the output DAC conversion frequency. The filter will interpolate all the missing wave in the ideal case.

In other words, your sine wave samples will be this $$x[n] = \cos(\pi n)$$ which has only two samples which are +1 and -1 per period, representing a critically sampled sinusoidal. Your DAC will output +A and -A after scaling for each consequitive conversion period (which is limited by either your chip speed or your DAC output settling time) and that perfect reconstruction filter will convert those apexes into a smooth sine wave.

By adjusting the DAC output conversion period you can control the produced sinusoidal wave frequency simply as: $$f_{sine} = \frac{1}{2 T_{conversion}}$$

Now things are not ideal (especially DAC output involves a zero order hold block that should be taken into account in the design of the reconstruction filter) and therefore sadly just two samples per period will not be enough. In such a case, to be able to use a less perfect and easier to design analog output reconstruction filter you will provide more samples of the sine wave per period. But how many samples? It depends on your filter quality. The better the filter the less the number of samples.

The frequency is however is still adjusted the same way: In general let: $$x[n] = \cos(\omega_k n)$$ be your one period of sampled signal (look up table samples) where $w_k$ (being between $0$ and $\pi$) is the discrete time frequency of the generic set of samples. And let your DAC output period be $T_s$ then the produced output sine wave will have the frequency of: $$f_{sine} = \frac {\omega_k}{2 \pi T_s}$$

Again by adjusting $T_s$ or $\omega_k$ you can control this frequency.

• Thank you very much, but i knew all of that and its quite obvious, these are the fundamentals of smapling theory. The interesting stuff is calculating what happens in each case of N samples of a signal. How it looks for every N in the f domain. However you provided a great answer and i accept it even that its not a clear sense of what will happen. Apr 27, 2016 at 6:41
• In your question you talk about using 1000 samples per peiod. That's too much! i.e. N=1000 samples gives you $w_k = 2\pi / 1000 = \pi / 500$ a very small discrete time frequency, this leads for example for a required output $f_{sine} = 3M Hz$ => $T_{DAC} = 1/ (3G) s$ => a 3 Ghz of operation DAC frequency. Just as you complain. This high frequency results from using very low $w_k$, if you instead use a small number of samples such as N=10, you would need a 30 Mhz of DAC frequency Apr 27, 2016 at 11:15
• Thats true but there is no such animal 30m dac :) Apr 27, 2016 at 11:18
• What's your DAC maximum frequency?, 30Mhz an animal?? Apr 27, 2016 at 11:19
• You don't need large number of bits... You may just use a switch with on/off to produce a very high frequency oscillation and filter out a sine wave instead? as a completely different approach ? Apr 27, 2016 at 11:23