testenc_uwb.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:3k
源码类别:

Windows CE

开发平台:

C/C++

  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif
  4. #include <speex/speex.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <speex/speex_callbacks.h>
  8. #ifdef FIXED_DEBUG
  9. extern long long spx_mips;
  10. #endif
  11. #define FRAME_SIZE 640
  12. #include <math.h>
  13. int main(int argc, char **argv)
  14. {
  15.    char *inFile, *outFile, *bitsFile;
  16.    FILE *fin, *fout, *fbits=NULL;
  17.    short in_short[FRAME_SIZE];
  18.    short out_short[FRAME_SIZE];
  19.    float in_float[FRAME_SIZE];
  20.    float sigpow,errpow,snr, seg_snr=0;
  21.    int snr_frames = 0;
  22.    char cbits[200];
  23.    int nbBits;
  24.    int i;
  25.    void *st;
  26.    void *dec;
  27.    SpeexBits bits;
  28.    int tmp;
  29.    int bitCount=0;
  30.    int skip_group_delay;
  31.    SpeexCallback callback;
  32.    sigpow = 0;
  33.    errpow = 0;
  34.    st = speex_encoder_init(&speex_uwb_mode);
  35.    dec = speex_decoder_init(&speex_uwb_mode);
  36.    callback.callback_id = SPEEX_INBAND_CHAR;
  37.    callback.func = speex_std_char_handler;
  38.    callback.data = stderr;
  39.    speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);
  40.    callback.callback_id = SPEEX_INBAND_MODE_REQUEST;
  41.    callback.func = speex_std_mode_request_handler;
  42.    callback.data = st;
  43.    speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);
  44.    tmp=0;
  45.    speex_decoder_ctl(dec, SPEEX_SET_ENH, &tmp);
  46.    tmp=0;
  47.    speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp);
  48.    tmp=7;
  49.    speex_encoder_ctl(st, SPEEX_SET_QUALITY, &tmp);
  50.    tmp=1;
  51.    speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &tmp);
  52.    speex_mode_query(&speex_nb_mode, SPEEX_MODE_FRAME_SIZE, &tmp);
  53.    fprintf (stderr, "frame size: %dn", tmp);
  54.    skip_group_delay = 509;
  55.    if (argc != 4 && argc != 3)
  56.    {
  57.       fprintf (stderr, "Usage: encode [in file] [out file] [bits file]nargc = %d", argc);
  58.       exit(1);
  59.    }
  60.    inFile = argv[1];
  61.    fin = fopen(inFile, "r");
  62.    outFile = argv[2];
  63.    fout = fopen(outFile, "w+");
  64.    if (argc==4)
  65.    {
  66.       bitsFile = argv[3];
  67.       fbits = fopen(bitsFile, "w");
  68.    }
  69.    speex_bits_init(&bits);
  70.    while (!feof(fin))
  71.    {
  72.       fread(in_short, sizeof(short), FRAME_SIZE, fin);
  73.       if (feof(fin))
  74.          break;
  75.       for (i=0;i<FRAME_SIZE;i++)
  76.          in_float[i]=in_short[i];
  77.       speex_bits_reset(&bits);
  78.       speex_encode_int(st, in_short, &bits);
  79.       nbBits = speex_bits_write(&bits, cbits, 200);
  80.       bitCount+=bits.nbBits;
  81.       if (argc==4)
  82.          fwrite(cbits, 1, nbBits, fbits);
  83.       speex_bits_rewind(&bits);
  84.       speex_decode_int(dec, &bits, out_short);
  85.       speex_bits_reset(&bits);
  86.       fwrite(&out_short[skip_group_delay], sizeof(short), FRAME_SIZE-skip_group_delay, fout);
  87.       skip_group_delay = 0;
  88.    }
  89.    fprintf (stderr, "Total encoded size: %d bitsn", bitCount);
  90.    speex_encoder_destroy(st);
  91.    speex_decoder_destroy(dec);
  92.    rewind(fin);
  93.    rewind(fout);
  94.    while ( FRAME_SIZE == fread(in_short, sizeof(short), FRAME_SIZE, fin) 
  95.            &&
  96.            FRAME_SIZE ==  fread(out_short, sizeof(short), FRAME_SIZE,fout) )
  97.    {
  98. float s=0, e=0;
  99.         for (i=0;i<FRAME_SIZE;++i) {
  100.             s += (float)in_short[i] * in_short[i];
  101.             e += ((float)in_short[i]-out_short[i]) * ((float)in_short[i]-out_short[i]);
  102.         }
  103. seg_snr += 10*log10((s+1)/(e+1));
  104. sigpow += s;
  105. errpow += e;
  106. snr_frames++;
  107.    }
  108.    fclose(fin);
  109.    fclose(fout);
  110.    snr = 10 * log10( sigpow / errpow );
  111.    seg_snr /= snr_frames;
  112.    fprintf(stderr,"SNR = %fnsegmental SNR = %fn",snr, seg_snr);
  113. #ifdef FIXED_DEBUG
  114.    printf ("Total: %f MIPSn", (float)(1e-6*50*spx_mips/snr_frames));
  115. #endif
  116.    
  117.    return 1;
  118. }