home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / c / lpc05b.zip / LPC.C < prev    next >
C/C++ Source or Header  |  1992-05-22  |  3KB  |  157 lines

  1. /*
  2. *------------------------------------------------------------------------------
  3. *    file:    lpc.c
  4. *    desct:    lpc module
  5. *    by:    patrick ko shu pui
  6. *    date:    18 may 1992
  7. *
  8. *    ref:
  9. *
  10. *    Shuzo Saito, Kazuo Nakata, "Fundamentals of Speech Signal Processing,"
  11. *    Academic Press, 1985.
  12. *------------------------------------------------------------------------------
  13. */
  14.  
  15. #include <stdio.h>
  16. #include "matrix.h"
  17. #include "lpc.h" 
  18. #include "gio.h"
  19.  
  20.  
  21. /*
  22. *------------------------------------------------------------------------------
  23. *    funct:    lpcout
  24. *    desct:    output data from generic input
  25. *    given:
  26. *        gout = generic output
  27. *        gacout = generic output for .ac file
  28. *        A = autocorrelation matrix (if exists)
  29. *        B = coefficients matrix (any type .lpc, .par, .cep)
  30. *        i = from offset
  31. *        n = number of coefficients
  32. *------------------------------------------------------------------------------
  33. */ 
  34. int lpcout(gout, B, gacout, A, i, n)
  35. GIO *gout;
  36. MATRIX B;
  37. GIO *gacout;
  38. MATRIX A;
  39. int i, n;
  40. {
  41.     int    j;
  42.     char    temp[128];
  43.  
  44.     for (j=i; j<n; j++)
  45.         {
  46.         sprintf(temp, "%f ", B[j][0]);
  47.         gputs( temp, gout );
  48.         if (gacout != NULL)
  49.             {
  50.             sprintf(temp, "%f ", A[j][0]);
  51.             gputs( temp, gacout );
  52.             }
  53.         }
  54.     gputs( "\n", gout );
  55.     if (gacout != NULL)
  56.         gputs( "\n", gacout );
  57. }
  58.  
  59. /*
  60. *------------------------------------------------------------------------------
  61. *    funct:    lpc
  62. *    desct:    get data from generic input and LPC it
  63. *    given:
  64. *        gin = generic input
  65. *        gout = generic output
  66. *        gacout = generic output for .ac file
  67. *        order = order of LPC
  68. *        ws = window size
  69. *        os = window overlap size
  70. *        method = (see lpc.h) 
  71. *------------------------------------------------------------------------------
  72. */ 
  73. int lpc( gin, dtype, gout, gacout, order, ws, os, method )
  74. GIO *gin, *gout, *gacout;
  75. int order, ws, os, dtype;
  76. int method;
  77. {
  78.     MATRIX    A, B, F, P, E, CEP;
  79.     double    *s;
  80.     short    t;
  81.     char    c;
  82.     int    i, j;
  83.     char    temp[128];
  84.  
  85.     A = mat_creat(order, order, UNDEFINED);
  86.     F = mat_creat(order, 1, UNDEFINED);
  87.     P = mat_creat(order, 1, UNDEFINED);
  88.     E = mat_creat(order+1, 1, UNDEFINED);
  89.     s = (double *)malloc(sizeof(double) * ws);
  90.     i = 0;
  91.  
  92.     while (!geof(gin))
  93.         {
  94.         switch (dtype)
  95.         {
  96.         case AUDIOULAW8:
  97.         if (gread(&c, sizeof(char), 1, gin))
  98.             {
  99.             t = u2s(c);
  100.             s[i] = norm_s2d(t);
  101.             i++;
  102.             }
  103.         break;
  104.  
  105.         case AUDIOPCM16:        
  106.         if (gread(&t,sizeof(short),1,gin))
  107.             {
  108.             s[i] = norm_s2d(t);
  109.             i++;
  110.             }
  111.         break;
  112.  
  113.         default:
  114.         fprintf(stderr, "wrong data type specified\n" );
  115.         exit (1);
  116.         }
  117.  
  118.         if (i>=ws)
  119.             {
  120.             switch (method)
  121.                 {
  122.                 case LPCCOVAR:
  123.                 B = lpc1(A, F, s, ws);
  124.                 lpcout( gout, B, NULL, A, 0, order );
  125.                 break;
  126.  
  127.                 case LPCAUTOCOR:
  128.                 B = lpc2(A, F, s, ws, P, E);
  129.                 lpcout( gout, B, gacout, A, 0, order );
  130.                 break;
  131.  
  132.                 case LPCCEPSTRUM:
  133.                 B = lpc2(A, F, s, ws, P, E);
  134.                 CEP = cepstrum(B, E[order][0]);
  135.                 lpcout( gout, CEP, gacout, A, 0, order + 1 );
  136.                 mat_free(CEP);
  137.                 break;
  138.  
  139.                 case PARCOR:
  140.                 B = lpc2(A, F, s, ws, P, E);
  141.                 lpcout( gout, P, gacout, A, 0, order );
  142.                 break;
  143.                 }
  144.  
  145.             mat_free(B);
  146.             if (os > 0)
  147.                 memmove( s, s+ws-os, sizeof(double) * os );
  148.             i = os;
  149.             }
  150.         }
  151.     mat_free(A);
  152.     mat_free(F);
  153.     mat_free(P);
  154.     mat_free(E);
  155.     free(s);
  156. }
  157.