### 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.

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] | +0.0000 | +1.0000 | +0.0000 | +1.0000 |

[1 ] | +0.8090 | +0.5878 | +0.9877 | -0.1564 |

[2] | +0.9511 | -0.3090 | -0.3090 | -0.9511 |

[3] | +0.3090 | -0.9511 | -0.8910 | +0.4540 |

[4] | -0.5878 | -0.8090 | +0.5878 | +0.8090 |

[5] | -1.0000 | -0.0000 | +0.7071 | -0.7071 |

In Listing One, the mark waveform's sine table is at index 0 (corresponding to* sin(q*_{m}*)* in Figure 3), the cosine at* 1(cos(q*_{m}*)),* the space waveform's sine table at *2(sin(q*_{s}*))*, and the cosine at *3(cos (q*_{s}*)*.

// clear out intermediate sums factors [0] = factors [1] = factors [2] = factors [3] = 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 [0] += handle -> correlates [0][i] * val; factors [1] += handle -> correlates [1][i] * val; factors [2] += handle -> correlates [2][i] * val; factors [3] += handle -> correlates [3][i] * val; j++; }

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 [0]*and*factors [2]*accumulators. - Assign the value of the sample at the ring buffer start to the
*factors [1]*and*factors [3]*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 [0] * factors [0] + factors [1] * factors [1] > factors [2] * factors [2] + factors [3] * factors [3];

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.