| 1 | #ifndef _ICA_h_ |
| 2 | #define _ICA_h_ |
| 3 | /* ICA.h |
| 4 | * |
| 5 | * Copyright (C) 2010-2020 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 | /* |
| 22 | djmw 20101202 Initial version |
| 23 | djmw 20120406 Latest modification. |
| 24 | */ |
| 25 | |
| 26 | #include "Covariance.h" |
| 27 | #include "Sound_and_MixingMatrix.h" |
| 28 | |
| 29 | |
| 30 | Thing_define (Diagonalizer, TableOfReal) { |
| 31 | }; |
| 32 | |
| 33 | Thing_define (CrossCorrelationTable, SSCP) { |
| 34 | void v1_info () |
| 35 | override; |
| 36 | }; |
| 37 | |
| 38 | Collection_define (CrossCorrelationTableList, OrderedOf, CrossCorrelationTable) { |
| 39 | void v1_info () |
| 40 | override; |
| 41 | }; |
| 42 | |
| 43 | /* |
| 44 | Cell [i,j] of a CrossCorrelationTable contains the cross-correlation between signal i and signal j (for one particular lag time). |
| 45 | For example, the CrossCorrelation of an n-channel sound is a nxn table where cell [i,j] contains the |
| 46 | cross-correlation of channel i with channel j for a particlular lag time tau. |
| 47 | In the statistical literature sometimes the cross-correlation between signals is also called "covariance". |
| 48 | However, the only thing a Covariance has in common with a CrossCorrelationTable is that both are symmetric |
| 49 | matrices. Differences between a CrossCorrelationTable and a Covariance: |
| 50 | 1. a Covariance matrix is always positive definite, for a cross-correlation table this is only guaranteed for |
| 51 | lag time tau = 0. |
| 52 | 2. The elements c [i] [j] in a Covariance always satisfy |c [i] [j] / sqrt (c [i] [i] * c [j] [j])| <= 1, this is |
| 53 | in general not the case for cross-correlations. |
| 54 | */ |
| 55 | |
| 56 | autoCrossCorrelationTable CrossCorrelationTable_create (integer dimension); |
| 57 | |
| 58 | autoCrossCorrelationTable CrossCorrelationTable_createSimple (conststring32 covars, conststring32 centroid, integer numberOfSamples); |
| 59 | |
| 60 | /* (sum(i,j=1..dimension, i!=j; C [i] [j]^2)) / (dimension * (dimension - 1)) */ |
| 61 | double CrossCorrelationTable_getDiagonalityMeasure (CrossCorrelationTable me); |
| 62 | |
| 63 | autoCrossCorrelationTable CrossCorrelationTable_Diagonalizer_diagonalize (CrossCorrelationTable me, Diagonalizer thee); |
| 64 | |
| 65 | double CrossCorrelationTableList_getDiagonalityMeasure (CrossCorrelationTableList me, double *w, integer start, integer end); |
| 66 | |
| 67 | double CrossCorrelationTableList_Diagonalizer_getDiagonalityMeasure (CrossCorrelationTableList me, Diagonalizer thee, double *w, integer start, integer end); |
| 68 | |
| 69 | autoCrossCorrelationTableList CrossCorrelationTableList_createTestSet (integer dimension, integer n, int firstPositiveDefinite, double sigma); |
| 70 | |
| 71 | autoDiagonalizer Diagonalizer_create (integer dimension); |
| 72 | |
| 73 | autoSound Sound_to_Sound_BSS (Sound me, |
| 74 | double startTime, double endTime, integer numberOfCrossCorrelations, double lagStep, |
| 75 | integer maxNumberOfIterations, double delta_w, int method); |
| 76 | |
| 77 | autoSound Sound_whitenChannels (Sound me, double varianceFraction); |
| 78 | autoSound Sound_Covariance_whitenChannels (Sound me, Covariance thee, double varianceFraction); |
| 79 | |
| 80 | void MixingMatrix_CrossCorrelationTableList_improveUnmixing (MixingMatrix me, CrossCorrelationTableList thee, integer maxNumberOfIterations, double tol, int method); |
| 81 | |
| 82 | void MixingMatrix_Sound_improveUnmixing (MixingMatrix me, Sound thee, |
| 83 | double startTime, double endTime, integer numberOfCrossCorrelations, double lagStep, |
| 84 | integer maxNumberOfIterations, double tol, int method); |
| 85 | /* |
| 86 | Determine the matrix that diagonalizes a series of CrossCorrelationTables as well as possible. |
| 87 | */ |
| 88 | autoDiagonalizer CrossCorrelationTableList_to_Diagonalizer (CrossCorrelationTableList me, integer maxNumberOfIterations, double tol, int method); |
| 89 | |
| 90 | void Diagonalizer_CrossCorrelationTableList_improveDiagonality (Diagonalizer me, CrossCorrelationTableList thee, |
| 91 | integer maxNumberOfIterations, double tol, int method); |
| 92 | |
| 93 | autoCrossCorrelationTableList CrossCorrelationTables_to_CrossCorrelationTableList (OrderedOf<structCrossCorrelationTable> *me); |
| 94 | /* |
| 95 | Determine V * C [k] * V' for k=1..n, where V is the diagonalizer matrix and C [k] the k-th CrossCorrelationTable. |
| 96 | */ |
| 97 | autoCrossCorrelationTableList CrossCorrelationTableList_Diagonalizer_diagonalize (CrossCorrelationTableList me, Diagonalizer thee); |
| 98 | |
| 99 | |
| 100 | autoDiagonalizer MixingMatrix_to_Diagonalizer (MixingMatrix me); |
| 101 | |
| 102 | autoMixingMatrix Diagonalizer_to_MixingMatrix (Diagonalizer me); |
| 103 | |
| 104 | /* |
| 105 | For multi-channel "sounds" like EEG signals. |
| 106 | The cross-correlation between channel i and channel j is defined as |
| 107 | sum (k=1..nsamples; (z [i] [k] - mean [i]) (z [j] [k + lag] - mean [j])) / (nsamples - 1). |
| 108 | */ |
| 109 | autoCrossCorrelationTable Sound_to_CrossCorrelationTable (Sound me, |
| 110 | double startTime, double endTime, double lagStep); |
| 111 | |
| 112 | autoCrossCorrelationTable Sounds_to_CrossCorrelationTable_combined (Sound me, Sound thee, |
| 113 | double relativeStartTime, double relativeEndTime, double lagStep); |
| 114 | |
| 115 | // The covariance is the cross-correlation with lag 0. |
| 116 | autoCovariance Sound_to_Covariance_channels (Sound me, double startTime, double endTime); |
| 117 | /* |
| 118 | Determine a CrossCorrelationTable for lags (k-1)*lagStep, where k = 1...n. |
| 119 | */ |
| 120 | autoCrossCorrelationTableList Sound_to_CrossCorrelationTableList (Sound me, |
| 121 | double startTime, double endTime, integer numberOfCrossCorrelations, double lagStep); |
| 122 | |
| 123 | autoMixingMatrix TableOfReal_to_MixingMatrix (TableOfReal me); |
| 124 | |
| 125 | autoMixingMatrix Sound_to_MixingMatrix (Sound me, |
| 126 | double startTime, double endTime, integer numberOfCrossCorrelations, double lagStep, |
| 127 | integer maxNumberOfIterations, double tol, int method); |
| 128 | |
| 129 | #endif /*_ICA_h_ */ |