Cepstrum_and_Spectrum.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Cepstrum_and_Spectrum.cpp
  2. *
  3. * Copyright (C) 1994-2017 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 20041124 Changed call to Sound_to_Spectrum.
  21. djmw 20070103 Sound interface changes
  22. djmw 20080122 float -> double
  23. djmw 20121015
  24. */
  25. #include "Cepstrum_and_Spectrum.h"
  26. #include "NUM2.h"
  27. #include "Spectrum_extensions.h"
  28. #include "Sound_and_Spectrum.h"
  29. #if 0
  30. static autoCepstrum Spectrum_to_Cepstrum_cmplx (Spectrum me) {
  31. try {
  32. autoMatrix unwrap = Spectrum_unwrap (me);
  33. autoSpectrum sx = Data_copy (me);
  34. // Copy magnitude-squared and unwrapped phase.
  35. for (integer i = 1; i <= my nx; i ++) {
  36. double xa = unwrap -> z[1][i];
  37. sx -> z[1][i] = xa > 0.0 ? 0.5 * log (xa) : -300.0;
  38. sx -> z[2][i] = unwrap -> z[2][i];
  39. }
  40. // Compute complex cepstrum x.
  41. autoSound x = Spectrum_to_Sound (sx.get());
  42. autoCepstrum thee = Cepstrum_create (x -> xmax - x -> xmin, x -> nx);
  43. NUMvector_copyElements (x -> z[1], thy z[1], 1, x -> nx);
  44. return thee;
  45. } catch (MelderError) {
  46. Melder_throw (me, U": no Cepstrum created.");
  47. }
  48. }
  49. #endif
  50. autoPowerCepstrum Spectrum_to_PowerCepstrum (Spectrum me) {
  51. try {
  52. autoSpectrum dBspectrum = Data_copy (me);
  53. double *re = dBspectrum -> z[1], *im = dBspectrum -> z[2];
  54. for (integer i = 1; i <= dBspectrum -> nx; i ++) {
  55. re [i] = log (re [i] * re [i] + im [i] * im [i] + 1e-300);
  56. im [i] = 0.0;
  57. }
  58. autoSound cepstrum = Spectrum_to_Sound (dBspectrum.get());
  59. autoPowerCepstrum thee = PowerCepstrum_create (0.5 / my dx, my nx);
  60. for (integer i = 1; i <= thy nx; i ++) {
  61. double val = cepstrum -> z [1] [i];
  62. thy z [1] [i] = val * val;
  63. }
  64. return thee;
  65. } catch (MelderError) {
  66. Melder_throw (me, U": not converted to Sound.");
  67. }
  68. }
  69. autoCepstrum Spectrum_to_Cepstrum (Spectrum me) {
  70. try {
  71. autoSpectrum dBspectrum = Data_copy (me);
  72. double *re = dBspectrum -> z[1], *im = dBspectrum -> z[2];
  73. for (integer i = 1; i <= dBspectrum -> nx; i ++) {
  74. re [i] = log (re [i] * re [i] + im [i] * im [i] + 1e-300);
  75. im [i] = 0.0;
  76. }
  77. autoSound cepstrum = Spectrum_to_Sound (dBspectrum.get());
  78. autoCepstrum thee = Cepstrum_create (0.5 / my dx, my nx);
  79. for (integer i = 1; i <= thy nx; i ++) {
  80. double val = cepstrum -> z [1] [i];
  81. thy z [1] [i] = val;
  82. }
  83. return thee;
  84. } catch (MelderError) {
  85. Melder_throw (me, U": not converted to Sound.");
  86. }
  87. }
  88. autoSpectrum Cepstrum_to_Spectrum (Cepstrum me) { //TODO power cepstrum
  89. try {
  90. autoCepstrum cepstrum = Data_copy (me);
  91. cepstrum -> z [1] [1] = my z [1] [1];
  92. for (integer i = 2; i <= cepstrum -> nx; i ++) {
  93. cepstrum -> z [1] [i] = 2 * my z [1] [i];
  94. }
  95. autoSpectrum thee = Sound_to_Spectrum ((Sound) cepstrum.get(), true);
  96. double *re = thy z [1], *im = thy z [2];
  97. for (integer i = 1; i <= thy nx; i ++) {
  98. re [i] = exp (0.5 * re [i]); // i.e., sqrt (exp(re [i]))
  99. im [i] = 0.0;
  100. }
  101. return thee;
  102. } catch (MelderError) {
  103. Melder_throw (me, U": no Spectrum created.");
  104. }
  105. }
  106. /* End of file Cepstrum_and_Spectrum.cpp */