DSP tutorial: RTTY decoder using FFT filtering

This example is a slightly better RTTY decoder, it uses FFT only for filtering, still uses chunks but doesn’t make bit decisions by calculating the average power of them.

Instead it uses the signal flow discussed in this paper:

Note that this example only implements the demodulator, UART emulator and Baudot terminal blocks from the diagram. For further explanation of the mechanism, continue to the next section of the tutorial.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// reads a chunk from the sound device
    public int getChunk(double[] chunkBuffer) {
        byte[] abBuffer = new byte[oneChunkSampleCount*2];
        int samplesRead = 0;

        // waiting for the buffer to get filled
        try {
            while (tdl.available() < abBuffer.length)
                Thread.sleep(0, 1); // without this, the audio will be choppy

            int bytesRead = tdl.read(abBuffer, 0, abBuffer.length);

            // converting frames stored as bytes to double values
            samplesRead = bytesRead / 2;
            for (int i = 0; i < samplesRead; i++) {
                chunkBuffer[i] = ((abBuffer[i * 2] & 0xFF) | (abBuffer[i * 2 + 1] << 8)) / 32768.0;
            }
        } catch (InterruptedException e) {
        }
        return samplesRead;
    }

    // analyzes a chunk with FFT and returns the bit value
    public int demodulator() {
        int samplesRead = getChunk(signalLine1Buffer);

        // copying to the other line
        for (int i = 0; i < signalLine1Buffer.length; i++) {
            signalLine2Buffer[i] = signalLine1Buffer[i];
        }

        // emptying fft buffer in case of samples read != fft buffer length
        for (int i = 0; i < signalLine1FFTData.length; i++)
            signalLine1FFTData[i] = signalLine2FFTData[i] = 0;
        for (int i = 0; i < samplesRead; i++) {
            // copying audio data to the fft data buffer, imaginary part is 0
            signalLine1FFTData[2 * i] = signalLine1Buffer[i];
            signalLine1FFTData[2 * i + 1] = 0;

            signalLine2FFTData[2 * i] = signalLine2Buffer[i];
            signalLine2FFTData[2 * i + 1] = 0;
        }
        fft.complexForward(signalLine1FFTData);
        fft.complexForward(signalLine2FFTData);

        int binOfFreq0 = (int)Math.round((FREQ0*(signalLine1FFTData.length/2)/(double)SAMPLERATE)*2);
        int binOfFreq1 = (int)Math.round((FREQ1*(signalLine2FFTData.length/2)/(double)SAMPLERATE)*2);

        // zeroing out all freqs except the carrier on signal line 1
        for (int i = 0; i < signalLine1FFTData.length; i += 2) {
            if (i != binOfFreq0)
                signalLine1FFTData[i] = signalLine1FFTData[i + 1] = 0;
        }
        // zeroing out all freqs except the carrier on signal line 2
        for (int i = 0; i < signalLine2FFTData.length; i += 2) {
            if (i != binOfFreq1)
                signalLine2FFTData[i] = signalLine2FFTData[i + 1] = 0;
        }
        fft.complexInverse(signalLine1FFTData, false);
        fft.complexInverse(signalLine2FFTData, false);

        for (int i = 0; i < samplesRead; i++) {
            signalLine1Buffer[i] = signalLine1FFTData[2 * i];
            signalLine2Buffer[i] = signalLine2FFTData[2 * i];
            signalLine1Buffer[i] *= signalLine1Buffer[i]; // squaring
            signalLine2Buffer[i] *= signalLine2Buffer[i]; // squaring
            signalLine2Buffer[i] *= -1; // inverting signal line 2

            // summing the two signal lines
            signalLine1Buffer[i] += signalLine2Buffer[i];
        }

        int lowPassCutFreq = (int)Math.ceil(BITSPERSEC);
        int binOfLowPassCutFreq = (int)Math.round(((lowPassCutFreq * (signalLine1FFTData.length/2)) / (double)SAMPLERATE)*2);

        // lowpass filtering
        for (int i = 0; i < signalLine1FFTData.length; i++)
            signalLine1FFTData[i] = 0;
        for (int i = 0; i < samplesRead; i++) {
            // copying audio data to the fft data buffer, imaginary part is 0
            signalLine1FFTData[2 * i] = signalLine1Buffer[i];
            signalLine1FFTData[2 * i + 1] = 0;
        }
        fft.complexForward(signalLine1FFTData);

        for (int i = 0; i < signalLine1FFTData.length; i += 2) {
            if (i > binOfLowPassCutFreq)
                signalLine1FFTData[i] = signalLine1FFTData[i + 1] = 0;
        }
        fft.complexInverse(signalLine1FFTData, false);

        for (int i = 0; i < samplesRead; i++) {
            signalLine1Buffer[i] = signalLine1FFTData[2 * i] / 70;
        }
        baos.write(getBytesFromDoubles(signalLine1Buffer, samplesRead), 0, samplesRead * 2); // writing to the output wav for debugging purposes

        double average = 0;
        for (int i = 0; i < samplesRead; i++) {
            average += signalLine1Buffer[i];
        }
        average /= samplesRead;

        if (average > 0)
            return 0;
        else
            return 1;
    }

    // this function returns at the half of a bit with the bit's value
    public int getBitDPLL() {
        boolean chunkPhaseChanged = false;
        int chunkVal = -1;
        int chunkPhase = 0;

        while (chunkPhase < CHUNKCOUNTPERBIT) {
            chunkVal = demodulator();
            if (chunkVal == -1)
                break;

            if (!chunkPhaseChanged && chunkVal != oldChunkVal) {
                if (chunkPhase < CHUNKCOUNTPERBIT/2)
                    chunkPhase++; // early
                else
                    chunkPhase--; // late
                chunkPhaseChanged = true;
            }
            oldChunkVal = chunkVal;
            chunkPhase++;
        }

        // putting a tick to the output wav signing the moment when the DPLL returned
        /*baos.write(100);
        baos.write(100);
        baos.write(100);
        baos.write(100);
        baos.write(100);
        baos.write(100);*/

        return chunkVal;
    }

    // this function returns only when the start bit is successfully received
    public void waitForStartBit() {
        int bitResult;

        while (!Thread.interrupted()) {
            do {
                bitResult = demodulator();
            } while ((bitResult == 0 || bitResult == -1) && !Thread.interrupted());
           
            //System.out.println("sb0: 1");
           
            do {
                bitResult = demodulator();
            } while ((bitResult == 1 || bitResult == -1) && !Thread.interrupted());
           
            //System.out.println("sb1: 0");

            // waiting half bit time
            for (int i = 0; i < CHUNKCOUNTPERBIT/2; i++)
                bitResult = demodulator();

            //System.out.println("sb2: " + bitResult);

            if (bitResult == 0)
                break;
        }
        //System.out.println("start bit ok");
    }
   
    @Override
    public void run() {
        tdl.start();

        int byteResult = 0;
        int byteResultp = 0;
        int bitResult;
       
        while (!Thread.interrupted()) {
            waitForStartBit();
           
            System.out.print("0 "); // first bit is the start bit, it's zero
           
            // reading 7 more bits
            for (byteResultp = 1, byteResult = 0; byteResultp < 8; byteResultp++) {
                bitResult = getBitDPLL();
                if (bitResult == -1) {
                    byteResult = -1;
                    break;
                }

                switch (byteResultp) {
                    case 6: // stop bit 1
                        System.out.print(" " + bitResult);
                        break;
                    case 7: // stop bit 2
                        System.out.print(bitResult);
                        break;
                    default:
                        System.out.print(bitResult);
                        byteResult += bitResult << (byteResultp-1);
                }
            }

            if (byteResult == -1)
                continue;

            switch (byteResult) {
                case 31:
                    mode = RTTYMode.letters;
                    System.out.println(" ^L^");
                    break;
                case 27:
                    mode = RTTYMode.symbols;
                    System.out.println(" ^F^");
                    break;
                default:
                    switch (mode) {
                    case letters:
                        System.out.println(" *** " + RTTYLetters[byteResult] + "(" + byteResult + ")");
                        break;
                    case symbols:
                        System.out.println(" *** " + RTTYSymbols[byteResult] + "(" + byteResult + ")");
                        break;
                    }
            }
        }

        tdl.stop();
        tdl.close();
    }

download (855.1 kb)

Name (required)
E-mail (required - never shown publicly)
Webpage URL
Comment:
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> in your comment.

Trackback responses to this post

About me

Nonoo
I'm Nonoo. This is my blog about music, sounds, filmmaking, amateur radio, computers, programming, electronics and other things I'm obsessed with. ... »

Twitter

Listening now

My favorite artists