From 4e4da3f708bc56d53abccede7ee4e0bb4110d631 Mon Sep 17 00:00:00 2001 From: Zilog80 Date: Thu, 21 Apr 2016 08:30:50 +0200 Subject: [PATCH] meteomodem pilotsonde --- m10/pilotsonde/m12.c | 596 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 596 insertions(+) create mode 100644 m10/pilotsonde/m12.c diff --git a/m10/pilotsonde/m12.c b/m10/pilotsonde/m12.c new file mode 100644 index 0000000..d677cbb --- /dev/null +++ b/m10/pilotsonde/m12.c @@ -0,0 +1,596 @@ + +/* + * pilotsonde + * FSK, 4800 baud, 8N1, little endian + * + * gcc -o m12 m12.c -lm + * + */ + + +#include +#include +#include +#include + +#ifdef WIN + #include // cygwin: _setmode() + #include +#endif + + +typedef unsigned char ui8_t; +typedef unsigned short ui16_t; + +typedef struct { + int lat; int lon; int alt; + int vE; int vN; int vU; + double vH; double vD; double vV; + int date; int time; +} datum_t; + +datum_t datum; + +int option_verbose = 0, // ausfuehrliche Anzeige + option_raw = 0, // rohe Frames + option_inv = 0, // invertiert Signal + option_auto = 0, + option_dc = 0, // non-constant bias + option_res = 0, // genauere Bitmessung + wavloaded = 0; + + +#define BAUD_RATE 4800 + +int sample_rate = 0, bits_sample = 0, channels = 0; +float samples_per_bit = 0; + +int findstr(char *buf, char *str, int pos) { + int i; + for (i = 0; i < 4; i++) { + if (buf[(pos+i)%4] != str[i]) break; + } + return i; +} + +int read_wav_header(FILE *fp) { + char txt[4+1] = "\0\0\0\0"; + unsigned char dat[4]; + int byte, p=0; + + if (fread(txt, 1, 4, fp) < 4) return -1; + if (strncmp(txt, "RIFF", 4)) return -1; + if (fread(txt, 1, 4, fp) < 4) return -1; + // pos_WAVE = 8L + if (fread(txt, 1, 4, fp) < 4) return -1; + if (strncmp(txt, "WAVE", 4)) return -1; + // pos_fmt = 12L + for ( ; ; ) { + if ( (byte=fgetc(fp)) == EOF ) return -1; + txt[p % 4] = byte; + p++; if (p==4) p=0; + if (findstr(txt, "fmt ", p) == 4) break; + } + if (fread(dat, 1, 4, fp) < 4) return -1; + if (fread(dat, 1, 2, fp) < 2) return -1; + + if (fread(dat, 1, 2, fp) < 2) return -1; + channels = dat[0] + (dat[1] << 8); + + if (fread(dat, 1, 4, fp) < 4) return -1; + memcpy(&sample_rate, dat, 4); //sample_rate = dat[0]|(dat[1]<<8)|(dat[2]<<16)|(dat[3]<<24); + + if (fread(dat, 1, 4, fp) < 4) return -1; + if (fread(dat, 1, 2, fp) < 2) return -1; + //byte = dat[0] + (dat[1] << 8); + + if (fread(dat, 1, 2, fp) < 2) return -1; + bits_sample = dat[0] + (dat[1] << 8); + + // pos_dat = 36L + info + for ( ; ; ) { + if ( (byte=fgetc(fp)) == EOF ) return -1; + txt[p % 4] = byte; + p++; if (p==4) p=0; + if (findstr(txt, "data", p) == 4) break; + } + if (fread(dat, 1, 4, fp) < 4) return -1; + + + fprintf(stderr, "sample_rate: %d\n", sample_rate); + fprintf(stderr, "bits : %d\n", bits_sample); + fprintf(stderr, "channels : %d\n", channels); + + if ((bits_sample != 8) && (bits_sample != 16)) return -1; + + samples_per_bit = sample_rate/(float)BAUD_RATE; + + fprintf(stderr, "samples/bit: %.2f\n", samples_per_bit); + + return 0; +} + + +#define EOF_INT 0x1000000 + +int read_signed_sample(FILE *fp) { // int = i32_t + int byte, i, ret; // EOF -> 0x1000000 + + for (i = 0; i < channels; i++) { + // i = 0: links bzw. mono + byte = fgetc(fp); + if (byte == EOF) return EOF_INT; + if (i == 0) ret = byte; + + if (bits_sample == 16) { + byte = fgetc(fp); + if (byte == EOF) return EOF_INT; + if (i == 0) ret += byte << 8; + } + + } + + if (bits_sample == 8) return ret-128; // 8bit: 00..FF, centerpoint 0x80=128 + if (bits_sample == 16) return (short)ret; + + return ret; +} + +unsigned long sample_count = 0; +int wlen; +int *sample_buff = NULL; + +int read_filter_sample(FILE *fp) { + int i; // wenn sample_buff[] ein 8N1-byte umfasst, + int s0, s, y; // mit (max+min)/2 Mittelwert bestimmen; + static int min, max; // Glaettung durch lowpass/moving average empfohlen + + s = read_signed_sample(fp); + if (s == EOF_INT) return EOF_INT; + + s0 = sample_buff[sample_count % wlen]; + sample_buff[sample_count % wlen] = s; + + y = 0; + if (sample_count > wlen-1) { + + if (s < min) min = s; + else { + if (s0 <= min) { + min = sample_buff[0]; + for (i = 1; i < wlen; i++) { + if (sample_buff[i] < min) min = sample_buff[i]; + } + } + } + + if (s > max) max = s; + else { + if (s0 >= max) { + max = sample_buff[0]; + for (i = 1; i < wlen; i++) { + if (sample_buff[i] > max) max = sample_buff[i]; + } + } + } + + y = sample_buff[(sample_count+wlen-1)%wlen] - (min+max)/2; + + } + else if (sample_count == wlen-1) { + min = sample_buff[0]; + max = sample_buff[0]; + for (i = 1; i < wlen; i++) { + if (sample_buff[i] < min) min = sample_buff[i]; + if (sample_buff[i] > max) max = sample_buff[i]; + } + y = sample_buff[(sample_count+wlen-1)%wlen] - (min+max)/2; + } + + sample_count++; + + return y; +} + +int par=1, par_alt=1; + +int read_bits_fsk(FILE *fp, int *bit, int *len) { + static int sample; + int n, y0; + float l, x1; + static float x0; + + n = 0; + do{ + y0 = sample; + + if (option_dc) sample = read_filter_sample(fp); + else sample = read_signed_sample(fp); + + if (sample == EOF_INT) return EOF; + //sample_count++; + par_alt = par; + par = (sample >= 0) ? 1 : -1; // 8bit: 0..127,128..255 (-128..-1,0..127) + n++; + } while (par*par_alt > 0); + + if (!option_res) l = (float)n / samples_per_bit; + else { // genauere Bitlaengen-Messung + x1 = sample/(float)(sample-y0); // hilft bei niedriger sample rate + l = (n+x0-x1) / samples_per_bit; // meist mehr frames (nicht immer) + x0 = x1; + } + + *len = (int)(l+0.5); + + if (!option_inv) *bit = (1+par_alt)/2; // oben 1, unten -1 + else *bit = (1-par_alt)/2; // sdr# (pos_GPSdate+7)*BITS) { + for (i = pos; i < BITFRAME_LEN; i++) frame_bits[i] = 0x30 + 0; + print_frame(pos);//byte_count + header_found = 0; + pos = FRAMESTART; + } + //inc_bufpos(); + //buf[bufpos] = 'x'; + continue; // ... + } + + for (i = 0; i < len; i++) { + + inc_bufpos(); + buf[bufpos] = 0x30 + bit; // Ascii + + if (!header_found) { + header_found = compare2(); + //if (header_found) fprintf(stdout, "[%c] ", header_found>0?'+':'-'); + if (header_found < 0) option_inv ^= 0x1; + // printf("[%c] ", option_inv?'-':'+'); + } + else { + frame_bits[pos] = 0x30 + bit; // Ascii + pos++; + + if (pos == BITFRAME_LEN) { + print_frame(pos);//FRAME_LEN + header_found = 0; + pos = FRAMESTART; + } + } + + } + } + + printf("\n"); + + fclose(fp); + if (option_dc) free(sample_buff); + + return 0; +} +