Created
November 8, 2013 17:40
-
-
Save kg4sgp/7374702 to your computer and use it in GitHub Desktop.
An amplitude shift keying (ASK) demodulator.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// An ASK Demodulator | |
// Simplicity first. Get it working, then improve! | |
// gcc ask-demod.c -o askdemod -lm -Wall | |
// Usage: ./ask-demod ask-msg.raw ask-msg.txt | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#include <string.h> | |
int main(int argc, char* argv[]){ | |
int sampleRate = 8000; | |
int baud = 50; | |
int sampPerSym = sampleRate/baud; | |
// this is half the value of RMS for a sine wave | |
int threshold = ((sqrt(2)/2)*pow(2,15))/2; | |
int sign = -1; | |
short int sample = 0; | |
short int output = 0; | |
short int rsample = 0; | |
int numBits = 0; | |
int sampsLeft = 0; | |
int counter = 0; | |
short int movAvg[sampPerSym+1]; | |
int movAvgI = 0; | |
int sumMovAvg = 0; | |
memset(&movAvg, 0, sizeof(movAvg)); | |
char bitBuffer = 0; | |
char bitCounter = 0; | |
int i = 0; | |
// open file | |
FILE *filein; | |
FILE *fileout; | |
// opens the file as read only | |
filein = fopen(argv[1], "rb"); | |
if(!filein) { | |
printf("Unable to open input file\n\n"); | |
return 1; | |
} | |
// opens the file as overwrite, binary | |
fileout = fopen(argv[2], "wb"); | |
if(!fileout) { | |
printf("Unable to open output file\n\n"); | |
return 1; | |
} | |
while(!feof(filein)){ | |
// read another sample in | |
fread(&sample, sizeof(short), 1, filein); | |
// rectify by taking abs | |
rsample = abs(sample); | |
// moving average | |
movAvg[movAvgI] = rsample/sampPerSym; | |
sumMovAvg = sumMovAvg + (movAvg[movAvgI]) - (movAvg[(movAvgI+1)%sampPerSym]); | |
movAvgI++; | |
if(movAvgI >= sampPerSym) movAvgI = 0; | |
// hard limit the samples | |
if (sumMovAvg >= threshold) { | |
output = 1; | |
} else { | |
output = -1; | |
} | |
// make bits out of the data | |
if (output == sign) { | |
counter++; | |
} else { | |
// count number of bits we have adding a partal if its good enough | |
numBits = (int)counter/sampPerSym; | |
sampsLeft = counter%sampPerSym; | |
if (sampsLeft > (0.7*sampPerSym)){ | |
numBits++; | |
} | |
// add the bits to our buffer | |
for (i = 0; i < numBits; i++) { | |
bitBuffer <<= 1; | |
// put a 1 or zero in the lsb after shifting the buffer to the left | |
if (sign == 1) { | |
bitBuffer ^= (0x01); | |
} else { | |
bitBuffer &= (0xfe); | |
} | |
bitCounter++; | |
// when we have enough to write a char out do so | |
if(bitCounter > 7){ | |
bitCounter = 0; | |
fwrite(&bitBuffer, sizeof(char), 1, fileout); | |
} | |
} | |
counter = 0; | |
sign *= -1; | |
} | |
// audio stuff | |
// fwrite(&rsample, sizeof(short), 1, fileout); | |
// fwrite(&sumMovAvg, sizeof(short), 1, fileout); | |
// fwrite(&output, sizeof(short), 1, fileout); | |
} | |
return 0; | |
} | |
/* | |
BSD 2-clause license | |
Copyright 2013 Jimmy Carter, KG4SGP. All rights reserved. | |
Redistribution and use in source and binary forms, with or without modification, are | |
permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright notice, this list of | |
conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright notice, this list | |
of conditions and the following disclaimer in the documentation and/or other materials | |
provided with the distribution. | |
THIS SOFTWARE IS PROVIDED BY Jimmy Carter ''AS IS'' AND ANY EXPRESS OR IMPLIED | |
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Jimmy Carter OR | |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment