Google

main
dspblog
cyclic_signals
FFT_interpolation
FFT_interpolation_how_does_it_work
FFT_smoothness_cyclic
discrete_time_reconstruction
discrete_time_reconstruction2
Nyquist_on_the_edge
FFT_delay_special_case
Fourier_reconstruction
pulse_and_Nyquist
FFT_complexError
matlabOctave
matlab_upsampling
matlab_downsampling
FFT_delay
FFT_filter_example
FFT_interpolation_example
FFT_bin_frequencies
fit_signal
FFT_peaksearch_audio_example
matlab_binary_readwrite
Octavesvg
C
FFTW_example
looprecord
SNR
SNR3
SNR_example_96kAudio
SNR_FFT_correlation_example
lua
luagpib
luasplit
luadump
mnoofltk
wxLuaDll
wxLua_loadAsDll
wxLua_HelloWorld
wxLua_simpleButton
wxLua_resourceManagement
wxLua_XMLparser
DSP
IQ_LO
IQ_LO_2
optimum_receiver
DSP_basics
sampleRateChange_terms
dirac_pulse
freqresp_s
zfilter_example
freqresp_z
freqresp_z_sign
misc
zero_forcing_equalizer_example
nonminphase_inverse
periodic_spectrum
lagrange_multipliers
Entropy
RC_chopper
TRex450_setup
EP100
EP100SE
EP100Gremlins
essential_spares
tail_rotor
motoradjustment
blade_balancing
blade_repair
GAUI_SAE12A
Walkera43


valid html (click to verify)



prevupnextdisable ads

Digital quadrature oscillator, part 2

Introduction

The previous page explained, how to generate a quadrature LO signal.
Calculating sine and cosine is expensive, and it may be better to use a table (wavetable).
This is straightforward, as long as the length of an LO cycle is a full number of clock cycles.
Frequency translation by \frac{f_c}{2}, \frac{f_c}{3}, \frac{f_c}{4}, ... \frac{f_c}{k} is possible, where f_c is the clock frequency.

Now how about other, arbitrary frequencies?

Interpolation allows to use the wavetable approach for a conversion frequency with an arbitrary period length.
The example uses zero-order interpolation (nearest neighbor). It is simple to implement, but suffers from aliasing at higher frequencies.

Alias products scale proportionally with the LO frequency. They can be reduced by using higher wavetable resolution, or better interpolation methods.

Example code

/* ***************************************************
 * Example for a quadrature oscillator using a wavetable
 * It can be used in situations, where evaluating sin() and
 * cos() is too expensive.
 * It uses nearest-neighbor interpolation, which will cause
 * severe aliasing at higher frequencies.
 * 
 * Markus Nentwig, 2007
 * This program is in the public domain
 */
#include <stdio.h>
#include <math.h>
#include <assert.h>
/* number of points in the wavetable */
/* assuming 32 bit int */
#define NINTBITS 10
#define NFRACBITS 20
#define TLEN (1<<NINTBITS)
#define MASK ((1<<(NFRACBITS+NINTBITS))-1)
#define PI 3.14159265358979

int main(void){
  /* wavetable, one period */
  float I[TLEN];
  float Q[TLEN];
  int index;

  /* Startup: build wavetable */
  for (index=0; index < TLEN; ++index){
    float p=2.0*PI*(float)index/(float)TLEN;
    I[index]=cos(p);
    Q[index]=-sin(p); /* upconconversion */
    /* alternative: Q[index]=+sin(p); for downconversion */
  }
  
  /* Operation: generate sine wave */
  float fsample=38.4e6; /* sampling frequency */
  float fLO=1e6; /* LO frequency */
  float f_increment=fLO/fsample*(float)(1<<(NFRACBITS+NINTBITS));
  unsigned int increment=(int)(f_increment+0.5); /* round */

  int count=10000;
  unsigned int phase=0;
  while (--count){
    int index=(phase>>NFRACBITS);
    assert(index < TLEN);
    printf("%1.7f\t%1.7f\n", I[index], Q[index]);

    /* advance oscillator phase */
    phase += increment;

    /* wrap around full cycle */
    phase &= MASK; 
  }
  return 0;
}
download wavetable.c

Compilation:

gcc -Wall wavetable.c

Running

Unix:
a.out > a.dat

Windows MSYS etc:
a.exe > a.dat

Plotting

Gnuplot:
set xrange [0:300]
plot "a.dat" using 0:1 with linespoints, "a.dat" using 0:2 with linespoints

Result

Program output. Note that samples in adjacent cycles have a different phase (dotted line).
More information on the topic can be found for example here.


prevupnextdisable ads

© Markus Nentwig 2007-2008
The content of this page is provided without any warranty and may not be reproduced without permission.

Comments? Questions?

Please send me a mail! mnentwig@elisanet.fi