subs.c
上传用户:bjsgzm
上传日期:2007-01-08
资源大小:256k
文件大小:6k
源码类别:

mpeg/mp3

开发平台:

Visual C++

  1. /*
  2. (c) Copyright 1998, 1999 - Tord Jansson
  3. =======================================
  4. This file is part of the BladeEnc MP3 Encoder, based on
  5. ISO's reference code for MPEG Layer 3 compression, and might
  6. contain smaller or larger sections that are directly taken
  7. from ISO's reference code.
  8. All changes to the ISO reference code herein are either
  9. copyrighted by Tord Jansson (tord.jansson@swipnet.se)
  10. or sublicensed to Tord Jansson by a third party.
  11. BladeEnc is free software; you can redistribute this file
  12. and/or modify it under the terms of the GNU Lesser General Public
  13. License as published by the Free Software Foundation; either
  14. version 2.1 of the License, or (at your option) any later version.
  15. */
  16. #include "common.h"
  17. #include "encoder.h"
  18. /*****************************************************************************
  19.  ************************** Start of Subroutines *****************************
  20.  *****************************************************************************/
  21. /*****************************************************************************
  22.  * FFT computes fast fourier transform of BLKSIZE samples of data            *
  23.  *   uses decimation-in-frequency algorithm described in "Digital            *
  24.  *   Signal Processing" by Oppenheim and Schafer, refer to pages 304         *
  25.  *   (flow graph) and 330-332 (Fortran program in problem 5)                 *
  26.  *   to get the inverse fft, change line 20 from                             *
  27.  *                 w_imag[L] = -sin(PI/le1);                                 *
  28.  *                          to                                               *
  29.  *                 w_imag[L] = sin(PI/le1);                                  *
  30.  *                                                                           *
  31.  *   required constants:                                                     *
  32.  *         #define      PI          3.14159265358979                         *
  33.  *         #define      BLKSIZE     1024                                     *
  34.  *         #define      LOGBLKSIZE  10                                       *
  35.  *         #define      BLKSIZE_S   256                                      *
  36.  *         #define      LOGBLKSIZE_S 8                                       *
  37.  *                                                                           *
  38.  *****************************************************************************/
  39. #define      BLKSIZE_S   256
  40. #define      LOGBLKSIZE_S 8
  41. int fInit_fft;
  42. void fft(FLOAT x_real[BLKSIZE],float x_imag[BLKSIZE], float energy[BLKSIZE], float phi[BLKSIZE], int N)
  43. {
  44.  int     M,MM1;
  45.  int     NV2, NM1, MP;
  46.  static double  w_real[2][LOGBLKSIZE], w_imag[2][LOGBLKSIZE];
  47.  int            i,j,k,L;
  48.  int            ip, le,le1;
  49.  double         t_real, t_imag, u_real, u_imag;
  50.  if(fInit_fft==0) 
  51.  {
  52.     memset((char *) w_real, 0, sizeof(w_real));  /* preset statics to 0 */
  53.     memset((char *) w_imag, 0, sizeof(w_imag));  /* preset statics to 0 */
  54.     M = LOGBLKSIZE;
  55.     for(L=0; L<M; L++)
  56. {
  57.        le = 1 << (M-L);
  58.        le1 = le >> 1;
  59.        w_real[0][L] = cos(PI/le1);
  60.        w_imag[0][L] = -sin(PI/le1);
  61.     }          
  62.     M = LOGBLKSIZE_S;
  63.     for(L=0; L<M; L++)
  64. {
  65.        le = 1 << (M-L);
  66.        le1 = le >> 1;
  67.        w_real[1][L] = cos(PI/le1);
  68.        w_imag[1][L] = -sin(PI/le1);
  69.     }          
  70.     fInit_fft++;
  71.  }
  72. if( N == BLKSIZE )
  73. {
  74. M = LOGBLKSIZE;
  75. MP = 0;
  76. }
  77. else /* N == BLKSIZE_S */
  78. {
  79. M = LOGBLKSIZE_S;
  80. MP = 1;
  81. }
  82. MM1 = M-1;
  83. NV2 = N >> 1;
  84. NM1 = N - 1;
  85. for(L=0; L<MM1; L++)
  86. {
  87.     le = 1 << (M-L);
  88.     le1 = le >> 1;
  89.     u_real = 1;
  90.     u_imag = 0;
  91.     for(j=0; j<le1; j++)
  92. {
  93.        for(i=j; i<N; i+=le)
  94.  {
  95.           ip = i + le1;
  96.           t_real = x_real[i] + x_real[ip];
  97.           t_imag = x_imag[i] + x_imag[ip];
  98.           x_real[ip] = x_real[i] - x_real[ip];
  99.           x_imag[ip] = x_imag[i] - x_imag[ip];
  100.           x_real[i] = t_real;
  101.           x_imag[i] = t_imag;
  102.           t_real = x_real[ip];
  103.           x_real[ip] = x_real[ip]*u_real - x_imag[ip]*u_imag;
  104.           x_imag[ip] = x_imag[ip]*u_real + t_real*u_imag;
  105.        }
  106.        t_real = u_real;
  107.        u_real = u_real*w_real[MP][L] - u_imag*w_imag[MP][L];
  108.        u_imag = u_imag*w_real[MP][L] + t_real*w_imag[MP][L];
  109.     }
  110.  }
  111.  /* special case: L = M-1; all Wn = 1 */
  112.  for(i=0; i<N; i+=2)
  113.  {
  114.     ip = i + 1;
  115.     t_real = x_real[i] + x_real[ip];
  116.     t_imag = x_imag[i] + x_imag[ip];
  117.     x_real[ip] = x_real[i] - x_real[ip];
  118.     x_imag[ip] = x_imag[i] - x_imag[ip];
  119.     x_real[i] = t_real;
  120.     x_imag[i] = t_imag;
  121.     energy[i] = x_real[i]*x_real[i] + x_imag[i]*x_imag[i];
  122.     if(energy[i] <= 0.0005)
  123. {
  124. phi[i] = 0;
  125. energy[i] = 0.0005;
  126. }
  127.     else 
  128. phi[i] = atan2((double) x_imag[i],(double) x_real[i]);
  129.     energy[ip] = x_real[ip]*x_real[ip] + x_imag[ip]*x_imag[ip];
  130.     if(energy[ip] == 0)
  131. phi[ip] = 0;
  132.     else 
  133. phi[ip] = atan2((double) x_imag[ip],(double) x_real[ip]);
  134.  }
  135.  /* this section reorders the data to the correct ordering */
  136.  j = 0;
  137.  for(i=0; i<NM1; i++)
  138.  {
  139.     if(i<j)
  140. {
  141. /* use this section only if you need the FFT in complex number form *
  142. * (and in the correct ordering)                                    */
  143.        t_real = x_real[j];
  144.        t_imag = x_imag[j];
  145.        x_real[j] = x_real[i];
  146.        x_imag[j] = x_imag[i];
  147.        x_real[i] = t_real;
  148.        x_imag[i] = t_imag;
  149. /* reorder the energy and phase, phi                                        */
  150.        t_real = energy[j];
  151.        energy[j] = energy[i];
  152.        energy[i] = t_real;
  153.        t_real = phi[j];
  154.        phi[j] = phi[i];
  155.        phi[i] = t_real;
  156.     }
  157.     k=NV2;
  158.     while(k<=j)
  159. {
  160.        j = j-k;
  161.        k = k >> 1;
  162.     }
  163.     j = j+k;
  164.  }
  165. }