From 86f0996fabf3a6decbdec96da107916e023b32b7 Mon Sep 17 00:00:00 2001 From: Zilog80 Date: Sat, 3 Dec 2022 10:06:04 +0100 Subject: [PATCH] C50: speed-up --- c34/c50dft.c | 230 +++++++++++++++++---------------------------------- 1 file changed, 75 insertions(+), 155 deletions(-) diff --git a/c34/c50dft.c b/c34/c50dft.c index f39eb51..a9cfd5e 100644 --- a/c34/c50dft.c +++ b/c34/c50dft.c @@ -1,7 +1,7 @@ /* C50 - (empfohlen: sample rate 48kHz) + (recommended: sample rate 48kHz) gcc c50dft.c -lm -o c50dft */ @@ -32,7 +32,6 @@ typedef unsigned char ui8_t; static int option_verbose = 0, option_raw = 0, option_ptu = 0, - option_dft = 0, option_json = 0, wavloaded = 0; @@ -73,11 +72,13 @@ static int read_wav_header(FILE *fp) { int byte, p=0; if (fread(txt, 1, 4, fp) < 4) return -1; - if (strncmp(txt, "RIFF", 4)) return -1; + if (strncmp(txt, "RIFF", 4) && strncmp(txt, "RF64", 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; @@ -115,7 +116,9 @@ static int read_wav_header(FILE *fp) { fprintf(stderr, "bits : %d\n", bits_sample); fprintf(stderr, "channels : %d\n", channels); - if ((bits_sample != 8) && (bits_sample != 16)) return -1; + if ((bits_sample != 8) && (bits_sample != 16) && (bits_sample != 32)) return -1; + + if (sample_rate == 900001) sample_rate -= 1; //samples_per_bit = sample_rate/(float)BAUD_RATE; //fprintf(stderr, "samples/bit: %.2f\n", samples_per_bit); @@ -123,30 +126,33 @@ static int read_wav_header(FILE *fp) { return 0; } -#define EOF_INT 0x1000000 static unsigned int sample_count; -static int read_signed_sample(FILE *fp) { // int = i32_t - int byte, i, ret; // EOF -> 0x1000000 +static int f32read_sample(FILE *fp, float *s) { + int i; + unsigned int word = 0; + short *b = (short*)&word; + float *f = (float*)&word; 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 (fread( &word, bits_sample/8, 1, fp) != 1) return EOF; + + if (i == 0) { // 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 == 32) { + *s = *f; + } + else { + if (bits_sample == 8) { *b -= 128; } + *s = *b/(float)(1< max) { - max = cabs(Z[k]); - kmax = k; - } - } - - return kmax; -} - static float freq2bin(int f) { return f * N / (float)sample_rate; } @@ -521,15 +460,15 @@ int main(int argc, char *argv[]) { FILE *fp; char *fpname; - int sample; - int i, j, kmax, k0, k1; + int i, k0, k1; int bit = 8, bit0 = 8; int pos = 0, pos0 = 0; int header_found = 0; int bitlen; // sample_rate/BAUD_RATE int len; - float k_f0, k_f1, k_df; + float k_f0, k_f1; float cb0, cb1; + float s = 0.0; int cfreq = -1; #ifdef CYGWIN @@ -557,14 +496,7 @@ int main(int argc, char *argv[]) { else if ( (strcmp(*argv, "--ptu") == 0) ) { option_ptu = 1; } - else if ( (strcmp(*argv, "-d1") == 0) || (strcmp(*argv, "--dft1") == 0) ) { - option_dft = 1; - } - else if ( (strcmp(*argv, "-d2") == 0) || (strcmp(*argv, "--dft2") == 0) ) { - option_dft = 2; - } else if ( (strcmp(*argv, "--json") == 0) ) { - option_dft = 0; option_verbose = 1; option_json = 1; } @@ -578,7 +510,7 @@ int main(int argc, char *argv[]) { else { fp = fopen(*argv, "rb"); if (fp == NULL) { - fprintf(stderr, "%s konnte nicht geoeffnet werden\n", *argv); + fprintf(stderr, "error: open %s\n", *argv); return -1; } wavloaded = 1; @@ -596,83 +528,71 @@ int main(int argc, char *argv[]) { fclose(fp); return -1; } + if ( sample_rate != 48000 ) { + fprintf(stderr, "note: sample rate not 48000\n"); + } bitlen = sample_rate/BAUD_RATE; k_f0 = freq2bin(4700); // bit0: 4800Hz k_f1 = freq2bin(2900); // bit1: 3000Hz - k_df = fabs(k_f0-k_f1)/2.5; k0 = (int)(k_f0+.5); k1 = (int)(k_f1+.5); init_dft(); ptr = -1; sample_count = -1; - while ((sample=read_signed_sample(fp)) < EOF_INT) { + while (f32read_sample(fp, &s) != EOF) { ptr++; sample_count++; if (ptr == N) ptr = 0; - buffer[ptr] = sample / (float)(1< cb1) ? 0 : 1; - if (option_dft) { - if (option_dft == 2) dft2(); - else dft(); - kmax = max_bin(); - if (kmax > k_f0-k_df && kmax < k_f0+k_df) bit = 0; // kmax = freq2bin(4800): 4800Hz - else if (kmax > k_f1-k_df && kmax < k_f1+k_df) bit = 1; // kmax = freq2bin(3000): 3000Hz - } - else { - cb0 = dft_k(k0); - cb1 = dft_k(k1); - if ( cb0 > cb1 ) bit = 0; // freq2bin(4800) : 4800Hz - else bit = 1; // freq2bin(3000) : 3000Hz - } + if (bit != bit0) { - if (bit != bit0) { + pos0 = pos; + pos = sample_count; //sample_count-(N-1)/2 - pos0 = pos; - pos = sample_count; //sample_count-(N-1)/2 + len = (pos-pos0+bitlen/2)/bitlen; //(pos-pos0)/(float)bitlen + 0.5; + for (i = 0; i < len; i++) { + inc_bufpos(); + buf[bufpos] = 0x30 + bit0; - len = (pos-pos0+bitlen/2)/bitlen; //(pos-pos0)/bitlen + 0.5; - for (i = 0; i < len; i++) { - inc_bufpos(); - buf[bufpos] = 0x30 + bit0; - - if (!header_found) { - if (compare() >= HEADLEN) { - header_found = 1; - for (bitpos = 0; bitpos < HEADLEN; bitpos++) bits[bitpos] = header[bitpos] & 0x1; - } - } - else { - bits[bitpos] = bit0; - bitpos++; - if (bitpos >= LEN_BITFRAME) { - - bits2bytes8N1(bits, bytes, bitpos/BITS); - - if (option_raw) { - printRaw(bitpos/BITS); - } - else { - evalBytes2(); - } - - bitpos = 0; - header_found = 0; - } + if (!header_found) { + if (compare() >= HEADLEN-1) { + header_found = 1; + for (bitpos = 0; bitpos < HEADLEN; bitpos++) bits[bitpos] = header[bitpos] & 0x1; + } + } + else { + bits[bitpos] = bit0; + bitpos++; + if (bitpos >= LEN_BITFRAME) { + + bits2bytes8N1(bits, bytes, bitpos/BITS); + + if (option_raw) { + printRaw(bitpos/BITS); + } + else { + evalBytes2(); + } + + bitpos = 0; + header_found = 0; } } - bit0 = bit; } + bit0 = bit; + } } printf("\n");