SUBS.C
上传用户:njqiyou
上传日期:2007-01-08
资源大小:574k
文件大小:7k
源码类别:

mpeg/mp3

开发平台:

C/C++

  1. /**********************************************************************
  2.  * ISO MPEG Audio Subgroup Software Simulation Group (1996)
  3.  * ISO 13818-3 MPEG-2 Audio Multichannel Encoder
  4.  *
  5.  * $Id: subs.c 1.6 1996/02/12 07:13:35 rowlands Exp $
  6.  *
  7.  * $Log: subs.c $
  8.  * Revision 1.6  1996/02/12 07:13:35  rowlands
  9.  * Release following Munich meeting
  10.  *
  11.  * Revision 1.3.2.1  1995/06/16  03:46:42  rowlands
  12.  * Input from Susanne Ritscher (IRT)
  13.  *
  14.  **********************************************************************/
  15. /**********************************************************************
  16.  *   date   programmers         comment                               *
  17.  * 2/25/91  Davis Pan           start of version 1.0 records          *
  18.  * 5/10/91  W. Joseph Carter    Ported to Macintosh and Unix.         *
  19.  * 7/10/91  Earle Jennings      Ported to MsDos from Macintosh        *
  20.  *                              Replacement of one float with FLOAT   *
  21.  * 2/11/92  W. Joseph Carter    Added type casting to memset() args.  *
  22.  **********************************************************************
  23.  *                                                                    *
  24.  *                                                                    *
  25.  *  MPEG/audio Phase 2 coding/decoding multichannel                   *
  26.  *                                                                    *
  27.  *  7/27/93        Susanne Ritscher,  IRT Munich                      *
  28.  *  8/27/93        Susanne Ritscher, IRT Munich                       *
  29.  *                 Channel-Switching is working                       *
  30.  *                                                                    *
  31.  *  9/1/93         Susanne Ritscher,  IRT Munich                      *
  32.  *                 all channels normalized                            *
  33.  *  9/20/93        channel-switching is only performed at a           *
  34.  *                 certain limit of TC_ALLOC dB, which is included    *
  35.  *                 in encoder.h                                       *
  36.  *                                                                    *
  37.  *  Version 1.0                                                       *
  38.  *                                                                    *
  39.  *  07/12/94       Susanne Ritscher,  IRT Munich                      *
  40.  *                 Tel: +49 89 32399 458                              *
  41.  *                 Fax: +49 89 32399 415                              *
  42.  *                                                                    *
  43.  *  Version 1.1                                                       *
  44.  *                                                                    *
  45.  *  02/23/95    Susanne Ritscher,  IRT Munich                      *
  46.  *                 corrected some bugs                                *
  47.  *                 extension bitstream is working                     *
  48.  *                                                                    *
  49.  **********************************************************************/
  50. #include "common.h"
  51. #include "encoder.h"
  52. /*****************************************************************************
  53.  ************************** Start of Subroutines *****************************
  54.  *****************************************************************************/
  55. /*****************************************************************************
  56.  * FFT computes fast fourier transform of BLKSIZE samples of data            *
  57.  *   uses decimation-in-frequency algorithm described in "Digital            *
  58.  *   Signal Processing" by Oppenheim and Schafer, refer to pages 304         *
  59.  *   (flow graph) and 330-332 (Fortran program in problem 5)                 *
  60.  *   to get the inverse fft, change line 20 from                             *
  61.  *                 w_imag[L] = -sin(PI/le1);                                 *
  62.  *                          to                                               *
  63.  *                 w_imag[L] = sin(PI/le1);                                  *
  64.  *                                                                           *
  65.  *   required constants:                                                     *
  66.  *         #define      PI          3.14159265358979                         *
  67.  *         #define      BLKSIZE     1024                                     *
  68.  *         #define      LOGBLKSIZE  10                                       *
  69.  *                                                                           *
  70.  *****************************************************************************/
  71. void fft(float *x_real, float *x_imag, float *energy, float *phi)
  72. {
  73.  static int     M, MM1;
  74.  static int     init=0, N, NV2, NM1;
  75.  static double  w_real[LOGBLKSIZE], w_imag[LOGBLKSIZE];
  76.  int            i,j,k,ll;
  77.  int            ip, le,le1;
  78.  double         t_real, t_imag, u_real, u_imag;
  79.  if(init==0) {
  80.     memset((char *) w_real, 0, sizeof(w_real));  /* preset statics to 0 */
  81.     memset((char *) w_imag, 0, sizeof(w_imag));  /* preset statics to 0 */
  82.     M = LOGBLKSIZE;
  83.     MM1 = LOGBLKSIZE-1;
  84.     N = BLKSIZE;
  85.     NV2 = BLKSIZE >> 1;
  86.     NM1 = BLKSIZE - 1;
  87.     for(ll=0; ll<M; ll++){
  88.        le = 1 << (M-ll);
  89.        le1 = le >> 1;
  90.        w_real[ll] = cos(PI/le1);
  91.        w_imag[ll] = -sin(PI/le1);
  92.     }          
  93.     init++;
  94.  }
  95.  for(ll=0; ll<MM1; ll++){
  96.     le = 1 << (M-ll);
  97.     le1 = le >> 1;
  98.     u_real = 1;
  99.     u_imag = 0;
  100.     for(j=0; j<le1; j++){
  101.        for(i=j; i<N; i+=le){
  102.           ip = i + le1;
  103.           t_real = x_real[i] + x_real[ip];
  104.           t_imag = x_imag[i] + x_imag[ip];
  105.           x_real[ip] = x_real[i] - x_real[ip];
  106.           x_imag[ip] = x_imag[i] - x_imag[ip];
  107.           x_real[i] = t_real;
  108.           x_imag[i] = t_imag;
  109.           t_real = x_real[ip];
  110.           x_real[ip] = x_real[ip]*u_real - x_imag[ip]*u_imag;
  111.           x_imag[ip] = x_imag[ip]*u_real + t_real*u_imag;
  112.        }
  113.        t_real = u_real;
  114.        u_real = u_real*w_real[ll] - u_imag*w_imag[ll];
  115.        u_imag = u_imag*w_real[ll] + t_real*w_imag[ll];
  116.     }
  117.  }
  118.  /* special case: ll = M-1; all Wn = 1 */
  119.  for(i=0; i<N; i+=2){
  120.     ip = i + 1;
  121.     t_real = x_real[i] + x_real[ip];
  122.     t_imag = x_imag[i] + x_imag[ip];
  123.     x_real[ip] = x_real[i] - x_real[ip];
  124.     x_imag[ip] = x_imag[i] - x_imag[ip];
  125.     x_real[i] = t_real;
  126.     x_imag[i] = t_imag;
  127.     energy[i] = x_real[i]*x_real[i] + x_imag[i]*x_imag[i];
  128.     if(energy[i] <= 0.0005){phi[i] = 0;energy[i] = 0.0005;}
  129.     else phi[i] = atan2((double) x_imag[i],(double) x_real[i]);
  130.     energy[ip] = x_real[ip]*x_real[ip] + x_imag[ip]*x_imag[ip];
  131.     if(energy[ip] == 0)phi[ip] = 0;
  132.     else phi[ip] = atan2((double) x_imag[ip],(double) x_real[ip]);
  133.  }
  134.  /* this section reorders the data to the correct ordering */
  135.  j = 0;
  136.  for(i=0; i<NM1; i++){
  137.     if(i<j){
  138. /* use this section only if you need the FFT in complex number form *
  139.  * (and in the correct ordering)                                    */
  140.        t_real = x_real[j];
  141.        t_imag = x_imag[j];
  142.        x_real[j] = x_real[i];
  143.        x_imag[j] = x_imag[i];
  144.        x_real[i] = t_real;
  145.        x_imag[i] = t_imag;
  146. /* reorder the energy and phase, phi                                        */
  147.        t_real = energy[j];
  148.        energy[j] = energy[i];
  149.        energy[i] = t_real;
  150.        t_real = phi[j];
  151.        phi[j] = phi[i];
  152.        phi[i] = t_real;
  153.     }
  154.     k=NV2;
  155.     while(k<=j){
  156.        j = j-k;
  157.        k = k >> 1;
  158.     }
  159.     j = j+k;
  160.  }
  161. }