| 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"cxx . . $ `<file>/dwtools/ICA.h`/ |
| 24 | #include "Sound_and_PCA.h"cxx . . $ `<file>/dwtools/Sound_and_PCA.h`/ |
| 25 | #include "Sound_extensions.h"cxx . . $ `<file>/dwtools/Sound_extensions.h`/ |
| 26 | #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 27 | |
| 28 | static void checkChannelsWithinRangecxx . . $ checkChannelsWithinRange(3e97dfcc9cbfd45). (constINTVECcxx . . $ constINTVEC# const& channels, integercxx . . $ integer# min, integercxx . . $ integer# max) { |
| 29 | for (integercxx . . $ integer# i = 1; i <= channels.sizecxx . . $ constvector#size.; i ++) |
| 30 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (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 | autoPCAcxx . . $ autoPCA# Sound_to_PCA_channelscxx . . $ Sound_to_PCA_channels(d972e64f26fbcca8). (Soundcxx . . $ Sound# me, double startTime, double endTime) { |
| 35 | try { |
| 36 | /* |
| 37 | Covariance is cross-correlation with lag time 0 |
| 38 | */ |
| 39 | autoCrossCorrelationTablecxx . . $ autoCrossCorrelationTable# thee = Sound_to_CrossCorrelationTablecxx . . $ Sound_to_CrossCorrelationTable(365ea64c78210068). (me, startTime, endTime, 0.0); |
| 40 | autoPCAcxx . . $ autoPCA# him = SSCP_to_PCAcxx . . $ SSCP_to_PCA(67c2aab67f958214). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 41 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 42 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 43 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no PCA created."); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | autoSoundcxx . . $ autoSound# Sound_PCA_to_Sound_pc_selectedChannelscxx . . $ Sound_PCA_to_Sound_pc_selectedChannels(2ecd99f663df0ea7). (Soundcxx . . $ Sound# me, PCAcxx . . $ PCA# thee, integercxx . . $ integer# numberOfComponents, constINTVECcxx . . $ constINTVEC# const& channels) { |
| 48 | try { |
| 49 | if (numberOfComponents <= 0 || numberOfComponents > thycxx . . $ `melder/melder.h:45:9`! numberOfEigenvaluescxx . . $ structEigen#numberOfEigenvalues.) |
| 50 | numberOfComponents = thycxx . . $ `melder/melder.h:45:9`! numberOfEigenvaluescxx . . $ structEigen#numberOfEigenvalues.; |
| 51 | |
| 52 | numberOfComponents = numberOfComponents > mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny. ? mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny. : numberOfComponents; |
| 53 | |
| 54 | checkChannelsWithinRangecxx . . $ checkChannelsWithinRange(3e97dfcc9cbfd45). (channels, 1, mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.); |
| 55 | |
| 56 | autoSoundcxx . . $ autoSound# him = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me); |
| 57 | /* |
| 58 | R ['i',j] = E(i,k]*S ['k',j] |
| 59 | use kij-variant for faster inner loop |
| 60 | */ |
| 61 | for (integercxx . . $ integer# k = 1; k <= thycxx . . $ `melder/melder.h:45:9`! dimensioncxx . . $ structEigen#dimension.; k ++) |
| 62 | for (integercxx . . $ integer# i = 1; i <= numberOfComponents; i ++) { |
| 63 | const double ev_ik = thycxx . . $ `melder/melder.h:45:9`! eigenvectorscxx . . $ structEigen#eigenvectors. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).k]; |
| 64 | for (integercxx . . $ integer# j = 1; j <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; j ++) |
| 65 | hiscxx . . $ `melder/melder.h:47:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).channels [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i]] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] += ev_ik * mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).channels [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).k]] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j]; |
| 66 | } |
| 67 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 68 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 69 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no principal components calculated with ", thee); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | autoSoundcxx . . $ autoSound# Sound_PCA_principalComponentscxx . . $ Sound_PCA_principalComponents(d75aa346150067bb). (Soundcxx . . $ Sound# me, PCAcxx . . $ PCA# thee, integercxx . . $ integer# numberOfComponents) { |
| 74 | autoINTVECcxx . . $ autoINTVEC# channels = to_INTVECcxx . . $ to_INTVEC(dab5d3ef28d6a25). (mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.); |
| 75 | return Sound_PCA_to_Sound_pc_selectedChannelscxx . . $ Sound_PCA_to_Sound_pc_selectedChannels(2ecd99f663df0ea7). (me, thee, numberOfComponents, channelscxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 76 | } |
| 77 | |
| 78 | autoSoundcxx . . $ autoSound# Sound_PCA_whitenSelectedChannelscxx . . $ Sound_PCA_whitenSelectedChannels(2ecd99f663df0ea7). (Soundcxx . . $ Sound# me, PCAcxx . . $ PCA# thee, integercxx . . $ integer# numberOfComponents, constINTVECcxx . . $ constINTVEC# const& channels) { |
| 79 | try { |
| 80 | if (numberOfComponents <= 0 || numberOfComponents > thycxx . . $ `melder/melder.h:45:9`! numberOfEigenvaluescxx . . $ structEigen#numberOfEigenvalues.) |
| 81 | numberOfComponents = thycxx . . $ `melder/melder.h:45:9`! numberOfEigenvaluescxx . . $ structEigen#numberOfEigenvalues.; |
| 82 | |
| 83 | checkChannelsWithinRangecxx . . $ checkChannelsWithinRange(3e97dfcc9cbfd45). (channels, 1, mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.); |
| 84 | |
| 85 | autoMATcxx . . $ autoMAT# whiten = raw_MATcxx . . $ raw_MAT(3d09731056d2b97d). (thycxx . . $ `melder/melder.h:45:9`! dimensioncxx . . $ structEigen#dimension., thycxx . . $ `melder/melder.h:45:9`! dimensioncxx . . $ structEigen#dimension.); |
| 86 | // W = E D^(-1/2) E' from http://cis.legacy.ics.tkk.fi/aapo/papers/IJCNN99_tutorialweb/node26.html |
| 87 | for (integercxx . . $ integer# i = 1; i <= thycxx . . $ `melder/melder.h:45:9`! dimensioncxx . . $ structEigen#dimension.; i ++) { |
| 88 | for (integercxx . . $ integer# j = i; j <= thycxx . . $ `melder/melder.h:45:9`! dimensioncxx . . $ structEigen#dimension.; j ++) { |
| 89 | longdoublecxx . . $ longdouble# wij = 0.0; |
| 90 | for (integercxx . . $ integer# k = 1; k <= numberOfComponents; k ++) |
| 91 | wij += thycxx . . $ `melder/melder.h:45:9`! eigenvectorscxx . . $ structEigen#eigenvectors. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).k] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * thycxx . . $ `melder/melder.h:45:9`! eigenvectorscxx . . $ structEigen#eigenvectors. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).k] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (thycxx . . $ `melder/melder.h:45:9`! eigenvaluescxx . . $ structEigen#eigenvalues. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k]); |
| 92 | whiten [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = whiten [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).j] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = double (wij); |
| 93 | } |
| 94 | } |
| 95 | autoSoundcxx . . $ autoSound# him = Sound_createcxx . . $ Sound_create(39ab106d6b9a0135). (mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny., mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx., mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1.); |
| 96 | for (integercxx . . $ integer# k = 1; k <= channels.sizecxx . . $ constvector#size.; k ++) { |
| 97 | for (integercxx . . $ integer# i = 1; i <= channels.sizecxx . . $ constvector#size.; i ++) { |
| 98 | const double w_ik = whiten [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).k]; |
| 99 | for (integercxx . . $ integer# j = 1; j <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; j ++) |
| 100 | hiscxx . . $ `melder/melder.h:47:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).channels [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i]] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] += w_ik * mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).channels [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).k]] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j]; |
| 101 | } |
| 102 | } |
| 103 | return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 104 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 105 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Sound not created."); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | autoSoundcxx . . $ autoSound# Sound_PCA_whitenChannelscxx . . $ Sound_PCA_whitenChannels(d75aa346150067bb). (Soundcxx . . $ Sound# me, PCAcxx . . $ PCA# thee, integercxx . . $ integer# numberOfComponents) { |
| 110 | autoINTVECcxx . . $ autoINTVEC# channels = to_INTVECcxx . . $ to_INTVEC(dab5d3ef28d6a25). (mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.); |
| 111 | return Sound_PCA_whitenSelectedChannelscxx . . $ Sound_PCA_whitenSelectedChannels(2ecd99f663df0ea7). (me, thee, numberOfComponents, channelscxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 112 | } |
| 113 | |
| 114 | /* End of file Sound_and_PCA.cpp */ |