Created
June 6, 2014 21:59
-
-
Save kg4sgp/6353ba1f10f55cd880b9 to your computer and use it in GitHub Desktop.
A psk31 modulator in the works
This file contains 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
/* This produces raised cosine bpsk*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
typedef struct { | |
long sample_rate; | |
long samples_per_symbol; | |
long max_amp; | |
short int* buffer; | |
long buffer_size; | |
} audioparams; | |
typedef struct { | |
float baud_rate; | |
float frequency; | |
} comparams; | |
float phase = 0; | |
short int* bit_to_audio(audioparams aud, comparams com, char bit) | |
{ | |
/* make sure theres only one bit */ | |
bit &= 0x01; | |
short int* buffer = (short int*)malloc(aud.samples_per_symbol*sizeof(short int)); | |
/* produce bpsk tones */ | |
float w_f = (2.0*(float)M_PI*(float)com.frequency)/((float)(aud.sample_rate)); | |
long i; | |
if (bit == 1) { | |
printf("phase: %f\nmod phase: %f\n", (phase + w_f * (float)aud.samples_per_symbol), phase ); | |
for (i = 0;i < aud.samples_per_symbol; i++) { | |
buffer[i] = (signed short int)(aud.max_amp * sin(w_f * i + phase)); | |
} | |
phase = fmod( (phase + w_f * (float)aud.samples_per_symbol), (2*M_PI) ); | |
} else if (bit == 0) { | |
for (i = 0; i < aud.samples_per_symbol; i++) { | |
buffer[i] = (signed short int)(aud.max_amp * -1 * sin(w_f * i + phase)); | |
} | |
phase = fmod( (phase + w_f * (float)aud.samples_per_symbol), (2*M_PI) ); | |
printf("phase: %f\nmod phase: %f\n", (phase + w_f * (float)aud.samples_per_symbol), phase ); | |
} else { | |
printf("\nSomething went horribly wrong. You shouldn't get here.\nExiting...\n"); | |
return 0; | |
} | |
/* apply raised cosine filter */ | |
for (i = 0; i <aud.samples_per_symbol; i++) { | |
buffer[i] = buffer[i] * sin((M_PI*i)/(aud.samples_per_symbol)); | |
} | |
return buffer; | |
} | |
short int bits_to_aud(audioparams aud, comparams com, char bits, char num_of_bits, FILE* fout) | |
{ | |
if(num_of_bits > 8){ | |
printf("\nError in bits_to_aud, num_of_bits must be <= 8\n"); | |
return 0; | |
} | |
short int* buffer; | |
int i; | |
for (i = 0; i < num_of_bits; i++) { | |
char bit = 0x01 & (bits>>i); | |
buffer = bit_to_audio(aud, com, bit); | |
fwrite(buffer, aud.samples_per_symbol*sizeof(short int), 1, fout); | |
} | |
return 0; | |
} | |
int main(int argc, char* argv[]) | |
{ | |
FILE* fout; | |
if( (fout = fopen(argv[1], "wb")) == NULL ){ | |
printf("\nError opening output file.\nExiting...\n"); | |
return 0; | |
} | |
audioparams audio; | |
comparams coms; | |
coms.baud_rate = 31.25; | |
coms.frequency = 650; | |
audio.sample_rate = 8000; | |
audio.samples_per_symbol = (long)floor(audio.sample_rate/coms.baud_rate); | |
audio.max_amp = pow(2, (8*sizeof(short))-1); | |
char bitpattern = 0xaa; | |
int i; | |
for(i = 0; i < 100; i++){ | |
bits_to_aud(audio, coms, bitpattern, (char)(sizeof(bitpattern)*8), fout); | |
} | |
printf("\nSample Rate: %ld\nBaudrate: %f\nFrequency: %f\nMax amplitude: %ld\nSamples Per Symbol: %ld\n", audio.sample_rate, coms.baud_rate, coms.frequency, audio.max_amp, audio.samples_per_symbol); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment