ELM Home Page

December 19, 2018

miniDDS and Something about Interpolation


There are many simple function generator projects with software implemented DDS on the web. Strangely some different projects are named the same name, miniDDS. I think it is because the miniDDS is actually not a specific project but generic name of this sort of simple function generator projects. I also have opened some software DDS projects, Musical Box and DDS Function Generator, in this site.

Lately I found a cheap ARM Cortex-M4 microcontroller, STM32F303K8T6 from ST which integrats three 12-bit D-A converters on chip, at Akizuki in Akihabara. The multi-channel DACs with high conversion rate, 1 Msa/sec, will able to be used for function generator project with decent functionality, so that I made a miniDDS library for the STM32. And I also studied about improvement of accuracy of synthesized waveform by interpolation of LUT data.

miniDDS for STM32F3

Fig.1 Software DDS on the STM32F3

The DDS function is implemented to the STM32F303K8 with the configuration shown in Figure 1. The interval timer generates a sample timing which drives the DDS system. The D-A conversion is triggered by the timer output and it gives jitter-free output. The DAC can generate an interrupt or DMA request on the every conversion to obtain a next data to output. Because the DAC conversion rate upto 1 Msa/sec (72 CPU clocks a transfer) is too fast for interrupt process, DAC data is transferred with DMA controller in this project. The DMA controller fetches a sample value from the buffer memory and write it to the DAC register every DMA request. The DMA controller is configured circular mode to re-start data transfer from top of buffer when it reaches end of the buffer. It also generates interrupt on the half transfer and the last transfer. The DMA interrupt service routine synthesizes a wave form and fills the half of DMA buffer each time. Thus DMA contrller is used as a data streaming FIFO buffer for the wave form data to the DAC.

The processing load ratio of DDS function, how much CPU time is occupied by DMA interrupt service, was only 45% at 72 MHz of CPU clock, 1 Msa/sec of sampling rate and single channel configuration. Dual channel configuration consumes 85% of CPU power and the sampling rate needs to be lowered to 720ksa/sec at triple channel configuration. It is not bad considering that 1 Msa/sec is the maximum DAC sampling rate. Because most software DDS projects are implememted on the 8-bit microcontroller with poor processing power, the wave form is synthesized without any LUT data interpolation (fraction part of phase value is simply rounded-off). In this project, the DDS is implemented on the powerful 32-bit microcontroller, so that it could get smooth wave form by utilizeing interpolation even with short (coarse sampling) wave table. For more studies on LUT interpolation, please refer the following sections.

Table Look-up and Interpolation Error

The DDS synthesizes a wave form by calcurating the discrete-time value of sampling point and outputting it in real-time. For instance, sin() function would need to be executed every sampling point to generate a sine wave. However this is not a practical way for actual imprementations considering the processing resources. Instead, a pre-calcurated sine wave, also any wave form if needed, is held on the LUT and the value of sampling point is obtained by referring the LUT. In this method, the output frequency fo is determined in fo = fs / N * m, where fs is the sampling frequency and N is the LUT length. Because multiplyer m can be an integer number, fo can be integer multiple of fs / N. To generate the wave form at arbitrary frequency, it needs to change fs or give a a real number to the m. The actual implementations adopt the latter one, however, it will need to obtain the value of sampling point not on the LUT entry but between two adjacent LUT enries, so that any interpolation will be needed for the table look-up.

Note that this article studies on reconstructing continuous-time wave form from discrete-time wave data on the LUT. About any wave form after D-A conversion is out of scope here.

Reconstructing Continuous-time Wave Form

Fig.2 Interpolated Wave Forms

There are some methods for the LUT interpolation and which method is used is depends on the requirements and the processing resource. Figure 2 shows the continuous-time sine wave reconstructed in some interpolation methods.

None: This is not that without interpolation but it only gives a value of nearest LUT sample. It also called ZOH and it does not want any calcuration to obtain the sample value. The interpolated wave form looks like a wave form at DAC output.

Geometric: Spline interpolation is the typical method of geometric interpolation. It interpolates the sampling points geometrically. There are many mathematical models and FOH, also called linear interpolation, is well used for reconstructing the wave form. The FOH approximates the wave form from two LUT entries beside the sampling point and wants two MAC operations a sapmle.

Filtering: FIR filter is usually used for digital signal processing. Any filter characteristics, even ZOH and FOH, can be configured by choosing the filter coefficients. sin(x)/x (usually its variant) is well used for the wave form interpolation. The FIR filter wants many MAC operations proportional to the filter length (number of taps).

Interpolation Error

Fig.3 Interpolation Error

Interpolation error appears as a noise on the reconstructed wave form. Figure 3 shows the error components at ZOH, FOH and sin(x)/x. The ZOH interpolation produces the error at steep line part and the FOH interpolation wihch approximates between LUT data in linear produces the error at curved line part. sin(x)/x reconstructs the wave form accurately and the error could not read from the chart at all.

Note that the LUT length in 16 entries in this explanation is only an instance to illustrate how the interpolation error appears. In actual implementation, an LUT in length of 256 entries or longer is used.

LUT Length and Error Level

Fig.4 LUT Length and Error Level

You will able to expect that the interpolation error gets smaller as number of LUT entries (how much fine to sample the original wave form) increases. Figure 4 shows the LUT length - noise level chart at some interpolation methods.

The noise level lowers 6 dB when the LUT length doubles at ZOH interpolation. At FOH interpolation, it is affected by the LUT length much more than ZOH interpolation and it lowers 12 dB when the LUT length doubles. In contrast, sin(x)/x interpolation is not much affected by LUT length, but, insted, the accuracy depends on the filter coefficients. In this case, Lanczos filter (a=7), a variant of sin(x)/x, is used for the filter coefficients.

Non-sinusoidal Wave Forms

Fig.5 Reconstructing a Square Wave

General purpose function generator needs to generate not only sine wave but non-sinusoidal wave forms, such as square wave, triangle wave, sawtooth wave and any pulse wave. Non-sinusoidal wave form is constructed from some harmonics. Figure 5 shows how the wave form affects interpolation. This is a extreme but typical example of pulse wave, square wave. sin(x)/x interpolation should be accurate from the description above but it produces strange ringings around the trangents. In contrast, the wave form reconstructed by inaccurate ZOH interpolation looks like proper as a square wave. What happened?

Because the square wave reconstructed by ZOH interpolation is actually unreal wave form that not in the LUT data and it only appears like a square wave due to the interpolation error. From the Nyquist sampling criterion, any frequency comopnent above F0 * N / 2 never exist in the LUT data in length of N points. Where F0 is the fundamental frequency in period of LUT length. Also the frequency domain of the discrete time signal is in discrete. Ringings around the trangents that appear when approximates a square wave or any wave form with large jumps with limited number of frequency components is an effect known as Gibbs phenomenon. Therefore sin(x)/x interpolation only reconstructs it accurately. However, this effect is not acceptable for some applications which place importance on the wave form rather than physical characteristics. It does not that mean sin(x)/x interpolation is useless but the physical accuracy is not that always important for every application. Any filter constant with decent result without Gibbs phenomenon, such as FOH and Gaussian, may be suitable for the generic function generators.

Sign