| 1 | /* EEG_extensions.cpp |
| 2 | * |
| 3 | * Copyright (C) 2012-2019 David Weenink, 2015,2017,2018 Paul Boersma |
| 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. |
| 13 | * See the GNU 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 | #include "Correlation.h" |
| 20 | #include "EEG_extensions.h" |
| 21 | #include "NUM2.h" |
| 22 | #include "Sound_and_PCA.h" |
| 23 | #include "Sound_extensions.h" |
| 24 | #include "Spectrum_extensions.h" |
| 25 | #include "Sound_and_MixingMatrix.h" |
| 26 | #include "Sound_and_Spectrum.h" |
| 27 | |
| 28 | static autoEEG EEG_copyWithoutSound (EEG me) { |
| 29 | try { |
| 30 | autoEEG thee = EEG_create (my xmin, my xmax); |
| 31 | thy numberOfChannels = my numberOfChannels; |
| 32 | thy textgrid = Data_copy (my textgrid.get()); |
| 33 | thy channelNames = copy_STRVEC (my channelNames.get()); |
| 34 | return thee; |
| 35 | } catch (MelderError) { |
| 36 | Melder_throw (me, U": not copied."); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | static autoINTVEC EEG_channelNames_to_channelNumbers (EEG me, constSTRVEC const& channelNames) { |
| 41 | try { |
| 42 | autoINTVEC channelNumbers = zero_INTVEC (channelNames.size); |
| 43 | for (integer i = 1; i <= channelNames.size; i ++) { |
| 44 | for (integer j = 1; j <= my numberOfChannels; j ++) |
| 45 | if (Melder_equ (channelNames [i], my channelNames [j].get())) |
| 46 | channelNumbers [i] = j; |
| 47 | Melder_require (channelNumbers [i] != 0, |
| 48 | U"Channel name \"", channelNames [i], U"\" not found."); |
| 49 | } |
| 50 | return channelNumbers; |
| 51 | } catch (MelderError) { |
| 52 | Melder_throw (me, U": channelNames not found."); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static void EEG_setChannelNames_selected (EEG me, conststring32 precursor, constINTVECVU const& channelNumbers) { |
| 57 | autoMelderString name; |
| 58 | const conststring32 zero = U"0"; |
| 59 | for (integer i = 1; i <= channelNumbers.size; i ++) { |
| 60 | MelderString_copy (& name, precursor); |
| 61 | if (my numberOfChannels > 100) { |
| 62 | if (i < 10) |
| 63 | MelderString_append (& name, zero); |
| 64 | if (i < 100) |
| 65 | MelderString_append (& name, zero); |
| 66 | } else if (i < 10) { |
| 67 | MelderString_append (& name, zero); |
| 68 | } |
| 69 | MelderString_append (& name, i); |
| 70 | EEG_setChannelName (me, channelNumbers [i], name.string); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | autoCrossCorrelationTable EEG_to_CrossCorrelationTable (EEG me, double startTime, double endTime, double lagStep, constINTVECVU const& channels) { |
| 75 | try { |
| 76 | // autowindow |
| 77 | if (startTime == endTime) { |
| 78 | startTime = my xmin; |
| 79 | endTime = my xmax; |
| 80 | } |
| 81 | // don't allow times outside domain |
| 82 | if (startTime < my xmin) { |
| 83 | startTime = my xmin; |
| 84 | } |
| 85 | if (endTime > my xmax) { |
| 86 | endTime = my xmax; |
| 87 | } |
| 88 | autoEEG thee = EEG_extractPart (me, startTime, endTime, true); |
| 89 | autoSound soundPart = Sound_extractChannels (thy sound.get(), channels); |
| 90 | autoCrossCorrelationTable him = Sound_to_CrossCorrelationTable (soundPart.get(), startTime, endTime, lagStep); |
| 91 | // assign channel names |
| 92 | for (integer i = 1; i <= channels.size; i ++) { |
| 93 | const integer ichannel = channels [i]; |
| 94 | const conststring32 label = my channelNames [ichannel].get(); |
| 95 | TableOfReal_setRowLabel (him.get(), i, label); |
| 96 | TableOfReal_setColumnLabel (him.get(), i, label); |
| 97 | } |
| 98 | return him; |
| 99 | } catch (MelderError) { |
| 100 | Melder_throw (me, U": no CrossCorrelationTable calculated."); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | autoCovariance EEG_to_Covariance (EEG me, double startTime, double endTime, constINTVECVU const& channels) { |
| 105 | try { |
| 106 | const double lagStep = 0.0; |
| 107 | autoCrossCorrelationTable thee = EEG_to_CrossCorrelationTable (me, startTime, endTime, lagStep, channels); |
| 108 | autoCovariance him = Thing_new (Covariance); |
| 109 | thy structCrossCorrelationTable :: v1_copy (him.get()); |
| 110 | return him; |
| 111 | } catch (MelderError) { |
| 112 | Melder_throw (me, U": no Covariance calculated."); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | autoCrossCorrelationTableList EEG_to_CrossCorrelationTableList (EEG me, |
| 117 | double startTime, double endTime, integer numberOfCrossCorrelations, double lagStep, constINTVECVU const& channels) |
| 118 | { |
| 119 | try { |
| 120 | // autowindow |
| 121 | if (startTime == endTime) { |
| 122 | startTime = my xmin; |
| 123 | endTime = my xmax; |
| 124 | } |
| 125 | // don't allow times outside domain |
| 126 | if (startTime < my xmin) { |
| 127 | startTime = my xmin; |
| 128 | } |
| 129 | if (endTime > my xmax) { |
| 130 | endTime = my xmax; |
| 131 | } |
| 132 | autoEEG thee = EEG_extractPart (me, startTime, endTime, true); |
| 133 | autoSound soundPart = Sound_extractChannels (thy sound.get(), channels); |
| 134 | autoCrossCorrelationTableList him = Sound_to_CrossCorrelationTableList (soundPart.get(), |
| 135 | startTime, endTime, numberOfCrossCorrelations, lagStep); |
| 136 | return him; |
| 137 | } catch (MelderError) { |
| 138 | Melder_throw (me, U": no CrossCorrelationTables calculated."); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | autoPCA EEG_to_PCA (EEG me, double startTime, double endTime, constINTVECVU const& channels, int fromCorrelation) { |
| 143 | try { |
| 144 | autoCovariance cov = EEG_to_Covariance (me, startTime, endTime, channels); |
| 145 | autoPCA him; |
| 146 | if (fromCorrelation) { |
| 147 | autoCorrelation cor = SSCP_to_Correlation (cov.get()); |
| 148 | him = SSCP_to_PCA (cor.get()); |
| 149 | } else { |
| 150 | him = SSCP_to_PCA (cov.get()); |
| 151 | } |
| 152 | return him; |
| 153 | } catch (MelderError) { |
| 154 | Melder_throw (me, U": no PCA calculated."); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | autoEEG EEG_PCA_to_EEG_whiten (EEG me, PCA thee, integer numberOfComponents) { |
| 159 | try { |
| 160 | if (numberOfComponents <= 0 || numberOfComponents > thy numberOfEigenvalues) |
| 161 | numberOfComponents = thy numberOfEigenvalues; |
| 162 | numberOfComponents = std::min (numberOfComponents, my numberOfChannels); |
| 163 | |
| 164 | Melder_assert (thy labels.size == thy dimension); |
| 165 | autoINTVEC channelNumbers = EEG_channelNames_to_channelNumbers (me, thy labels.get()); |
| 166 | |
| 167 | autoEEG him = Data_copy (me); |
| 168 | autoSound white = Sound_PCA_whitenSelectedChannels (my sound.get(), thee, numberOfComponents, channelNumbers.get()); |
| 169 | for (integer i = 1; i <= channelNumbers.size; i ++) |
| 170 | his sound -> z.row (channelNumbers [i]) <<= white -> z.row (i); |
| 171 | |
| 172 | EEG_setChannelNames_selected (him.get(), U"wh", channelNumbers.get()); |
| 173 | return him; |
| 174 | } catch(MelderError) { |
| 175 | Melder_throw (me, U": not whitened with ", thee); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | autoEEG EEG_PCA_to_EEG_principalComponents (EEG me, PCA thee, integer numberOfComponents) { |
| 180 | try { |
| 181 | if (numberOfComponents <= 0 || numberOfComponents > thy numberOfEigenvalues) |
| 182 | numberOfComponents = thy numberOfEigenvalues; |
| 183 | numberOfComponents = std::min (numberOfComponents, my numberOfChannels); |
| 184 | |
| 185 | Melder_assert (thy labels.size == thy dimension); |
| 186 | autoINTVEC channelNumbers = EEG_channelNames_to_channelNumbers (me, thy labels.get()); |
| 187 | autoEEG him = Data_copy (me); |
| 188 | autoSound pc = Sound_PCA_to_Sound_pc_selectedChannels (my sound.get(), thee, numberOfComponents, channelNumbers.get()); |
| 189 | for (integer i = 1; i <= channelNumbers.size; i ++) |
| 190 | his sound -> z.row (channelNumbers [i]) <<= pc -> z.row (i); |
| 191 | |
| 192 | EEG_setChannelNames_selected (him.get(), U"pc", channelNumbers.get()); |
| 193 | return him; |
| 194 | } catch (MelderError) { |
| 195 | Melder_throw (me, U": not projected."); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | autoEEG EEG_to_EEG_bss (EEG me, double startTime, double endTime, integer numberOfCrossCorrelations, double lagStep, |
| 200 | constINTVECVU const& channels, int whiteningMethod, int diagonalizerMethod, integer maxNumberOfIterations, double tol, |
| 201 | autoMixingMatrix *p_resultingMixingMatrix) |
| 202 | { |
| 203 | try { |
| 204 | // autowindow |
| 205 | if (startTime == endTime) { |
| 206 | startTime = my xmin; |
| 207 | endTime = my xmax; |
| 208 | } |
| 209 | // don't allow times outside domain |
| 210 | if (startTime < my xmin) |
| 211 | startTime = my xmin; |
| 212 | |
| 213 | if (endTime > my xmax) |
| 214 | endTime = my xmax; |
| 215 | |
| 216 | autoEEG thee = EEG_extractPart (me, startTime, endTime, true); |
| 217 | if (whiteningMethod != 0) { |
| 218 | const bool fromCorrelation = ( whiteningMethod == 2 ); |
| 219 | autoPCA pca = EEG_to_PCA (thee.get(), thy xmin, thy xmax, channels, fromCorrelation); |
| 220 | autoEEG white = EEG_PCA_to_EEG_whiten (thee.get(), pca.get(), 0); |
| 221 | thee = white.move(); |
| 222 | } |
| 223 | autoMixingMatrix mm = Sound_to_MixingMatrix (thy sound.get(), |
| 224 | startTime, endTime, numberOfCrossCorrelations, lagStep, |
| 225 | maxNumberOfIterations, tol, diagonalizerMethod); |
| 226 | |
| 227 | autoEEG him = EEG_copyWithoutSound (me); |
| 228 | his sound = Sound_MixingMatrix_unmix (my sound.get(), mm.get()); |
| 229 | EEG_setChannelNames_selected (him.get(), U"ic", channels); |
| 230 | |
| 231 | // Calculate the cross-correlations between eye-channels and the ic's |
| 232 | |
| 233 | if (p_resultingMixingMatrix) |
| 234 | *p_resultingMixingMatrix = mm.move(); |
| 235 | return thee; |
| 236 | } catch (MelderError) { |
| 237 | Melder_throw (me, U": no independent components determined."); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | autoSound EEG_to_Sound_modulated (EEG me, double baseFrequency, double channelBandwidth, constINTVEC const& channelNumbers) { |
| 242 | try { |
| 243 | const double maxFreq = baseFrequency + my numberOfChannels * channelBandwidth; |
| 244 | const double samplingFrequency = std::max (2.0 * maxFreq, 44100.0); |
| 245 | autoSound thee = Sound_createSimple (1, my xmax - my xmin, samplingFrequency); |
| 246 | for (integer i = 1; i <= channelNumbers.size; i ++) { |
| 247 | const integer ichannel = channelNumbers [i]; |
| 248 | const double fbase = baseFrequency;// + (ichannel - 1) * channelBandwidth; |
| 249 | autoSound si = Sound_extractChannel (my sound.get(), ichannel); |
| 250 | autoSpectrum spi = Sound_to_Spectrum (si.get(), true); |
| 251 | Spectrum_passHannBand (spi.get(), 0.5, channelBandwidth - 0.5, 0.5); |
| 252 | autoSpectrum spi_shifted = Spectrum_shiftFrequencies (spi.get(), fbase, samplingFrequency / 2.0, 30); |
| 253 | autoSound resampled = Spectrum_to_Sound (spi_shifted.get()); |
| 254 | const integer nx = std::min (resampled -> nx, thy nx); |
| 255 | for (integer j = 1; j <= nx; j ++) { |
| 256 | thy z [1] [j] += resampled -> z [1] [j]; |
| 257 | } |
| 258 | } |
| 259 | Vector_scale (thee.get(), 0.99); |
| 260 | return thee; |
| 261 | } catch (MelderError) { |
| 262 | Melder_throw (me, U": no playable sound created."); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | autoSound EEG_to_Sound_frequencyShifted (EEG me, integer channel, double frequencyShift, double samplingFrequency, double maxAmp) { |
| 267 | try { |
| 268 | autoSound si = Sound_extractChannel (my sound.get(), channel); |
| 269 | autoSpectrum spi = Sound_to_Spectrum (si.get(), true); |
| 270 | autoSpectrum spi_shifted = Spectrum_shiftFrequencies (spi.get(), frequencyShift, samplingFrequency / 2.0, 30); |
| 271 | autoSound thee = Spectrum_to_Sound (spi_shifted.get()); |
| 272 | if (maxAmp > 0) |
| 273 | Vector_scale (thee.get(), maxAmp); |
| 274 | return thee; |
| 275 | } catch (MelderError) { |
| 276 | Melder_throw (me, U": channel not converted to sound."); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /* End of file EEG_extensions.cpp */ |