# How do I decode(demodulation) and get original data from audio sample data

Here I'm talking about the less-than-ideal situations, in other words, the audio sample data may have burrs or error bits in it.

Encode and decode theory: I am using FSK to encode my original binary data (0,1 and lead-byte), and transform the square wave it generated to sine wave. Then I generate a stream of audio samples through mic (for example, a stream of 44100 16-bit integers for every second etc.). When I decode (c language), the key factor is, 0:1:lead-byte = 1:2:4(or other ratio).

For example:

original binary data: 0|1...0|1.

encoded wave data: lead-byte...lead-byte 0|1...0|1 lead-byte.

The typical ideal samples and bad samples waveform are as below. good wave

Now my decode method is simple (just count the samples up and below zero, and get stable lead-byte width first, then use 1:2:4 to calculate 0 and 1's width). Should I use more complex mathematical analysis in decoding? If so, what theory can I use, Fourier? Gaussian Noise? Rayleigh Fading? Goertzel algorithm?