DSP tutorial: FSK encoder

This example encodes ASCII characters as sine wave whistles on two audible frequencies, so it’s an AFSK encoder.

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
    private static void playBit(int bit) {
        int samplesNeeded = (int)Math.round(SAMPLERATE/BITSPERSEC);
        double[] abBufferDouble = new double[samplesNeeded];

        for (int j = 0; j < samplesNeeded; j++) {
            oscPhase += (2 * Math.PI * (bit == 0 ? FREQ0 : FREQ1)) / SAMPLERATE;
   
            abBufferDouble[j] = Math.sin(oscPhase) * 0.2;
               
            if (oscPhase >= 2 * Math.PI)
                oscPhase -= 2 * Math.PI;
        }
        sdl.write(getBytesFromDoubles(abBufferDouble, samplesNeeded), 0, samplesNeeded*sdl.getFormat().getFrameSize());
    }

    public static void main(String[] args) {
        AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, SAMPLERATE, 16, 1, 2, SAMPLERATE, false);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat, BUFFERSIZE);

        try {
            sdl = (SourceDataLine)AudioSystem.getLine(info);
            sdl.open(audioFormat, BUFFERSIZE);
        } catch (LineUnavailableException e2) {
            e2.printStackTrace();
        }
        sdl.start();

        //while (1 != 0) {
            for (int msgPos = 0; msgPos < msg.length(); msgPos++) {
                char c = msg.charAt(msgPos);
                System.out.print(c + " (" + (int)c + "): ");
                for (int charBitPos = 7; charBitPos >= 0; charBitPos--) {
                    int bit = ((c & (1 << charBitPos)) == 0 ? 0 : 1);
                    System.out.print(bit);
                    playBit(bit);
                }
                System.out.println();
            }
        //}
        sdl.drain();
        sdl.close();
    }

download (mirror2) (6.5 kb, 210 dls, today: 0)

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