c50: json output

pull/12/head
Zilog80 2019-05-09 21:36:45 +02:00
rodzic ac81fbd11f
commit 95f48e8536
1 zmienionych plików z 105 dodań i 67 usunięć

Wyświetl plik

@ -2,6 +2,7 @@
/* /*
C50 C50
(empfohlen: sample rate 48kHz) (empfohlen: sample rate 48kHz)
gcc c50dft.c -lm -o c50dft
*/ */
#include <stdio.h> #include <stdio.h>
@ -11,9 +12,10 @@
typedef unsigned char ui8_t; typedef unsigned char ui8_t;
int option_verbose = 0, static int option_verbose = 0,
option_raw = 0, option_raw = 0,
option_dft = 0, option_dft = 0,
option_json = 0,
wavloaded = 0; wavloaded = 0;
@ -21,21 +23,22 @@ typedef struct {
int frnr; int frnr;
int jahr; int monat; int tag; int jahr; int monat; int tag;
int std; int min; int sek; int std; int min; int sek;
double lat; double lon; double h; float lat; float lon; float alt;
unsigned chk; unsigned chk;
} gpx_t; } gpx_t;
gpx_t gpx; static gpx_t gpx;
/* ------------------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------------------ */
#define BAUD_RATE 2400 #define BAUD_RATE 2400
int sample_rate = 0, bits_sample = 0, channels = 0; static unsigned int sample_rate = 0;
static int bits_sample = 0, channels = 0;
//float samples_per_bit = 0; //float samples_per_bit = 0;
int findstr(char *buff, char *str, int pos) { static int findstr(char *buff, char *str, int pos) {
int i; int i;
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
if (buff[(pos+i)%4] != str[i]) break; if (buff[(pos+i)%4] != str[i]) break;
@ -43,7 +46,7 @@ int findstr(char *buff, char *str, int pos) {
return i; return i;
} }
int read_wav_header(FILE *fp) { static int read_wav_header(FILE *fp) {
char txt[4+1] = "\0\0\0\0"; char txt[4+1] = "\0\0\0\0";
unsigned char dat[4]; unsigned char dat[4];
int byte, p=0; int byte, p=0;
@ -100,8 +103,9 @@ int read_wav_header(FILE *fp) {
} }
#define EOF_INT 0x1000000 #define EOF_INT 0x1000000
static unsigned int sample_count;
int read_signed_sample(FILE *fp) { // int = i32_t static int read_signed_sample(FILE *fp) { // int = i32_t
int byte, i, ret; // EOF -> 0x1000000 int byte, i, ret; // EOF -> 0x1000000
for (i = 0; i < channels; i++) { for (i = 0; i < channels; i++) {
@ -135,31 +139,31 @@ int read_signed_sample(FILE *fp) { // int = i32_t
#define LEN_BITFRAME (9*BITS) #define LEN_BITFRAME (9*BITS)
#define HEADLEN 20 #define HEADLEN 20
char header[] = "00000000010111111111"; // 0x00 0xFF static char header[] = "00000000010111111111"; // 0x00 0xFF
char buf[HEADLEN+1] = "x"; static char buf[HEADLEN+1] = "x";
int bufpos = -1; static int bufpos = -1;
int bitpos; static int bitpos;
ui8_t bits[LEN_BITFRAME+20] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, static ui8_t bits[LEN_BITFRAME+20] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1}; 0, 1, 1, 1, 1, 1, 1, 1, 1, 1};
ui8_t bytes[LEN_BITFRAME/BITS+2]; static ui8_t bytes[LEN_BITFRAME/BITS+2];
double x[N]; static float x[N];
double complex Z[N], w[N], expw[N][N], ew[N*N]; static float complex Z[N], w[N], expw[N][N], ew[N*N];
int ptr; static int ptr;
double Hann[N], buffer[N+1], xn[N]; static float Hann[N], buffer[N+1], xn[N];
void init_dft() { static void init_dft() {
int i, k, n; int i, k, n;
for (i = 0; i < N; i++) Hann[i] = 0; for (i = 0; i < N; i++) Hann[i] = 0;
for (i = 0; i < WLEN; i++) Hann[i] = 0.5 * (1 - cos( 2 * M_PI * i / (double)(WLEN-1) ) ); for (i = 0; i < WLEN; i++) Hann[i] = 0.5 * (1 - cos( 2 * M_PI * i / (float)(WLEN-1) ) );
//Hann[i+(N-1-WLEN)/2] = 0.5 * (1 - cos( 2 * M_PI * i / (double)(WLEN-1) ) ); //Hann[i+(N-1-WLEN)/2] = 0.5 * (1 - cos( 2 * M_PI * i / (float)(WLEN-1) ) );
for (k = 0; k < N; k++) { for (k = 0; k < N; k++) {
w[k] = -I*2*M_PI * k / (double)N; w[k] = -I*2*M_PI * k / (float)N;
for (n = 0; n < N; n++) { for (n = 0; n < N; n++) {
expw[k][n] = cexp( w[k] * n ); expw[k][n] = cexp( w[k] * n );
ew[k*n] = expw[k][n]; ew[k*n] = expw[k][n];
@ -168,9 +172,9 @@ void init_dft() {
} }
double dft_k(int k) { static float dft_k(int k) {
int n; int n;
double complex Zk; float complex Zk;
Zk = 0; Zk = 0;
for (n = 0; n < N; n++) { for (n = 0; n < N; n++) {
@ -179,7 +183,7 @@ double dft_k(int k) {
return cabs(Zk); return cabs(Zk);
} }
void dft() { static void dft() {
int k, n; int k, n;
for (k = 0; k < N/2; k++) { // xn reell, brauche nur N/2 unten for (k = 0; k < N/2; k++) { // xn reell, brauche nur N/2 unten
@ -190,12 +194,12 @@ void dft() {
} }
} }
void dft2() { static void dft2() {
int s, l, l2, i, j, k; int s, l, l2, i, j, k;
double complex w1, w2, T; float complex w1, w2, T;
for (i = 0; i < N; i++) { for (i = 0; i < N; i++) {
Z[i] = (double complex)xn[i]; Z[i] = (float complex)xn[i];
} }
j = 1; j = 1;
@ -216,8 +220,8 @@ void dft2() {
for (s = 0; s < LOG2N; s++) { for (s = 0; s < LOG2N; s++) {
l2 = 1 << s; l2 = 1 << s;
l = l2 << 1; l = l2 << 1;
w1 = (double complex)1.0; w1 = (float complex)1.0;
w2 = cexp(-I*M_PI/(double)l2); w2 = cexp(-I*M_PI/(float)l2);
for (j = 1; j <= l2; j++) { for (j = 1; j <= l2; j++) {
for (i = j; i <= N; i += l) { for (i = j; i <= N; i += l) {
k = i + l2; k = i + l2;
@ -230,9 +234,9 @@ void dft2() {
} }
} }
int max_bin() { static int max_bin() {
int k, kmax; int k, kmax;
double max; float max;
max = 0; kmax = 0; max = 0; kmax = 0;
for (k = 0; k < N/2-1; k++) { for (k = 0; k < N/2-1; k++) {
@ -245,22 +249,22 @@ int max_bin() {
return kmax; return kmax;
} }
double freq2bin(int f) { static float freq2bin(int f) {
return f * N / (double)sample_rate; return f * N / (float)sample_rate;
} }
int bin2freq(int k) { static int bin2freq(int k) {
return sample_rate * k / N; return sample_rate * k / N;
} }
/* ------------------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------------------ */
void inc_bufpos() { static void inc_bufpos() {
bufpos = (bufpos+1) % HEADLEN; bufpos = (bufpos+1) % HEADLEN;
} }
int compare() { static int compare() {
int i=0, j = bufpos; int i=0, j = bufpos;
while (i < HEADLEN) { while (i < HEADLEN) {
@ -272,7 +276,7 @@ int compare() {
return i; return i;
} }
int bits2bytes8N1(ui8_t bits[], ui8_t bytes[], int n) { static int bits2bytes8N1(ui8_t bits[], ui8_t bytes[], int n) {
int i, j, byteval=0, d=1; int i, j, byteval=0, d=1;
for (j = 0; j < n; j++) { for (j = 0; j < n; j++) {
@ -288,15 +292,15 @@ int bits2bytes8N1(ui8_t bits[], ui8_t bytes[], int n) {
return 0; return 0;
} }
void printGPX() { static void printGPX() {
int i; int i;
printf(" %4d-%02d-%02d", gpx.jahr, gpx.monat, gpx.tag); printf(" %04d-%02d-%02d", gpx.jahr, gpx.monat, gpx.tag);
printf(" %2d:%02d:%02d", gpx.std, gpx.min, gpx.sek); printf(" %02d:%02d:%02d", gpx.std, gpx.min, gpx.sek);
printf(" "); printf(" ");
printf(" lat: %.5f°", gpx.lat); printf(" lat: %.5f", gpx.lat);
printf(" lon: %.5f°", gpx.lon); printf(" lon: %.5f", gpx.lon);
printf(" h: %.1fm", gpx.h); printf(" alt: %.1f", gpx.alt);
if (option_verbose) { if (option_verbose) {
printf(" # "); printf(" # ");
@ -306,8 +310,16 @@ void printGPX() {
printf("\n"); printf("\n");
} }
static void printJSON() {
// UTC or GPS time ?
printf("{ \"datetime\": \"%04d-%02d-%02dT%02d:%02d:%02dZ\", \"lat\": %.5f, \"lon\": %.5f, \"alt\": %.1f",
gpx.jahr, gpx.monat, gpx.tag, gpx.std, gpx.min, gpx.sek, gpx.lat, gpx.lon, gpx.alt);
printf(" }\n");
//printf("\n");
}
// Chechsum Fletcher16 // Chechsum Fletcher16
unsigned check2(ui8_t *bytes, int len) { static unsigned check2(ui8_t *bytes, int len) {
int sum1, sum2; int sum1, sum2;
int i; int i;
@ -323,7 +335,7 @@ unsigned check2(ui8_t *bytes, int len) {
return sum2 | (sum1<<8); return sum2 | (sum1<<8);
} }
/* // equivalent /* // equivalent
unsigned check16(ui8_t *bytes, int len) { static unsigned check16(ui8_t *bytes, int len) {
unsigned sum1, sum2; unsigned sum1, sum2;
int i; int i;
sum1 = sum2 = 0; sum1 = sum2 = 0;
@ -336,16 +348,18 @@ unsigned check16(ui8_t *bytes, int len) {
} }
*/ */
double NMEAll2(int ll) { // NMEA GGA,GLL: ll/1e5=(D)DDMM.mmmm static float NMEAll2(int ll) { // NMEA GGA,GLL: ll/1e5=(D)DDMM.mmmm
int deg = ll / 10000000; int deg = ll / 10000000;
double min = (ll - deg*10000000)/1e5; float min = (ll - deg*10000000)/1e5;
return deg+min/60.0; return deg+min/60.0;
} }
int evalBytes2() { static int evalBytes2() {
int i, val = 0; int i, val = 0;
ui8_t id = bytes[2]; ui8_t id = bytes[2];
unsigned check; unsigned check;
static unsigned int cnt_dat = -1, cnt_tim = -1,
cnt_lat = -1, cnt_lon = -1, cnt_alt = -1;
check = ((bytes[7]<<8)|bytes[8]) != check2(bytes+2, 5); check = ((bytes[7]<<8)|bytes[8]) != check2(bytes+2, 5);
@ -359,6 +373,7 @@ int evalBytes2() {
gpx.monat = mon; gpx.monat = mon;
gpx.jahr = 2000+jrz; gpx.jahr = 2000+jrz;
gpx.chk = (gpx.chk & ~(0x1<<0)) | (check<<0); gpx.chk = (gpx.chk & ~(0x1<<0)) | (check<<0);
if (check==0) cnt_dat = sample_count;
} }
else if (id == 0x15 ) { // time else if (id == 0x15 ) { // time
int std = val / 10000; int std = val / 10000;
@ -368,28 +383,46 @@ int evalBytes2() {
gpx.min = min; gpx.min = min;
gpx.sek = sek; gpx.sek = sek;
gpx.chk = (gpx.chk & ~(0x1<<1)) | (check<<1); gpx.chk = (gpx.chk & ~(0x1<<1)) | (check<<1);
if (check==0) cnt_tim = sample_count;
} }
else if (id == 0x16 ) { // lat: wie NMEA mit Faktor 1e5 else if (id == 0x16 ) { // lat: wie NMEA mit Faktor 1e5
gpx.lat = NMEAll2(val); gpx.lat = NMEAll2(val);
gpx.chk = (gpx.chk & ~(0x1<<2)) | (check<<2); gpx.chk = (gpx.chk & ~(0x1<<2)) | (check<<2);
if (check==0) cnt_lat = sample_count;
} }
else if (id == 0x17 ) { // lon: wie NMEA mit Faktor 1e5 else if (id == 0x17 ) { // lon: wie NMEA mit Faktor 1e5
gpx.lon = NMEAll2(val); gpx.lon = NMEAll2(val);
gpx.chk = (gpx.chk & ~(0x1<<3)) | (check<<3); gpx.chk = (gpx.chk & ~(0x1<<3)) | (check<<3);
if (check==0) cnt_lon = sample_count;
} }
else if (id == 0x18 ) { // alt: decimeter else if (id == 0x18 ) { // alt: decimeter
gpx.h = val/10.0; gpx.alt = val/10.0;
gpx.chk = (gpx.chk & ~(0x1<<4)) | (check<<4); gpx.chk = (gpx.chk & ~(0x1<<4)) | (check<<4);
if (check==0) cnt_alt = sample_count;
} }
if (id == 0x18) printGPX(); if (id == 0x18) {
printGPX();
if (option_json && check==0) {
if ( ((cnt_dat|cnt_tim|cnt_lat|cnt_lon)&0x80000000)==0 &&
cnt_alt - cnt_dat < sample_rate &&
cnt_alt - cnt_tim < sample_rate &&
cnt_alt - cnt_lat < sample_rate &&
cnt_alt - cnt_lon < sample_rate )
{
printJSON();
}
}
}
return check; return check;
} }
void printRaw(int n) { static void printRaw(int n) {
int j; int j;
//if ( ((bytes[7]<<8)|bytes[8]) == check2(bytes+2, 5)) unsigned chkbyt = (bytes[7]<<8) | bytes[8];
unsigned chksum = check2(bytes+2, 5);
//if (chksum == chkbyt)
{ {
for (j = 0; j < LEN_BITFRAME; j++) { for (j = 0; j < LEN_BITFRAME; j++) {
if (j%BITS == 1) printf(" "); if (j%BITS == 1) printf(" ");
@ -400,9 +433,10 @@ void printRaw(int n) {
printf("%02X%02X ", bytes[0], bytes[1]); printf("%02X%02X ", bytes[0], bytes[1]);
printf("%02X ", bytes[2]); printf("%02X ", bytes[2]);
printf("%02X%02X%02X%02X ", bytes[3], bytes[4], bytes[5], bytes[6]); printf("%02X%02X%02X%02X ", bytes[3], bytes[4], bytes[5], bytes[6]);
printf("%02X%02X", bytes[7], bytes[8]); printf("%02X%02X", bytes[7], bytes[8]); // chkbyt
if (option_verbose) { if (option_verbose) {
printf(" # %04X", check2(bytes+2, 5)); printf(" # %04X", chksum);
if (chksum == chkbyt) printf(" [OK]"); else printf(" [NO]");
} }
printf("\n"); printf("\n");
} }
@ -414,15 +448,14 @@ int main(int argc, char *argv[]) {
FILE *fp; FILE *fp;
char *fpname; char *fpname;
int sample; int sample;
unsigned int sample_count;
int i, j, kmax, k0, k1; int i, j, kmax, k0, k1;
int bit = 8, bit0 = 8; int bit = 8, bit0 = 8;
int pos = 0, pos0 = 0; int pos = 0, pos0 = 0;
int header_found = 0; int header_found = 0;
int bitlen; // sample_rate/BAUD_RATE int bitlen; // sample_rate/BAUD_RATE
int len; int len;
double k_f0, k_f1, k_df; float k_f0, k_f1, k_df;
double cb0, cb1; float cb0, cb1;
fpname = argv[0]; fpname = argv[0];
++argv; ++argv;
@ -446,6 +479,11 @@ int main(int argc, char *argv[]) {
else if ( (strcmp(*argv, "-d2") == 0) || (strcmp(*argv, "--dft2") == 0) ) { else if ( (strcmp(*argv, "-d2") == 0) || (strcmp(*argv, "--dft2") == 0) ) {
option_dft = 2; option_dft = 2;
} }
else if ( (strcmp(*argv, "--json") == 0) ) {
option_dft = 0;
option_verbose = 1;
option_json = 1;
}
else { else {
fp = fopen(*argv, "rb"); fp = fopen(*argv, "rb");
if (fp == NULL) { if (fp == NULL) {
@ -481,7 +519,7 @@ int main(int argc, char *argv[]) {
ptr++; ptr++;
sample_count++; sample_count++;
if (ptr == N) ptr = 0; if (ptr == N) ptr = 0;
buffer[ptr] = sample / (double)(1<<bits_sample); buffer[ptr] = sample / (float)(1<<bits_sample);
if (sample_count < N) continue; if (sample_count < N) continue;