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.


Channels ▼
RSS

Embedded Systems

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

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 [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++;
}

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


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips 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.