 Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

# Real-Time Enough

### The FSK Algorithm

How much work is it? Look at the FSK algorithm itself. Mathematically speaking, we are constructing the formula in Figure 3. The gist of this formula is that you take each sample and multiply it by the sine at the mark frequency, the cosine at the mark frequency, the sine at the space frequency, and the cosine at the space frequency. We add up all the multiplication and square each set. Whichever set turns out to be bigger indicates which tone you've decoded.

[Click image to view at full size] Figure 3: The formula.

To detect 1200 Hz and 2200 Hz tones, you construct a "correlation template"—a table of four normalized (meaning that they are between -1 and +1) sets of values, giving the sine and cosine of a 1200 Hz and 2200 Hz waveform sampled at the sampling rate of 8 kHz. I only require enough samples to match the slowest waveform, which is 1200 Hz in this case. One complete waveform of a 1200 Hz sine wave, sampled at 8 kHz, will require 6 2/3 samples (8000/1200=6.666...), so I can get away with just six samples. The correlation tables look like Table 1.

 M A R K S P A C E sine cosine sine cosine  +0.0000 +1.0000 +0.0000 +1.0000 [1 ] +0.8090 +0.5878 +0.9877 -0.1564  +0.9511 -0.3090 -0.3090 -0.9511  +0.3090 -0.9511 -0.8910 +0.4540  -0.5878 -0.8090 +0.5878 +0.8090  -1.0000 -0.0000 +0.7071 -0.7071

Table 1: Correlation tables.

In Listing One, the mark waveform's sine table is at index 0 (corresponding to sin(qm) in Figure 3), the cosine at 1(cos(qm)), the space waveform's sine table at 2(sin(qs)), and the cosine at 3(cos (qs).

```// clear out intermediate sums
factors  = factors  = factors  = factors  = 0;

// get to the beginning of the samples
j = handle -> ringstart;

// do the multiply-accumulates
for (i = 0; i < handle -> corrsize; i++) {
if (j >= handle -> corrsize) {
j = 0;
}
val = handle -> buffer [j];
factors  += handle -> correlates [i] * val;
factors  += handle -> correlates [i] * val;
factors  += handle -> correlates [i] * val;
factors  += handle -> correlates [i] * val;
j++;
}

```
Listing One

The algorithm assumes that samples are placed into the ring buffer handle -> buffer, with the variable handle -> ringstart pointing to the beginning of the buffer.

Then, each sample in the buffer is multiplied by each of the four correlates, and the values are accumulated into the factors array. (The logic dealing with j simply makes sure that j loops around the ring buffer correctly.) The four factors correspond to the four summations in Figure 3.

So far, the total amount of work is 24 multiply-accumulates, which is followed by a comparison and some buffer housekeeping.

At this point, it's fair to point out a small optimization.

Notice that the correlates at index zero are always zero (for the sine components) and one (for the cosine components). The code could be optimized slightly to:

• Assign the value zero to the factors  and factors  accumulators.
• Assign the value of the sample at the ring buffer start to the factors  and factors  accumulators.
• Start at the next sample position.
• Start at one rather than zero for the multiply-accumulate cycle.

This would bring the code down by four multiply-accumulates (17 percent savings). (Alternatively, you could do the aforementioned optimization, but keep the same number of multiply-accumulates and just extend the table. Remember, I'm using six samples where my calculations indicated 6 and 2/3 samples, so 7 samples is okay, too.)

Finally, once all the multiply-accumulates are done for the four factors, you square and add the respective mark and space factors, and see which one is bigger:

```
handle -> current_bit = factors  *
factors  + factors  *
factors  > factors  *
factors  + factors  *
factors ;

```

Whichever set of factors is bigger indicates which frequency was detected; therefore, if you get a true value, it means you detected a mark; otherwise, you detected a space.

The algorithm just described is basically a pair of matched filters at the mark and space frequencies; the result is obtained by comparing the amplitude of the output of the two filters.

### More Insights To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.

## Featured Reports ## Featured Whitepapers 