[xiph-cvs] cvs commit: speex/libspeex testenc.c

Jean-Marc Valin jm at xiph.org
Mon Sep 1 23:26:15 PDT 2003



jm          03/09/02 02:26:15

  Modified:    libspeex testenc.c
  Log:
  segmental SNR estimate works

Revision  Changes    Path
1.44      +55 -47    speex/libspeex/testenc.c

Index: testenc.c
===================================================================
RCS file: /usr/local/cvsroot/speex/libspeex/testenc.c,v
retrieving revision 1.43
retrieving revision 1.44
diff -u -r1.43 -r1.44
--- testenc.c	27 Oct 2002 06:01:30 -0000	1.43
+++ testenc.c	2 Sep 2003 06:26:15 -0000	1.44
@@ -9,8 +9,12 @@
 {
    char *inFile, *outFile, *bitsFile;
    FILE *fin, *fout, *fbits=NULL;
-   short in[FRAME_SIZE];
-   float input[FRAME_SIZE], bak[FRAME_SIZE], bak2[FRAME_SIZE];
+   short in_short[FRAME_SIZE];
+   short out_short[FRAME_SIZE];
+   float in_float[FRAME_SIZE];
+   float out_float[FRAME_SIZE];
+   float sigpow,errpow,snr, seg_snr;
+   int snr_frames = 0;
    char cbits[200];
    int nbBits;
    int i;
@@ -19,10 +23,12 @@
    SpeexBits bits;
    int tmp;
    int bitCount=0;
+   int skip_group_delay;
    SpeexCallback callback;
 
-   for (i=0;i<FRAME_SIZE;i++)
-      bak2[i]=0;
+   sigpow = 0;
+   errpow = 0;
+
    st = speex_encoder_init(&speex_nb_mode);
    dec = speex_decoder_init(&speex_nb_mode);
 
@@ -42,11 +48,12 @@
    speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp);
    tmp=8;
    speex_encoder_ctl(st, SPEEX_SET_QUALITY, &tmp);
-   tmp=1;
+   tmp=3;
    speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &tmp);
 
    speex_mode_query(&speex_nb_mode, SPEEX_MODE_FRAME_SIZE, &tmp);
    fprintf (stderr, "frame size: %d\n", tmp);
+   skip_group_delay = tmp / 2;
 
    if (argc != 4 && argc != 3)
    {
@@ -56,7 +63,7 @@
    inFile = argv[1];
    fin = fopen(inFile, "r");
    outFile = argv[2];
-   fout = fopen(outFile, "w");
+   fout = fopen(outFile, "w+");
    if (argc==4)
    {
       bitsFile = argv[3];
@@ -65,63 +72,64 @@
    speex_bits_init(&bits);
    while (!feof(fin))
    {
-      fread(in, sizeof(short), FRAME_SIZE, fin);
+      fread(in_short, sizeof(short), FRAME_SIZE, fin);
       if (feof(fin))
          break;
       for (i=0;i<FRAME_SIZE;i++)
-         bak[i]=input[i]=in[i];
+         in_float[i]=in_short[i];
       speex_bits_reset(&bits);
-      /*
-      speex_bits_pack(&bits, 14, 5);
-      speex_bits_pack(&bits, SPEEX_INBAND_CHAR, 4);
-      speex_bits_pack(&bits, 'A', 8);
-      
-      speex_bits_pack(&bits, 14, 5);
-      speex_bits_pack(&bits, SPEEX_INBAND_MODE_REQUEST, 4);
-      speex_bits_pack(&bits, 7, 4);
-
-      speex_bits_pack(&bits, 15, 5);
-      speex_bits_pack(&bits, 2, 4);
-      speex_bits_pack(&bits, 0, 16);
-      */
-      speex_encode(st, input, &bits);
+
+      speex_encode(st, in_float, &bits);
       nbBits = speex_bits_write(&bits, cbits, 200);
       bitCount+=bits.nbBits;
-      printf ("Encoding frame in %d bits\n", nbBits*8);
+
       if (argc==4)
          fwrite(cbits, 1, nbBits, fbits);
-      {
-         float enoise=0, esig=0, snr;
-         for (i=0;i<FRAME_SIZE;i++)
-         {
-            enoise+=(bak2[i]-input[i])*(bak2[i]-input[i]);
-            esig += bak2[i]*bak2[i];
-         }
-         snr = 10*log10((esig+1)/(enoise+1));
-         printf ("real SNR = %f\n", snr);
-      }
       speex_bits_rewind(&bits);
-      
-      speex_decode(dec, &bits, input);
-      
+
+      speex_decode(dec, &bits, out_float);
+      speex_bits_reset(&bits);
+
       /* Save the bits here */
       for (i=0;i<FRAME_SIZE;i++)
       {
-         if (input[i]>32000)
-            input[i]=32000;
-         else if (input[i]<-32000)
-            input[i]=-32000;
+         if (out_float[i]>32767)
+            out_short[i]=32767;
+         else if (out_float[i]<-32768)
+            out_short[i]=-32768;
+         else
+             out_short[i]=(short)(out_float[i]+.5);
       }
-      speex_bits_reset(&bits);
-      for (i=0;i<FRAME_SIZE;i++)
-         in[i]=(short)input[i];
-      for (i=0;i<FRAME_SIZE;i++)
-         bak2[i]=bak[i];
-      fwrite(in, sizeof(short), FRAME_SIZE, fout);
+      fwrite(&out_short[skip_group_delay], sizeof(short), FRAME_SIZE-skip_group_delay, fout);
+      skip_group_delay = 0;
    }
    fprintf (stderr, "Total encoded size: %d bits\n", bitCount);
-   
    speex_encoder_destroy(st);
    speex_decoder_destroy(dec);
+
+   rewind(fin);
+   rewind(fout);
+
+   while ( FRAME_SIZE == fread(in_short, sizeof(short), FRAME_SIZE, fin) 
+           &&
+           FRAME_SIZE ==  fread(out_short, sizeof(short), FRAME_SIZE,fout) )
+   {
+	float s=0, e=0;
+        for (i=0;i<FRAME_SIZE;++i) {
+            s += in_short[i] * in_short[i];
+            e += (in_short[i]-out_short[i]) * (in_short[i]-out_short[i]);
+        }
+	seg_snr += 10*log10((s+1)/(e+1));
+	sigpow += s;
+	errpow += e;
+	snr_frames++;
+   }
+   fclose(fin);
+   fclose(fout);
+
+   snr = 10 * log10( sigpow / errpow );
+   seg_snr /= snr_frames;
+   fprintf(stderr,"SNR = %f\nsegmental SNR = %f\n",snr, seg_snr);
+   
    return 1;
 }

<p><p>--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list