read float32 samples

pull/18/head
Zilog80 2019-08-28 00:13:45 +02:00
rodzic 2c7c60d428
commit 7d899ca410
6 zmienionych plików z 155 dodań i 58 usunięć

Wyświetl plik

@ -267,7 +267,7 @@ float read_wav_header(pcm_t *pcm, FILE *fp) {
if (pcm->sel_ch < 0 || pcm->sel_ch >= channels) pcm->sel_ch = 0; // default channel: 0
//fprintf(stderr, "channel-In : %d\n", pcm->sel_ch+1); // nur wenn nicht IQ
if ((bits_sample != 8) && (bits_sample != 16)) return -1;
if (bits_sample != 8 && bits_sample != 16 && bits_sample != 32) return -1;
pcm->sr = sample_rate;
@ -280,19 +280,26 @@ float read_wav_header(pcm_t *pcm, FILE *fp) {
static int f32read_sample(dsp_t *dsp, float *s) {
int i;
short b = 0;
unsigned int word = 0;
short *b = (short*)&word;
float *f = (float*)&word;
for (i = 0; i < dsp->nch; i++) {
if (fread( &b, dsp->bps/8, 1, dsp->fp) != 1) return EOF;
if (fread( &word, dsp->bps/8, 1, dsp->fp) != 1) return EOF;
if (i == dsp->ch) { // i = 0: links bzw. mono
//if (bits_sample == 8) sint = b-128; // 8bit: 00..FF, centerpoint 0x80=128
//if (bits_sample == 16) sint = (short)b;
if (dsp->bps == 8) { b -= 128; }
*s = b/128.0;
if (dsp->bps == 16) { *s /= 256.0; }
if (dsp->bps == 32) {
*s = *f;
}
else {
if (dsp->bps == 8) { *b -= 128; }
*s = *b/128.0;
if (dsp->bps == 16) { *s /= 256.0; }
}
}
}
@ -300,16 +307,27 @@ static int f32read_sample(dsp_t *dsp, float *s) {
}
static int f32read_csample(dsp_t *dsp, float complex *z) {
short x = 0, y = 0;
if (fread( &x, dsp->bps/8, 1, dsp->fp) != 1) return EOF;
if (fread( &y, dsp->bps/8, 1, dsp->fp) != 1) return EOF;
if (dsp->bps == 32) {
float x = 0, y = 0;
*z = x + I*y;
if (fread( &x, dsp->bps/8, 1, dsp->fp) != 1) return EOF;
if (fread( &y, dsp->bps/8, 1, dsp->fp) != 1) return EOF;
if (dsp->bps == 8) { *z -= 128 + I*128; }
*z /= 128.0;
if (dsp->bps == 16) { *z /= 256.0; }
*z = x + I*y;
}
else { // dsp->bps == 8,16
short a = 0, b = 0;
if (fread( &a, dsp->bps/8, 1, dsp->fp) != 1) return EOF;
if (fread( &b, dsp->bps/8, 1, dsp->fp) != 1) return EOF;
*z = a + I*b;
if (dsp->bps == 8) { *z -= 128 + I*128; }
*z /= 128.0;
if (dsp->bps == 16) { *z /= 256.0; }
}
return 0;
}
@ -321,16 +339,21 @@ static int f32read_cblock(dsp_t *dsp) {
len = dsp->decM;
if (dsp->bps == 8) {
if (dsp->bps == 8) { //uint8
ui8_t u[2*dsp->decM];
len = fread( u, dsp->bps/8, 2*dsp->decM, dsp->fp) / 2;
for (n = 0; n < len; n++) dsp->decMbuf[n] = (u[2*n]-128)/128.0 + I*(u[2*n+1]-128)/128.0;
}
else { // dsp->bps == 16
else if (dsp->bps == 16) { //int16
short b[2*dsp->decM];
len = fread( b, dsp->bps/8, 2*dsp->decM, dsp->fp) / 2;
for (n = 0; n < len; n++) dsp->decMbuf[n] = b[2*n]/32768.0 + I*b[2*n+1]/32768.0;
}
else { // dsp->bps == 32 //float32
float f[2*dsp->decM];
len = fread( f, dsp->bps/8, 2*dsp->decM, dsp->fp) / 2;
for (n = 0; n < len; n++) dsp->decMbuf[n] = f[2*n] + I*f[2*n+1];
}
return len;
}

Wyświetl plik

@ -268,7 +268,7 @@ float read_wav_header(pcm_t *pcm) {
if (pcm->sel_ch < 0 || pcm->sel_ch >= channels) pcm->sel_ch = 0; // default channel: 0
//fprintf(stderr, "channel-In : %d\n", pcm->sel_ch+1);
if ((bits_sample != 8) && (bits_sample != 16)) return -1;
if (bits_sample != 8 && bits_sample != 16 && bits_sample != 32) return -1;
pcm->sr = sample_rate;
@ -280,19 +280,26 @@ float read_wav_header(pcm_t *pcm) {
static int f32read_sample(dsp_t *dsp, float *s) {
int i;
short b = 0;
unsigned int word = 0;
short *b = (short*)&word;
float *f = (float*)&word;
for (i = 0; i < dsp->nch; i++) {
if (fread( &b, dsp->bps/8, 1, dsp->fp) != 1) return EOF;
if (fread( &word, dsp->bps/8, 1, dsp->fp) != 1) return EOF;
if (i == dsp->ch) { // i = 0: links bzw. mono
//if (bits_sample == 8) sint = b-128; // 8bit: 00..FF, centerpoint 0x80=128
//if (bits_sample == 16) sint = (short)b;
if (dsp->bps == 8) { b -= 128; }
*s = b/128.0;
if (dsp->bps == 16) { *s /= 256.0; }
if (dsp->bps == 32) {
*s = *f;
}
else {
if (dsp->bps == 8) { *b -= 128; }
*s = *b/128.0;
if (dsp->bps == 16) { *s /= 256.0; }
}
}
}
@ -301,16 +308,27 @@ static int f32read_sample(dsp_t *dsp, float *s) {
static int f32read_csample(dsp_t *dsp, float complex *z) {
short x = 0, y = 0;
if (fread( &x, dsp->bps/8, 1, dsp->fp) != 1) return EOF;
if (fread( &y, dsp->bps/8, 1, dsp->fp) != 1) return EOF;
if (dsp->bps == 32) {
float x = 0, y = 0;
*z = x + I*y;
if (fread( &x, dsp->bps/8, 1, dsp->fp) != 1) return EOF;
if (fread( &y, dsp->bps/8, 1, dsp->fp) != 1) return EOF;
if (dsp->bps == 8) { *z -= 128 + I*128; }
*z /= 128.0;
if (dsp->bps == 16) { *z /= 256.0; }
*z = x + I*y;
}
else { // dsp->bps == 8,16
short a = 0, b = 0;
if (fread( &a, dsp->bps/8, 1, dsp->fp) != 1) return EOF;
if (fread( &b, dsp->bps/8, 1, dsp->fp) != 1) return EOF;
*z = a + I*b;
if (dsp->bps == 8) { *z -= 128 + I*128; }
*z /= 128.0;
if (dsp->bps == 16) { *z /= 256.0; }
}
return 0;
}
@ -333,16 +351,21 @@ static int __f32read_cblock_nocond(dsp_t *dsp) { // blk
if (rbf == 0)
{
if (dsp->bps == 8) {
if (dsp->bps == 8) { //uint8
ui8_t u[2*BL];
len = fread( u, dsp->bps/8, 2*BL, dsp->fp) / 2;
for (n = 0; n < len; n++) dsp->thd.blk[n] = (u[2*n]-128)/128.0 + I*(u[2*n+1]-128)/128.0;
}
else { // dsp->bps == 16
else if (dsp->bps == 16) { //int16
short b[2*BL];
len = fread( b, dsp->bps/8, 2*BL, dsp->fp) / 2;
for (n = 0; n < len; n++) dsp->thd.blk[n] = b[2*n]/32768.0 + I*b[2*n+1]/32768.0;
}
else { // dsp->bps == 32 //float32
float f[2*BL];
len = fread( f, dsp->bps/8, 2*BL, dsp->fp) / 2;
for (n = 0; n < len; n++) dsp->thd.blk[n] = f[2*n] + I*f[2*n+1];
}
if (len < BL) bufeof = 1;
rbf = rbf1; // set all bits
@ -376,16 +399,21 @@ static int f32read_cblock(dsp_t *dsp) { // blk_cond
if (rbf == 0)
{
if (dsp->bps == 8) {
if (dsp->bps == 8) { //uint8
ui8_t u[2*BL];
len = fread( u, dsp->bps/8, 2*BL, dsp->fp) / 2;
for (n = 0; n < len; n++) dsp->thd.blk[n] = (u[2*n]-128)/128.0 + I*(u[2*n+1]-128)/128.0;
}
else { // dsp->bps == 16
else if (dsp->bps == 16) { //int16
short b[2*BL];
len = fread( b, dsp->bps/8, 2*BL, dsp->fp) / 2;
for (n = 0; n < len; n++) dsp->thd.blk[n] = b[2*n]/32768.0 + I*b[2*n+1]/32768.0;
}
else { // dsp->bps == 32 //float32
float f[2*BL];
len = fread( f, dsp->bps/8, 2*BL, dsp->fp) / 2;
for (n = 0; n < len; n++) dsp->thd.blk[n] = f[2*n] + I*f[2*n+1];
}
if (len < BL) bufeof = 1;
rbf = rbf1; // set all bits
@ -836,7 +864,7 @@ int init_buffers(dsp_t *dsp) {
if (dsp->opt_iq == 5)
{
// look up table, exp-rotation
// lookup table, exp-rotation
int W = 2*8; // 16 Hz window
int d = 1; // 1..W , groesster Teiler d <= W von sr_base
int freq = (int)( dsp->thd.xlt_fq * (double)dsp->sr_base + 0.5);

Wyświetl plik

@ -149,7 +149,7 @@ int main(int argc, char **argv) {
++argv;
if (*argv) bits_sample = atoi(*argv); else return -1;
channels = 2;
if (sample_rate < 1 || (bits_sample != 8 && bits_sample != 16 /*&&bits_sample!=32*/)) {
if (sample_rate < 1 || (bits_sample != 8 && bits_sample != 16 && bits_sample != 32)) {
fprintf(stderr, "- <sr> <bs>\n");
return -1;
}

Wyświetl plik

@ -381,26 +381,33 @@ static int read_wav_header(FILE *fp, int wav_channel) {
else wav_ch = 0;
//fprintf(stderr, "channel-In : %d\n", wav_ch+1);
if ((bits_sample != 8) && (bits_sample != 16)) return -1;
if (bits_sample != 8 && bits_sample != 16 && bits_sample != 32) return -1;
return 0;
}
static int f32read_sample(FILE *fp, float *s) {
int i;
short b = 0;
unsigned int word = 0;
short *b = (short*)&word;
float *f = (float*)&word;
for (i = 0; i < channels; i++) {
if (fread( &b, bits_sample/8, 1, fp) != 1) return EOF;
if (fread( &word, bits_sample/8, 1, fp) != 1) return EOF;
if (i == wav_ch) { // i = 0: links bzw. mono
//if (bits_sample == 8) sint = b-128; // 8bit: 00..FF, centerpoint 0x80=128
//if (bits_sample == 16) sint = (short)b;
if (bits_sample == 8) { b -= 128; }
*s = b/128.0;
if (bits_sample == 16) { *s /= 256.0; }
if (bits_sample == 32) {
*s = *f;
}
else {
if (bits_sample == 8) { *b -= 128; }
*s = *b/128.0;
if (bits_sample == 16) { *s /= 256.0; }
}
}
}
@ -408,16 +415,27 @@ static int f32read_sample(FILE *fp, float *s) {
}
static int f32read_csample(FILE *fp, float complex *z) {
short x = 0, y = 0;
if (fread( &x, bits_sample/8, 1, fp) != 1) return EOF;
if (fread( &y, bits_sample/8, 1, fp) != 1) return EOF;
if (bits_sample == 32) {
float x = 0, y = 0;
*z = x + I*y;
if (fread( &x, bits_sample/8, 1, fp) != 1) return EOF;
if (fread( &y, bits_sample/8, 1, fp) != 1) return EOF;
if (bits_sample == 8) { *z -= 128 + I*128; }
*z /= 128.0;
if (bits_sample == 16) { *z /= 256.0; }
*z = x + I*y;
}
else { // bits_sample == 8,16
short a = 0, b = 0;
if (fread( &a, bits_sample/8, 1, fp) != 1) return EOF;
if (fread( &b, bits_sample/8, 1, fp) != 1) return EOF;
*z = a + I*b;
if (bits_sample == 8) { *z -= 128 + I*128; }
*z /= 128.0;
if (bits_sample == 16) { *z /= 256.0; }
}
return 0;
}
@ -447,11 +465,16 @@ static int f32read_cblock(FILE *fp) {
len = fread( u, bits_sample/8, 2*dsp__decM, fp) / 2;
for (n = 0; n < len; n++) dsp__decMbuf[n] = (u[2*n]-128)/128.0 + I*(u[2*n+1]-128)/128.0;
}
else { // bits_sample == 16
else if (bits_sample == 16) { // bits_sample == 16
short b[2*dsp__decM];
len = fread( b, bits_sample/8, 2*dsp__decM, fp) / 2;
for (n = 0; n < len; n++) dsp__decMbuf[n] = b[2*n]/32768.0 + I*b[2*n+1]/32768.0;
}
else { // bits_sample == 32 //float32
float f[2*dsp__decM];
len = fread( f, bits_sample/8, 2*dsp__decM, fp) / 2;
for (n = 0; n < len; n++) dsp__decMbuf[n] = f[2*n] + I*f[2*n+1];
}
return len;
}

Wyświetl plik

@ -167,8 +167,9 @@ static void db_power(dft_t *dft, float complex Z[], float db[]) { // iq-samples
static int init_dft(dft_t *dft) {
int i, k, n;
float normM = 0;
int bytes_sample = bits_sample/8;
bufIQ = calloc(2*(dft->N+2), 2); if (bufIQ == NULL) return -1;
bufIQ = calloc(2*(dft->N+2), bytes_sample); if (bufIQ == NULL) return -1;
buffer = calloc(dft->N+1, sizeof(float complex)); if (buffer == NULL) return -1;
dft->xn = calloc(dft->N+1, sizeof(float complex)); if (dft->xn == NULL) return -1;
@ -275,7 +276,7 @@ static int read_wav_header(FILE *fp, int wav_channel) {
else wav_ch = 0;
fprintf(stderr, "channel-In : %d\n", wav_ch+1);
if ((bits_sample != 8) && (bits_sample != 16)) return -1;
if (bits_sample != 8 && bits_sample != 16 && bits_sample != 32) return -1;
return 0;
}
@ -302,8 +303,9 @@ static int bufIQ2complex(dft_t *dft) {
float complex z;
unsigned char *buf8;
short *buf16;
float *buf32;
if (bits_sample == 8) {
if (bits_sample == 8) {
buf8 = bufIQ;
for (i = 0; i < dft->N2; i++) {
z = buf8[2*i]-128.0 + I*(buf8[2*i+1]-128.0);
@ -311,7 +313,7 @@ static int bufIQ2complex(dft_t *dft) {
buffer[i] = z;
}
}
else { // bits_sample == 16
else if (bits_sample == 16) {
buf16 = bufIQ;
for (i = 0; i < dft->N2; i++) {
z = buf16[2*i] + I*buf16[2*i+1];
@ -319,25 +321,39 @@ static int bufIQ2complex(dft_t *dft) {
buffer[i] = z;
}
}
else { // bits_sample == 32
buf32 = bufIQ;
for (i = 0; i < dft->N2; i++) {
z = buf32[2*i] + I*buf32[2*i+1];
buffer[i] = z;
}
}
return 0;
}
static int f32read_sample(FILE *fp, float *s) {
int i;
short b = 0;
unsigned int word = 0;
short *b = (short*)&word;
float *f = (float*)&word;
for (i = 0; i < channels; i++) {
if (fread( &b, bits_sample/8, 1, fp) != 1) return EOF;
if (fread( &word, bits_sample/8, 1, fp) != 1) return EOF;
if (i == wav_ch) { // i = 0: links bzw. mono
//if (bits_sample == 8) sint = b-128; // 8bit: 00..FF, centerpoint 0x80=128
//if (bits_sample == 16) sint = (short)b;
if (bits_sample == 8) { b -= 128; }
*s = b/128.0;
if (bits_sample == 16) { *s /= 256.0; }
if (bits_sample == 32) {
*s = *f;
}
else {
if (bits_sample == 8) { *b -= 128; }
*s = *b/128.0;
if (bits_sample == 16) { *s /= 256.0; }
}
}
}
@ -433,6 +449,13 @@ int main(int argc, char **argv) {
}
}
// read_wav_header() == -1
if (bits_sample != 8 && bits_sample != 16 && bits_sample != 32) {
fclose(fp);
fprintf(stderr, "error: bits/sample\n");
return -1;
}
DFT.sr = sample_rate;
DFT.LOG2N = 14; // 2^12=4096: 300-400Hz bins, 2^14=16384: 75-100 Hz
mn = 0;

Wyświetl plik

@ -272,7 +272,7 @@ static int read_wav_header(FILE *fp, int wav_channel) {
else wav_ch = 0;
fprintf(stderr, "channel-In : %d\n", wav_ch+1);
if ((bits_sample != 8) && (bits_sample != 16)) return -1;
if (bits_sample != 8 && bits_sample != 16 && bits_sample != 32) return -1;
return 0;
}