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