Sound_and_PCA.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Sound_and_PCA.cpp
  2. *
  3. * Copyright (C) 2012-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 20121001
  20. */
  21. #include "ICA.h"
  22. #include "Sound_and_PCA.h"
  23. #include "Sound_extensions.h"
  24. #include "NUM2.h"
  25. static void checkChannelsWithinRange (constINTVEC channels, integer min, integer max) {
  26. for (integer i = 1; i <= channels.size; i ++) {
  27. if (channels [i] < min || channels [i] > max) {
  28. Melder_throw (U"Channel ", channels [i], U" is not within range [", min, U", ", max, U"].");
  29. }
  30. }
  31. }
  32. autoPCA Sound_to_PCA_channels (Sound me, double startTime, double endTime) {
  33. try {
  34. // covariance is cross-correlation with lag time 0
  35. autoCrossCorrelationTable thee = Sound_to_CrossCorrelationTable (me, startTime, endTime, 0);
  36. autoPCA him = SSCP_to_PCA (thee.get());
  37. return him;
  38. } catch (MelderError) {
  39. Melder_throw (me, U": no PCA created.");
  40. }
  41. }
  42. autoSound Sound_PCA_to_Sound_pc_selectedChannels (Sound me, PCA thee, integer numberOfComponents, constINTVEC channels) {
  43. try {
  44. if (numberOfComponents <= 0 || numberOfComponents > thy numberOfEigenvalues) {
  45. numberOfComponents = thy numberOfEigenvalues;
  46. }
  47. numberOfComponents = numberOfComponents > my ny ? my ny : numberOfComponents;
  48. checkChannelsWithinRange (channels, 1, my ny);
  49. autoSound him = Data_copy (me);
  50. // R ['i',j] = E(i,k]*S ['k',j]
  51. // use kij-variant for faster inner loop
  52. for (integer k = 1; k <= thy dimension; k ++)
  53. for (integer i = 1; i <= numberOfComponents; i ++) {
  54. double ev_ik = thy eigenvectors [i] [k];
  55. for (integer j = 1; j <= my nx; j ++)
  56. his z [channels [i]] [j] += ev_ik * my z [channels [k]] [j];
  57. }
  58. return him;
  59. } catch (MelderError) {
  60. Melder_throw (me, U": no principal components calculated with ", thee);
  61. }
  62. }
  63. autoSound Sound_PCA_principalComponents (Sound me, PCA thee, integer numberOfComponents) {
  64. autoINTVEC channels = INTVECto (my ny);
  65. return Sound_PCA_to_Sound_pc_selectedChannels (me, thee, numberOfComponents, channels.get());
  66. }
  67. autoSound Sound_PCA_whitenSelectedChannels (Sound me, PCA thee, integer numberOfComponents, constINTVEC channels) {
  68. try {
  69. if (numberOfComponents <= 0 || numberOfComponents > thy numberOfEigenvalues)
  70. numberOfComponents = thy numberOfEigenvalues;
  71. checkChannelsWithinRange (channels, 1, my ny);
  72. autoNUMmatrix <double> whiten (1, thy dimension, 1, thy dimension);
  73. // W = E D^(-1/2) E' from http://cis.legacy.ics.tkk.fi/aapo/papers/IJCNN99_tutorialweb/node26.html
  74. for (integer i = 1; i <= thy dimension; i ++) {
  75. for (integer j = i; j <= thy dimension; j ++) {
  76. longdouble wij = 0.0;
  77. for (integer k = 1; k <= numberOfComponents; k ++) {
  78. wij += thy eigenvectors [k] [i] * thy eigenvectors [k] [j] / sqrt (thy eigenvalues [k]);
  79. }
  80. whiten [i] [j] = whiten [j] [i] = wij;
  81. }
  82. }
  83. autoSound him = Sound_create (my ny, my xmin, my xmax, my nx, my dx, my x1);
  84. for (integer k = 1; k <= channels.size; k ++) {
  85. for (integer i = 1; i <= channels.size; i ++) {
  86. double w_ik = whiten [i] [k];
  87. for (integer j = 1; j <= my nx; j ++) {
  88. his z [channels [i]] [j] += w_ik * my z [channels [k]] [j];
  89. }
  90. }
  91. }
  92. return him;
  93. } catch (MelderError) {
  94. Melder_throw (U"Sound not created.");
  95. }
  96. }
  97. autoSound Sound_PCA_whitenChannels (Sound me, PCA thee, integer numberOfComponents) {
  98. autoINTVEC channels = INTVECto (my ny);
  99. return Sound_PCA_whitenSelectedChannels (me, thee, numberOfComponents, channels.get());
  100. }
  101. /* End of file Sound_and_PCA.cpp */