Wednesday, May 8, 2013

Direct SSB generation by frequency modulating a PLL

The following code can generate SSB modulation just by controlling a PLL carrier. I have applied this method on the RapsberryPi PLL, and made several contacts on 40m and 20m band with my RaspberryPi.

I will explain the principle in more detail, later. The original idea is depicted here:

The original idea (as seen above) was to implement a complete SSB modulator and demodulator with software in a ATTINY45 micro-controller chip and a AD9834 as DDS. The software samples microphone signal with ADC and then derives the amplitude and phase of the audio signal. The amplitude and phase extraction is derived  from the in-phase microphone signal and its 90 degree phase shifted quadrature signal (created by a Hilbert transform on the in-phase signal). The resulting phase is replicated on the oscillator signal of the DDS, and the amplitude information is used to change the drive-strength (current) of the DDS. The output of the DDS is representing a clean single side-band output SSB near the configured frequency.

To proof this idea is working, I implemented the algorithm on a RaspberryPi. In the RaspberryPi setup there is no DDS, but there is a digital-PLL on the micro-processor SoC which we can use for RF generation. By manipulating the Integer and Fractional dividers, a frequency can be generated from 0 to 250 MHz on GPIO4 pin with a resolution of about 20 Hz at 7 MHz. By alternating the Fractional divider between two nearby values,  the resolution can be further increased to micro-Hz level; for this a 32 bit clock at 32 MHz is used to generate a PWM alike switching pattern between the two Fractional divider values.

The PLL oscillator can be phase modulated by short manipulations of the configured frequency. Increasing the frequency temporarily and then restoring to its original frequency, will shift the phase upwards, while decreasing the frequency temporarily will decrease the phase of the signal.
In this way the phase information for generating a SSB signal can be applied to the RaspberryPi PLL by means of frequency modulation. To do so, first phase differences are calculated for every sample. For each phase deviation (difference) the corresponding frequency change is calculated. This is dependent on the rate that the PLL is changed from frequency; the exact formula is: dev_freq_hz = dev_phase_rad * samp_rate_hz / (2 * pi)

The amplitude signal can be applied by changing the drive-strength of the GPIO4 port, which has 8 levels with a dynamic range of about 13 dB. After some experimenting, amplitude information can be completely rejected, instead the frequency must be placed outside the single side band (e.g. placed to center freq). In this case single side band is generated wihout suppressed carrier, i.e. a constant amplitude envelope is applied (good to prevent RF interference). In this case the audio quality does not seem to suffer from this constant amplitude, and the audio quality becomes even better by applying a little-bit of noise to the microphone input. My impression is that using SSB with constant amplitude increases the readability when the signal is just above the noise.

The RaspberryPi receives the Microphone input via an external USB sound device. To improve the SSB quality, the signal is companded by a A-law compression technique. Three parallel BS170 MOSFETs where directly driven by RaspberryPi GPIO4 output to create about 1Watt of RF. On 40m I could made several SSB contacts through Europe using this setup, receiving stations back by using a nearby online WebSDR receiver. From my location in south-Netherlands following contacts where made:

Apr 27 14:00 40m on4azw/p
Apr 27 14:00 40m pd6king
Apr 27 14:00 40m pa150ba
Apr 27 14:00 40m hb9ag
Apr 27 14:00 40m pd2edr
Apr 28 13:22 40m m0zag
Apr 28 13:22 40m g100rsgb
Apr 28 13:22 40m hb9efx
Apr 28 13:22 40m pa3zax
Apr 28 13:22 40m tm02ref
Apr 28 16:00 20m ea2dt
Apr 29 19:00 40m g3mlo
Apr 30 10:23 7100 pc1king
Apr 30 16:42 7082 pa2cvd
Apr 30 17:00 7092 dl9fcs Gazi, Frankfurt 57
Apr 30 18:30 7074 pa3a Arie, Barendrecht
May  4 11:52 7107 g100c
May  9 15:20 7077 dl/pc1mk Menno, Borkum

Below a fragment of the code that generates SSB signal in the form of amplitude and frequency deviation information, based on the inputed audio signal:

#define ln(x) (log(x)/log(2.718281828459045235f))

static int xv[45];

void filter(int val, int* i, int* q)
    int j;

    for (j = 0; j < 44; j++) {
        xv[j] = xv[j+1];
    xv[44] = val;

    *i = xv[22];

    int _q = (xv[1] + xv[3] + xv[5] + xv[7]+xv[7] + 4*xv[9] + 6*xv[11] \
        + 9*xv[13] + 14*xv[15] + 23*xv[17] + 41*xv[19] + 127*xv[21] \
        - (127*xv[23] + 41*xv[25] + 23*xv[27] + 14*xv[29] + 9*xv[31] \
        + 6*xv[33] + 4*xv[35] + xv[37]+xv[37] + xv[39] + xv[41] + xv[43]) ) / 202;

  *q = _q;

int arctan2(int y, int x)
   int abs_y = abs(y);
   int angle;
   if(x >= 0){
      angle = 45 - 45 * (x - abs_y) / ((x + abs_y)==0?1:(x + abs_y));
   } else {
      angle = 135 - 45 * (x + abs_y) / ((abs_y - x)==0?1:(abs_y - x));
   return (y < 0) ? -angle : angle; // negate if in quad III or IV

static float t = 0;
static float prev_f = 0;
static float prev_phase = 0;
static float acc = 0;

void ssb(float in, float fsamp, float* amp, float* df)
   int i, q;
   float phase;

   filter(in * 128, &i, &q);
   *amp = sqrt( i*i + q*q) / 128.0f; 
   if(*amp > 1.0){
     printf("amp overflow %f\n", *amp);
     *amp = 1.0;
   phase = M_PI + ((float)arctan2(q,i)) * M_PI/180.0f;
   float dp = phase - prev_phase;
   if(dp < 0) dp = dp + 2*M_PI;
   prev_phase = phase;

   *df = dp*fsamp/(2.0f*M_PI);

void main(){
    int samplerate = 8000; // set sample-rate of microphone input here, use 4800 to 8000
    int notvi = 0;   // set no TVI to true if you want constant carrier

    for(;;) {    
        float df, amp;

        int data = //16 bit microphone input here
        ssb((float)data/32767.0, samplerate, &amp, &df);

        float A = 87.7f; // compression parameter
        amp = (fabs(amp) < 1.0f/A) ? A*fabs(amp)/(1.0f+ln(A)) : (1.0f+ln(A*fabs(amp)))/(1.0f+ln(A)); //compand

        int ampval = (int)(round(amp * 8.0f)) - 1;
        int enaval = (ampval < 0) ? 0 : 1;

        if(notvi && ampval < 0){
          enaval = 1; // tx always on
          df = 0;
        if(notvi) ampval = 7; // optional: no amplitude changes

        if(ampval>7) ampval=7;
        if(ampval<0 ampval="0;<!--0--">

       // here, send ampval to 3 bit drive-strength register (if available)
       // here, add or substract df from PLL center frequency (in Hz), adding will make USB, substract. LSB