LPC_and_LFCC.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* LPC_and_LFCC.cpp
  2. *
  3. * Copyright (C) 1994-2018 David Weenink
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. djmw 20020812 GPL header
  20. djmw 20030205 Latest modifiation
  21. */
  22. #include "LPC_and_LFCC.h"
  23. #include "NUM2.h"
  24. void LPC_Frame_into_CC_Frame (LPC_Frame me, CC_Frame thee) {
  25. double *c = thy c, *a = my a.at;
  26. thy c0 = 0.5 * log (my gain);
  27. if (my nCoefficients < 1) {
  28. return;
  29. }
  30. c [1] = -a [1];
  31. for (integer n = 2; n <= std::min ((integer) my nCoefficients, thy numberOfCoefficients); n ++) {
  32. double s = 0;
  33. for (integer k = 1; k < n; k ++) {
  34. s += a [k] * c [n - k] * (n - k);
  35. }
  36. c [n] = -a [n] - s / n;
  37. }
  38. for (integer n = my nCoefficients + 1; n <= thy numberOfCoefficients; n ++) {
  39. double s = 0;
  40. for (integer k = 1; k <= my nCoefficients; k ++) {
  41. s += a [k] * c [n - k] * (n - k);
  42. }
  43. c [n] = - s / n;
  44. }
  45. }
  46. void CC_Frame_into_LPC_Frame (CC_Frame me, LPC_Frame thee) {
  47. integer n = std::min (my numberOfCoefficients, (integer) thy nCoefficients);
  48. thy gain = exp (2.0 * my c0);
  49. if (n < 1) return;
  50. thy a [1] = - my c [1];
  51. for (integer i = 2; i <= n; i ++) {
  52. longdouble ai = my c [i] * i;
  53. for (integer j = 1; j < i; j ++) {
  54. ai += thy a [j] * my c [i - j] * (i - j);
  55. }
  56. thy a [i] = -ai / i;
  57. }
  58. }
  59. autoLFCC LPC_to_LFCC (LPC me, integer numberOfCoefficients) {
  60. try {
  61. if (numberOfCoefficients < 1) {
  62. numberOfCoefficients = my maxnCoefficients;
  63. }
  64. autoLFCC thee = LFCC_create (my xmin, my xmax, my nx, my dx, my x1, numberOfCoefficients, 0, 0.5 / my samplingPeriod);
  65. for (integer i = 1; i <= my nx; i ++) {
  66. CC_Frame_init (& thy frame [i], numberOfCoefficients);
  67. LPC_Frame_into_CC_Frame (& my d_frames [i], & thy frame [i]);
  68. }
  69. return thee;
  70. } catch (MelderError) {
  71. Melder_throw (me, U": no LFCC created.");
  72. }
  73. }
  74. autoLPC LFCC_to_LPC (LFCC me, integer numberOfCoefficients) {
  75. try {
  76. if (numberOfCoefficients < 1) {
  77. numberOfCoefficients = my maximumNumberOfCoefficients;
  78. }
  79. numberOfCoefficients = std::min (numberOfCoefficients, my maximumNumberOfCoefficients);
  80. autoLPC thee = LPC_create (my xmin, my xmax, my nx, my dx, my x1, numberOfCoefficients, 0.5 / my fmax);
  81. for (integer i = 1; i <= my nx; i ++) {
  82. LPC_Frame_init (& thy d_frames [i], numberOfCoefficients);
  83. CC_Frame_into_LPC_Frame (& my frame [i], & thy d_frames [i]);
  84. }
  85. return thee;
  86. } catch (MelderError) {
  87. Melder_throw (me, U": no LPC created.");
  88. }
  89. }
  90. /* End of file LPC_and_LFCC.cpp */