Add files via upload

master
Wojciech Kaczmarski 2020-09-20 14:41:47 +02:00 committed by GitHub
parent e5dd1108e0
commit 252da45c4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 179 additions and 676 deletions

855
dump.c
View File

@ -1,676 +1,179 @@
/*---------------------------------------------------------------------------*\ //as usual...
#include <stdio.h>
FILE........: dump.c #include <stdlib.h>
AUTHOR......: David Rowe #include <stdint.h>
DATE CREATED: 25/8/09 #include <string.h>
#include <math.h>
Routines to dump data to text files for Octave analysis.
//internet
\*---------------------------------------------------------------------------*/ #include <arpa/inet.h>
#include <sys/socket.h>
/*
All rights reserved. //port
uint16_t port_num=17000; //default port
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License version 2.1, as //UDP packet
published by the Free Software Foundation. This program is uint8_t bits[54];
distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or //internet
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public struct sockaddr_in si_me, si_other;
License for more details. int s, i, slen = sizeof(si_other) , rcv_len;
You should have received a copy of the GNU Lesser General Public License //CRC
along with this program; if not, see <http://www.gnu.org/licenses/>. uint16_t CRC_LUT[256];
*/ uint16_t poly=0x5935;
#include "defines.h" //test
#include "comp.h" //FILE *fp;
#include "dump.h"
#include <assert.h> //stream info
#include <stdlib.h> struct moip_packet
#include <stdio.h> {
#include <string.h> uint16_t sid;
#include <math.h> uint8_t src[10];
uint8_t dst[10];
#ifdef __EMBEDDED__ uint16_t type;
#include "gdb_stdio.h" uint8_t nonce[14];
#define fprintf gdb_stdio_fprintf uint16_t fn;
#define fopen gdb_stdio_fopen uint8_t payload[16];
#define fclose gdb_stdio_fclose uint16_t crc_udp;
#endif } packet;
#ifdef DUMP void CRC_Init(uint16_t *crc_table, uint16_t poly)
static int dumpon = 0; {
uint16_t remainder;
static FILE *fsn = NULL;
static FILE *fsw = NULL; for(uint16_t dividend=0; dividend<256; dividend++)
static FILE *few = NULL; {
static FILE *fsw_ = NULL; remainder=dividend<<8;
static FILE *fsoftdec = NULL;
static FILE *fmodel = NULL; for(uint8_t bit=8; bit>0; bit--)
static FILE *fqmodel = NULL; {
static FILE *fpwb = NULL; if(remainder&(1<<15))
static FILE *fpw = NULL; remainder=(remainder<<1)^poly;
static FILE *frw = NULL; else
static FILE *flsp = NULL; remainder=(remainder<<1);
static FILE *fweights = NULL; }
static FILE *flsp_ = NULL;
static FILE *fmel = NULL; crc_table[dividend]=remainder;
static FILE *fmel_indexes = NULL; }
static FILE *fphase = NULL; }
static FILE *fphase_ = NULL;
static FILE *ffw = NULL; uint16_t CRC_M17(uint16_t* crc_table, const uint8_t* message, uint16_t nBytes)
static FILE *fe = NULL; {
static FILE *fsq = NULL; uint8_t data;
static FILE *fdec = NULL; uint16_t remainder=0xFFFF;
static FILE *fsnr = NULL;
static FILE *flpcsnr = NULL; for(uint16_t byte=0; byte<nBytes; byte++)
static FILE *fak = NULL; {
static FILE *fak_ = NULL; data=message[byte]^(remainder>>8);
static FILE *fbg = NULL; remainder=crc_table[data]^(remainder<<8);
static FILE *fE = NULL; }
static FILE *frk = NULL;
static FILE *fhephase = NULL; return(remainder);
}
static char prefix[MAX_STR];
uint8_t* decode_callsign_base40(uint64_t encoded, uint8_t *callsign)
void dump_on(char p[]) { {
dumpon = 1; if(encoded >= 262144000000000)
strcpy(prefix, p); {
} *callsign=0;
return callsign;
void dump_off(){ }
if (fsn != NULL)
fclose(fsn); uint8_t *p = callsign;
if (fsw != NULL)
fclose(fsw); for (; encoded>0; p++)
if (fsw_ != NULL) {
fclose(fsw_); *p = "xABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-/."[encoded % 40];
if (few != NULL) encoded/=40;
fclose(few); }
if (fmodel != NULL)
fclose(fmodel); *p = 0;
if (fsoftdec != NULL)
fclose(fsoftdec); return callsign;
if (fqmodel != NULL) }
fclose(fqmodel);
if (fpwb != NULL) // ./this.out port
fclose(fpwb); int main(int argc, char *argv[])
if (fpw != NULL) {
fclose(fpw); if(argc==2)
if (frw != NULL) {
fclose(frw); port_num=atoi(argv[1]);
if (flsp != NULL) }
fclose(flsp); else
if (fweights != NULL) {
fclose(fweights); fprintf(stderr, "Not enough args\n");
if (flsp_ != NULL) return 1;
fclose(flsp_); }
if (fmel != NULL)
fclose(fmel); //init
if (fmel_indexes != NULL) memset(bits, 0, 54);
fclose(fmel_indexes); CRC_Init(CRC_LUT, poly);
if (fphase != NULL)
fclose(fphase); s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (fphase_ != NULL)
fclose(fphase_); if(s==-1)
if (ffw != NULL) {
fclose(ffw); fprintf(stderr, "Socket error\n");
if (fe != NULL) return 1;
fclose(fe); }
if (fsq != NULL)
fclose(fsq); //zero out the structure
if (fdec != NULL) memset((char*)&si_me, 0, sizeof(si_me));
fclose(fdec);
if (fsnr != NULL) si_me.sin_family = AF_INET;
fclose(fsnr); si_me.sin_port = htons(port_num);
if (flpcsnr != NULL) si_me.sin_addr.s_addr = htonl(INADDR_ANY);
fclose(flpcsnr);
if (fak != NULL) if(bind(s, (struct sockaddr*)&si_me, sizeof(si_me))==-1)
fclose(fak); {
if (fak_ != NULL) fprintf(stderr, "Can't bind to port %d\n", port_num);
fclose(fak_); return 1;
if (fbg != NULL) }
fclose(fbg);
if (fE != NULL) printf("Listening for M17 frames on port %d:\n", port_num);
fclose(fE); printf("Src\t\tDst\t\tType\tPld\n");
if (frk != NULL)
fclose(frk); while(1)
if (fhephase != NULL) {
fclose(fhephase); //receive packet via UDP
} if((rcv_len=recvfrom(s, bits, 54, 0, (struct sockaddr*)&si_other, &slen))==-1)
{
void dump_Sn(int m_pitch, float Sn[]) { fprintf(stderr, "What the hell?\n");
int i; return 1;
char s[MAX_STR]; }
if (!dumpon) return; if(rcv_len==54)
{
if (fsn == NULL) { packet.sid=(bits[4]<<8)|bits[5];
sprintf(s,"%s_sn.txt", prefix); uint64_t tmp;
fsn = fopen(s, "wt"); tmp=(bits[6]<<(5*8))|(bits[7]<<(4*8))|(bits[8]<<(3*8))|(bits[9]<<(2*8))|(bits[10]<<(1*8))|bits[11];
assert(fsn != NULL); decode_callsign_base40(tmp, packet.src);
} tmp=(bits[12]<<(5*8))|(bits[13]<<(4*8))|(bits[14]<<(3*8))|(bits[15]<<(2*8))|(bits[16]<<(1*8))|bits[17];
decode_callsign_base40(tmp, packet.dst);
/* split across two lines to avoid max line length problems */ packet.type=(bits[18]<<8)|bits[19];
/* reconstruct in Octave */
memcpy(packet.nonce, &bits[20], 14);
for(i=0; i<m_pitch/2; i++) packet.fn=bits[34]<<8|bits[35];
fprintf(fsn,"%f\t",Sn[i]); memcpy(packet.payload, &bits[36], 16);
fprintf(fsn,"\n"); packet.crc_udp=bits[52]<<8|bits[53];
for(i=m_pitch/2; i<m_pitch; i++)
fprintf(fsn,"%f\t",Sn[i]); //info
fprintf(fsn,"\n"); printf("%s\t\t%s\t\t%04X\t", packet.src, packet.dst, packet.type);
} for(uint8_t i=0; i<128/8; i++)
{
void dump_Sw(COMP Sw[]) { printf("%02X", packet.payload[i]);
int i; if(i==(128/8-1))
char s[MAX_STR]; printf("\n");
}
if (!dumpon) return;
/*fp=fopen("out.raw", "a");
if (fsw == NULL) { fwrite(speech_buff, 2, 160, fp);
sprintf(s,"%s_sw.txt", prefix); fclose(fp);*/
fsw = fopen(s, "wt"); }
assert(fsw != NULL); }
}
for(i=0; i<FFT_ENC/2; i++) return 0;
fprintf(fsw,"%f\t", }
10.0*log10(Sw[i].real*Sw[i].real + Sw[i].imag*Sw[i].imag));
fprintf(fsw,"\n");
}
void dump_Sw_(COMP Sw_[]) {
int i;
char s[MAX_STR];
if (!dumpon) return;
if (fsw_ == NULL) {
sprintf(s,"%s_sw_.txt", prefix);
fsw_ = fopen(s, "wt");
assert(fsw_ != NULL);
}
for(i=0; i<FFT_ENC/2; i++)
fprintf(fsw_,"%f\t",
10.0*log10(Sw_[i].real*Sw_[i].real + Sw_[i].imag*Sw_[i].imag));
fprintf(fsw_,"\n");
}
void dump_Ew(COMP Ew[]) {
int i;
char s[MAX_STR];
if (!dumpon) return;
if (few == NULL) {
sprintf(s,"%s_ew.txt", prefix);
few = fopen(s, "wt");
assert(few != NULL);
}
for(i=0; i<FFT_ENC/2; i++)
fprintf(few,"%f\t",
10.0*log10(Ew[i].real*Ew[i].real + Ew[i].imag*Ew[i].imag));
fprintf(few,"\n");
}
void dump_softdec(float *softdec, int n)
{
int i;
char s[MAX_STR];
if (!dumpon) return;
if (fsoftdec == NULL) {
sprintf(s,"%s_softdec.txt", prefix);
fsoftdec = fopen(s, "wt");
assert(fsoftdec != NULL);
}
for(i=0; i<n; i++)
fprintf(fsoftdec,"%f\t", softdec[i]);
fprintf(fsoftdec,"\n");
}
void dump_model(MODEL *model) {
int l;
char s[MAX_STR];
char line[MAX_STR*10];
if (!dumpon) return;
if (fmodel == NULL) {
sprintf(s,"%s_model.txt", prefix);
fmodel = fopen(s, "wt");
assert(fmodel != NULL);
}
sprintf(line,"%12f %12d ", model->Wo, model->L);
for(l=1; l<=model->L; l++) {
sprintf(s,"%12f ",model->A[l]);
strcat(line, s);
assert(strlen(line) < MAX_STR*10);
}
for(l=model->L+1; l<=MAX_AMP; l++) {
sprintf(s,"%12f ", 0.0);
strcat(line,s);
assert(strlen(line) < MAX_STR*10);
}
sprintf(s,"%d\n",model->voiced);
strcat(line,s);
fprintf(fmodel,"%s",line);
}
void dump_quantised_model(MODEL *model) {
int l;
char s[MAX_STR];
char line[4096];
if (!dumpon) return;
if (fqmodel == NULL) {
sprintf(s,"%s_qmodel.txt", prefix);
fqmodel = fopen(s, "wt");
assert(fqmodel != NULL);
}
sprintf(line,"%12f %12d ", model->Wo, model->L);
for(l=1; l<=model->L; l++) {
sprintf(s,"%12f ",model->A[l]);
strcat(line, s);
assert(strlen(line) < 4096);
}
for(l=model->L+1; l<=MAX_AMP; l++) {
sprintf(s,"%12f ", 0.0);
strcat(line, s);
assert(strlen(line) < 4096);
}
sprintf(s,"%d\n",model->voiced);
strcat(line, s);
fprintf(fqmodel, "%s", line);
}
void dump_phase(float phase[], int L) {
int l;
char s[MAX_STR];
if (!dumpon) return;
if (fphase == NULL) {
sprintf(s,"%s_phase.txt", prefix);
fphase = fopen(s, "wt");
assert(fphase != NULL);
}
for(l=1; l<=L; l++)
fprintf(fphase,"%f\t",phase[l]);
for(l=L+1; l<=MAX_AMP; l++)
fprintf(fphase,"%f\t",0.0);
fprintf(fphase,"\n");
}
void dump_phase_(float phase_[], int L) {
int l;
char s[MAX_STR];
if (!dumpon) return;
if (fphase_ == NULL) {
sprintf(s,"%s_phase_.txt", prefix);
fphase_ = fopen(s, "wt");
assert(fphase_ != NULL);
}
for(l=1; l<=L; l++)
fprintf(fphase_,"%f\t",phase_[l]);
for(l=L+1; l<MAX_AMP; l++)
fprintf(fphase_,"%f\t",0.0);
fprintf(fphase_,"\n");
}
void dump_hephase(int ind[], int dim) {
int m;
char s[MAX_STR];
if (!dumpon) return;
if (fhephase == NULL) {
sprintf(s,"%s_hephase.txt", prefix);
fhephase = fopen(s, "wt");
assert(fhephase != NULL);
}
for(m=0; m<dim; m++)
fprintf(fhephase,"%d\t",ind[m]);
fprintf(fhephase,"\n");
}
void dump_snr(float snr) {
char s[MAX_STR];
if (!dumpon) return;
if (fsnr == NULL) {
sprintf(s,"%s_snr.txt", prefix);
fsnr = fopen(s, "wt");
assert(fsnr != NULL);
}
fprintf(fsnr,"%f\n",snr);
}
void dump_lpc_snr(float snr) {
char s[MAX_STR];
if (!dumpon) return;
if (flpcsnr == NULL) {
sprintf(s,"%s_lpc_snr.txt", prefix);
flpcsnr = fopen(s, "wt");
assert(flpcsnr != NULL);
}
fprintf(flpcsnr,"%f\n",snr);
}
/* Pw "before" post filter so we can plot before and after */
void dump_Pwb(float Pwb[]) {
int i;
char s[MAX_STR];
if (!dumpon) return;
if (fpwb == NULL) {
sprintf(s,"%s_pwb.txt", prefix);
fpwb = fopen(s, "wt");
assert(fpwb != NULL);
}
for(i=0; i<FFT_ENC/2; i++)
fprintf(fpwb,"%f\t",Pwb[i]);
fprintf(fpwb,"\n");
}
void dump_Pw(float Pw[]) {
int i;
char s[MAX_STR];
if (!dumpon) return;
if (fpw == NULL) {
sprintf(s,"%s_pw.txt", prefix);
fpw = fopen(s, "wt");
assert(fpw != NULL);
}
for(i=0; i<FFT_ENC/2; i++)
fprintf(fpw,"%f\t",Pw[i]);
fprintf(fpw,"\n");
}
void dump_Rw(float Rw[]) {
int i;
char s[MAX_STR];
if (!dumpon) return;
if (frw == NULL) {
sprintf(s,"%s_rw.txt", prefix);
frw = fopen(s, "wt");
assert(frw != NULL);
}
for(i=0; i<FFT_ENC/2; i++)
fprintf(frw,"%f\t",Rw[i]);
fprintf(frw,"\n");
}
void dump_weights(float w[], int order) {
int i;
char s[MAX_STR];
if (!dumpon) return;
if (fweights == NULL) {
sprintf(s,"%s_weights.txt", prefix);
fweights = fopen(s, "wt");
assert(fweights != NULL);
}
for(i=0; i<order; i++)
fprintf(fweights,"%f\t", w[i]);
fprintf(fweights,"\n");
}
void dump_lsp(float lsp[]) {
int i;
char s[MAX_STR];
if (!dumpon) return;
if (flsp == NULL) {
sprintf(s,"%s_lsp.txt", prefix);
flsp = fopen(s, "wt");
assert(flsp != NULL);
}
for(i=0; i<10; i++)
fprintf(flsp,"%f\t",lsp[i]);
fprintf(flsp,"\n");
}
void dump_lsp_(float lsp_[]) {
int i;
char s[MAX_STR];
if (!dumpon) return;
if (flsp_ == NULL) {
sprintf(s,"%s_lsp_.txt", prefix);
flsp_ = fopen(s, "wt");
assert(flsp_ != NULL);
}
for(i=0; i<10; i++)
fprintf(flsp_,"%f\t",lsp_[i]);
fprintf(flsp_,"\n");
}
void dump_mel(float mel[], int order) {
int i;
char s[MAX_STR];
if (!dumpon) return;
if (fmel == NULL) {
sprintf(s,"%s_mel.txt", prefix);
fmel = fopen(s, "wt");
assert(fmel != NULL);
}
for(i=0; i<order; i++)
fprintf(fmel,"%f\t",mel[i]);
fprintf(fmel,"\n");
}
void dump_mel_indexes(int mel_indexes[], int order) {
int i;
char s[MAX_STR];
if (!dumpon) return;
if (fmel_indexes == NULL) {
sprintf(s,"%s_mel_indexes.txt", prefix);
fmel_indexes = fopen(s, "wt");
assert(fmel_indexes != NULL);
}
for(i=0; i<order; i++)
fprintf(fmel_indexes,"%d\t",mel_indexes[i]);
fprintf(fmel_indexes,"\n");
}
void dump_ak(float ak[], int order) {
int i;
char s[MAX_STR];
if (!dumpon) return;
if (fak == NULL) {
sprintf(s,"%s_ak.txt", prefix);
fak = fopen(s, "wt");
assert(fak != NULL);
}
for(i=0; i<=order; i++)
fprintf(fak,"%f\t",ak[i]);
fprintf(fak,"\n");
}
void dump_ak_(float ak_[], int order) {
int i;
char s[MAX_STR];
if (!dumpon) return;
if (fak_ == NULL) {
sprintf(s,"%s_ak_.txt", prefix);
fak_ = fopen(s, "wt");
assert(fak_ != NULL);
}
for(i=0; i<=order; i++)
fprintf(fak_,"%f\t",ak_[i]);
fprintf(fak_,"\n");
}
void dump_Fw(COMP Fw[]) {
int i;
char s[MAX_STR];
if (!dumpon) return;
if (ffw == NULL) {
sprintf(s,"%s_fw.txt", prefix);
ffw = fopen(s, "wt");
assert(ffw != NULL);
}
for(i=0; i<256; i++)
fprintf(ffw,"%f\t",Fw[i].real);
fprintf(ffw,"\n");
}
void dump_e(float e_hz[]) {
int i;
char s[MAX_STR];
if (!dumpon) return;
if (fe == NULL) {
sprintf(s,"%s_e.txt", prefix);
fe = fopen(s, "wt");
assert(fe != NULL);
}
for(i=0; i<500/2; i++)
fprintf(fe,"%f\t",e_hz[i]);
fprintf(fe,"\n");
for(i=500/2; i<500; i++)
fprintf(fe,"%f\t",e_hz[i]);
fprintf(fe,"\n");
}
void dump_sq(int m_pitch, float sq[]) {
int i;
char s[MAX_STR];
if (!dumpon) return;
if (fsq == NULL) {
sprintf(s,"%s_sq.txt", prefix);
fsq = fopen(s, "wt");
assert(fsq != NULL);
}
for(i=0; i<m_pitch/2; i++)
fprintf(fsq,"%f\t",sq[i]);
fprintf(fsq,"\n");
for(i=m_pitch/2; i<m_pitch; i++)
fprintf(fsq,"%f\t",sq[i]);
fprintf(fsq,"\n");
}
void dump_dec(COMP Fw[]) {
int i;
char s[MAX_STR];
if (!dumpon) return;
if (fdec == NULL) {
sprintf(s,"%s_dec.txt", prefix);
fdec = fopen(s, "wt");
assert(fdec != NULL);
}
for(i=0; i<320/5; i++)
fprintf(fdec,"%f\t",Fw[i].real);
fprintf(fdec,"\n");
}
void dump_bg(float e, float bg_est, float percent_uv) {
char s[MAX_STR];
if (!dumpon) return;
if (fbg == NULL) {
sprintf(s,"%s_bg.txt", prefix);
fbg = fopen(s, "wt");
assert(fbg != NULL);
}
fprintf(fbg,"%f\t%f\t%f\n", e, bg_est, percent_uv);
}
void dump_E(float E) {
char s[MAX_STR];
if (!dumpon) return;
if (fE == NULL) {
sprintf(s,"%s_E.txt", prefix);
fE = fopen(s, "wt");
assert(fE != NULL);
}
fprintf(fE,"%f\n", 10.0*log10(E));
}
#if 0
void dump_Rk(float Rk[]) {
int i;
char s[MAX_STR];
if (!dumpon) return;
if (frk == NULL) {
sprintf(s,"%s_rk.txt", prefix);
frk = fopen(s, "wt");
assert(frk != NULL);
}
for(i=0; i<P_MAX; i++)
fprintf(frk,"%f\t",Rk[i]);
fprintf(frk,"\n");
}
#endif
#endif