Sample audio input for square waves

Jan 16, 2013 at 12:44 PM

I want to monitor the microphone input and listen for a square wave type input. This is actually Morse Code and I want to sense the "dots" and the "dashes". Programmatically I will have to determine which is which as the length of each and time in-between can fluctuate. My question is, what would be the best way to parse the input stream to check for differences in amplitude. There is bound to be some background noise so the input would be a low value then go high possibly with a leading spike. this high level would be maintained and then drop to a low value again. Any pointers on what commands I could use to monitor the stream of values?

Coordinator
Jan 21, 2013 at 1:43 PM

you can examine the value of each sample, by handling the DataAvailable property of WaveIn. This comes as a byte array, so if it was 16 bit samples, you'd use BitConverter.ToInt16 on each pair to get the sample value. Then implement your own logic to determine whether you are currently in a low or high level.

Jan 21, 2013 at 1:51 PM

Could you show some sudo code as I'm not sure about the bitconverter on each pair. Many thanks.

Coordinator
Jan 21, 2013 at 1:56 PM

for 16 bit audio it would be something like this:

void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
    for (int i = 0; i < e.BytesRecorded; i += 2)
    {
        short sampleValue = BitConverter.ToInt16(e.Buffer, i);
    }
}