test_real.c
上传用户:ozl2332
上传日期:2009-12-28
资源大小:38k
文件大小:3k
源码类别:

语音压缩

开发平台:

C/C++

  1. #include "kiss_fftr.h"
  2. #include "_kiss_fft_guts.h"
  3. #include <sys/times.h>
  4. #include <time.h>
  5. #include <unistd.h>
  6. static double cputime(void)
  7. {
  8.     struct tms t;
  9.     times(&t);
  10.     return (double)(t.tms_utime + t.tms_stime)/  sysconf(_SC_CLK_TCK) ;
  11. }
  12. static
  13. double snr_compare( kiss_fft_cpx * vec1,kiss_fft_cpx * vec2, int n)
  14. {
  15.     int k;
  16.     double sigpow,noisepow,err,snr,scale=0;
  17.     sigpow = noisepow = .00000000000000000001; 
  18.     for (k=0;k<n;++k) {
  19.         sigpow += (double)vec1[k].r * (double)vec1[k].r + 
  20.                   (double)vec1[k].i * (double)vec1[k].i;
  21.         err = (double)vec1[k].r - (double)vec2[k].r;
  22.         noisepow += err * err;
  23.         err = (double)vec1[k].i - (double)vec2[k].i;
  24.         noisepow += err * err;
  25.         if (vec1[k].r)
  26.             scale +=(double) vec2[k].r / (double)vec1[k].r;
  27.         /*
  28.         fprintf(stderr,"vec1=");pcpx(vec1+k);
  29.         fprintf(stderr,"vec2=");pcpx(vec2+k);
  30.         */
  31.     }
  32.     snr = 10*log10( sigpow / noisepow );
  33.     scale /= n;
  34.     if (snr<10) {
  35.         printf( "npoor snr, try a scaling factor %fn" , scale );
  36.         exit(1);
  37.     }
  38.     return snr;
  39. }
  40. #define RANDOM
  41. #ifndef RANDOM
  42. #define NFFT 8
  43. #else
  44. #define NFFT 8*3*5
  45. #endif
  46. #ifndef NUMFFTS
  47. #define NUMFFTS 1000
  48. #endif
  49. int main(void)
  50. {
  51.     double ts,tfft,trfft;
  52.     int i;
  53.     kiss_fft_cpx cin[NFFT];
  54.     kiss_fft_scalar rin[NFFT] = {0.309655,0.815653,0.768570,0.591841,0.404767,0.637617,0.007803,0.012665};
  55.     kiss_fft_cpx cout[NFFT];
  56.     kiss_fft_cpx sout[NFFT];
  57.     
  58.     kiss_fft_cfg  kiss_fft_state;
  59.     kiss_fftr_cfg  kiss_fftr_state;
  60.     
  61.     srand(time(0));
  62.     for (i=0;i<NFFT;++i) {
  63. #ifdef RANDOM        
  64.         rin[i] = (kiss_fft_scalar)(rand()-RAND_MAX/2);
  65. #endif        
  66.         cin[i].r = rin[i];
  67.         cin[i].i = 0;
  68.     }
  69.     kiss_fft_state = kiss_fft_alloc(NFFT,0,0,0);
  70.     kiss_fftr_state = kiss_fftr_alloc(NFFT,0,0,0);
  71.     kiss_fft(kiss_fft_state,cin,cout);
  72.     kiss_fftr(kiss_fftr_state,rin,sout);
  73.     printf( "nfft=%d, inverse=%d, snr=%gn",
  74.             NFFT,0, snr_compare(cout,sout,(NFFT/2)+1) );
  75. #ifdef RANDOM        
  76.     ts = cputime();
  77.     for (i=0;i<NUMFFTS;++i) {
  78.         kiss_fft(kiss_fft_state,cin,cout);
  79.     }
  80.     tfft = cputime() - ts;
  81.     
  82.     ts = cputime();
  83.     for (i=0;i<NUMFFTS;++i) {
  84.         kiss_fftr( kiss_fftr_state, rin, cout );
  85.         /* kiss_fftri(kiss_fftr_state,cout,rin); */
  86.     }
  87.     trfft = cputime() - ts;
  88.     printf("%d complex ffts took %gs, real took %gsn",NUMFFTS,tfft,trfft);
  89. #endif        
  90.     free(kiss_fft_state);
  91.     free(kiss_fftr_state);
  92.     kiss_fft_state = kiss_fft_alloc(NFFT,1,0,0);
  93.     kiss_fftr_state = kiss_fftr_alloc(NFFT,1,0,0);
  94.     kiss_fft(kiss_fft_state,cout,cin);
  95.     kiss_fftri(kiss_fftr_state,cout,rin);
  96.     for (i=0;i<NFFT;++i) {
  97.         sout[i].r = rin[i];
  98.         sout[i].i = 0;
  99.     }
  100.     
  101.     printf( "nfft=%d, inverse=%d, snr=%gn",
  102.             NFFT,1, snr_compare(cin,cin,NFFT/2) );
  103.     free(kiss_fft_state);
  104.     free(kiss_fftr_state);
  105.     return 0;
  106. }