LPC_and_Cepstrumc.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* LPC_and_Cepstrumc.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 20080122 float -> double
  21. */
  22. #include "LPC_and_Cepstrumc.h"
  23. void LPC_Frame_into_Cepstrumc_Frame (LPC_Frame me, Cepstrumc_Frame thee) {
  24. integer n = my nCoefficients > thy nCoefficients ? thy nCoefficients : my nCoefficients;
  25. double *c = thy c, *a = my a.at;
  26. c [0] = 0.5 * log (my gain);
  27. if (n == 0) {
  28. return;
  29. }
  30. c [1] = -a [1];
  31. for (integer i = 2; i <= n; i ++) {
  32. c [i] = 0;
  33. for (integer k = 1; k < i; k ++) {
  34. c [i] += a [i - k] * c [k] * k;
  35. }
  36. c [i] = -a [i] - c [i] / i;
  37. }
  38. }
  39. void Cepstrumc_Frame_into_LPC_Frame (Cepstrumc_Frame me, LPC_Frame thee) {
  40. double *c = my c, *a = thy a.at;
  41. thy gain = exp (2.0 * c [0]);
  42. if (thy nCoefficients == 0) {
  43. return;
  44. }
  45. a [1] = -c [1];
  46. for (integer i = 2; i <= thy nCoefficients; i ++) {
  47. c [i] *= i;
  48. }
  49. for (integer i = 2; i <= thy nCoefficients; i ++) {
  50. a [i] = c [i];
  51. for (integer j = 1 ; j < i; j++) {
  52. a [i] += a [j] * c [i - j];
  53. }
  54. a [i] /= -i;
  55. }
  56. for (integer i = 2; i <= thy nCoefficients; i ++) {
  57. c [i] /= i;
  58. }
  59. }
  60. autoCepstrumc LPC_to_Cepstrumc (LPC me) {
  61. try {
  62. autoCepstrumc thee = Cepstrumc_create (my xmin, my xmax, my nx, my dx, my x1, my maxnCoefficients, 1.0 / my samplingPeriod);
  63. for (integer i = 1; i <= my nx; i ++) {
  64. Cepstrumc_Frame_init (& thy frame [i], my d_frames [i].nCoefficients);
  65. LPC_Frame_into_Cepstrumc_Frame (& my d_frames [i], & thy frame [i]);
  66. }
  67. return thee;
  68. } catch (MelderError) {
  69. Melder_throw (me, U": no Cepstrum created.");
  70. }
  71. }
  72. autoLPC Cepstrumc_to_LPC (Cepstrumc me) {
  73. try {
  74. autoLPC thee = LPC_create (my xmin, my xmax, my nx, my dx, my x1,
  75. my maxnCoefficients, 1.0 / my samplingFrequency);
  76. for (integer i = 1; i <= my nx; i ++) {
  77. LPC_Frame_init (& thy d_frames [i], my frame [i].nCoefficients);
  78. Cepstrumc_Frame_into_LPC_Frame (& my frame [i], & thy d_frames [i]);
  79. }
  80. return thee;
  81. } catch (MelderError) {
  82. Melder_throw (me, U":no LPC created.");
  83. }
  84. }
  85. /* End of file LPC_and_Cepstrumc.cpp */