ICA.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifndef _ICA_h_
  2. #define _ICA_h_
  3. /* ICA.h
  4. *
  5. * Copyright (C) 2010-2017 David Weenink, 2015 Paul Boersma
  6. *
  7. * This code is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This code is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /*
  21. djmw 20101202 Initial version
  22. djmw 20120406 Latest modification.
  23. */
  24. #include "SSCP.h"
  25. #include "Sound_and_MixingMatrix.h"
  26. Thing_define (Diagonalizer, TableOfReal) {
  27. };
  28. Thing_define (CrossCorrelationTable, SSCP) {
  29. void v_info ()
  30. override;
  31. };
  32. Collection_define (CrossCorrelationTableList, OrderedOf, CrossCorrelationTable) {
  33. void v_info ()
  34. override;
  35. };
  36. /*
  37. Cell [i,j] of a CrossCorrelationTable contains the cross-correlation between signal i and signal j (for one particular lag time).
  38. For example, the CrossCorrelation of an n-channel sound is a nxn table where cell [i,j] contains the
  39. cross-correlation of channel i with channel j for a particlular lag time tau.
  40. In the statistical literature sometimes the cross-correlation between signals is also called "covariance".
  41. However, the only thing a Covariance has in common with a CrossCorrelationTable is that both are symmetric
  42. matrices. Differences between a CrossCorrelationTable and a Covariance:
  43. 1. a Covariance matrix is always positive definite, for a cross-correlation table this is only guaranteed for
  44. lag time tau = 0.
  45. 2. The elements c[i][j] in a Covariance always satisfy |c[i][j]/sqrt(c[i][i]*c[j][j])| <= 1, this is
  46. in general not the case for cross-correlations.
  47. */
  48. autoCrossCorrelationTable CrossCorrelationTable_create (integer dimension);
  49. autoCrossCorrelationTable CrossCorrelationTable_createSimple (conststring32 covars, conststring32 centroid, integer numberOfSamples);
  50. /* (sum(i,j=1..dimension, i!=j; C[i][j]^2))/(dimension*(dimension-1)) */
  51. double CrossCorrelationTable_getDiagonalityMeasure (CrossCorrelationTable me);
  52. autoCrossCorrelationTable CrossCorrelationTable_Diagonalizer_diagonalize (CrossCorrelationTable me, Diagonalizer thee);
  53. double CrossCorrelationTableList_getDiagonalityMeasure (CrossCorrelationTableList me, double *w, integer start, integer end);
  54. double CrossCorrelationTableList_Diagonalizer_getDiagonalityMeasure (CrossCorrelationTableList me, Diagonalizer thee, double *w, integer start, integer end);
  55. autoCrossCorrelationTableList CrossCorrelationTableList_createTestSet (integer dimension, integer n, int firstPositiveDefinite, double sigma);
  56. autoDiagonalizer Diagonalizer_create (integer dimension);
  57. autoSound Sound_to_Sound_BSS (Sound me,
  58. double startTime, double endTime, integer numberOfCrossCorrelations, double lagStep,
  59. integer maxNumberOfIterations, double delta_w, int method);
  60. autoSound Sound_whitenChannels (Sound me, double varianceFraction);
  61. autoSound Sound_Covariance_whitenChannels (Sound me, Covariance thee, double varianceFraction);
  62. void MixingMatrix_CrossCorrelationTableList_improveUnmixing (MixingMatrix me, CrossCorrelationTableList thee, integer maxNumberOfIterations, double tol, int method);
  63. void MixingMatrix_Sound_improveUnmixing (MixingMatrix me, Sound thee,
  64. double startTime, double endTime, integer numberOfCrossCorrelations, double lagStep,
  65. integer maxNumberOfIterations, double tol, int method);
  66. /*
  67. Determine the matrix that diagonalizes a series of CrossCorrelationTables as well as possible.
  68. */
  69. autoDiagonalizer CrossCorrelationTableList_to_Diagonalizer (CrossCorrelationTableList me, integer maxNumberOfIterations, double tol, int method);
  70. void Diagonalizer_CrossCorrelationTableList_improveDiagonality (Diagonalizer me, CrossCorrelationTableList thee,
  71. integer maxNumberOfIterations, double tol, int method);
  72. autoCrossCorrelationTableList CrossCorrelationTables_to_CrossCorrelationTableList (OrderedOf<structCrossCorrelationTable> *me);
  73. /*
  74. Determine V*C[k]*V' for k=1..n, where V is the diagonalizer matrix and C[k} the k-th CrossCorrelationTable.
  75. */
  76. autoCrossCorrelationTableList CrossCorrelationTableList_Diagonalizer_diagonalize (CrossCorrelationTableList me, Diagonalizer thee);
  77. autoDiagonalizer MixingMatrix_to_Diagonalizer (MixingMatrix me);
  78. autoMixingMatrix Diagonalizer_to_MixingMatrix (Diagonalizer me);
  79. /*
  80. For multi-channel "sounds" like EEG signals.
  81. The cross-correlation between channel i and channel j is defined as
  82. sum(k=1..nsamples; (z[i][k] - mean[i])(z[j][k + lag] - mean[j])) / (nsamples - 1).
  83. */
  84. autoCrossCorrelationTable Sound_to_CrossCorrelationTable (Sound me,
  85. double startTime, double endTime, double lagStep);
  86. autoCrossCorrelationTable Sounds_to_CrossCorrelationTable_combined (Sound me, Sound thee,
  87. double relativeStartTime, double relativeEndTime, double lagStep);
  88. // The covariance is the cross-correlation with lag 0.
  89. autoCovariance Sound_to_Covariance_channels (Sound me, double startTime, double endTime);
  90. /*
  91. Determine a CrossCorrelationTable for lags (k-1)*lagStep, where k = 1...n.
  92. */
  93. autoCrossCorrelationTableList Sound_to_CrossCorrelationTableList (Sound me,
  94. double startTime, double endTime, integer numberOfCrossCorrelations, double lagStep);
  95. autoMixingMatrix TableOfReal_to_MixingMatrix (TableOfReal me);
  96. autoMixingMatrix Sound_to_MixingMatrix (Sound me,
  97. double startTime, double endTime, integer numberOfCrossCorrelations, double lagStep,
  98. integer maxNumberOfIterations, double tol, int method);
  99. #endif /*_ICA_h_ */