Sound EEG_to_Sound_modulated (EEG me, double baseFrequency, double channelBandwidth, const wchar_t *channelRanges) { try { long numberOfChannels; autoNUMvector <long> channelNumbers (NUMstring_getElementsOfRanges (channelRanges, my d_numberOfChannels, & numberOfChannels, NULL, L"channel", true), 1); double maxFreq = baseFrequency + my d_numberOfChannels * channelBandwidth; double samplingFrequency = 2 * maxFreq; samplingFrequency = samplingFrequency < 44100 ? 44100 : samplingFrequency; autoSound thee = Sound_createSimple (1, my xmax - my xmin, samplingFrequency); for (long i = 1; i <= numberOfChannels; i++) { long ichannel = channelNumbers[i]; double fbase = baseFrequency;// + (ichannel - 1) * channelBandwidth; autoSound si = Sound_extractChannel (my d_sound, ichannel); autoSpectrum spi = Sound_to_Spectrum (si.peek(), 1); Spectrum_passHannBand (spi.peek(), 0.5, channelBandwidth - 0.5, 0.5); autoSpectrum spi_shifted = Spectrum_shiftFrequencies (spi.peek(), fbase, samplingFrequency / 2, 30); autoSound resampled = Spectrum_to_Sound (spi_shifted.peek()); long nx = resampled -> nx < thy nx ? resampled -> nx : thy nx; for (long j = 1; j <= nx; j++) { thy z[1][j] += resampled -> z[1][j]; } } Vector_scale (thee.peek(), 0.99); return thee.transfer(); } catch (MelderError) { Melder_throw (me, ": no playable sound created."); } }
static long *EEG_channelNames_to_channelNumbers (EEG me, wchar_t **channelNames, long numberOfChannelNames) { try { autoNUMvector<long> channelNumbers (1, numberOfChannelNames); for (long i = 1; i <= numberOfChannelNames; i++) { for (long j = 1; j <= my d_numberOfChannels; j++) { if (Melder_wcsequ (channelNames[i], my d_channelNames[j])) { channelNumbers[i] = j; } } if (channelNumbers[i] == 0) { Melder_throw ("Channel name \"", channelNames[i], "\" not found."); } } return channelNumbers.transfer(); } catch (MelderError) { Melder_throw (me, ": channelNames not found."); } }
EEG EEG_and_PCA_to_EEG_principalComponents (EEG me, PCA thee, long numberOfComponents) { try { if (numberOfComponents <= 0 || numberOfComponents > thy numberOfEigenvalues) { numberOfComponents = thy numberOfEigenvalues; } numberOfComponents = numberOfComponents > my d_numberOfChannels ? my d_numberOfChannels : numberOfComponents; autoNUMvector<long> channelNumbers (EEG_channelNames_to_channelNumbers (me, thy labels, thy dimension), 1); autoEEG him = (EEG) Data_copy (me); autoSound pc = Sound_and_PCA_to_Sound_pc_selectedChannels (my d_sound, thee, numberOfComponents, channelNumbers.peek(), thy dimension); for (long i = 1; i <= thy dimension; i++) { long ichannel = channelNumbers[i]; NUMvector_copyElements<double> (pc -> z[i], his d_sound -> z[ichannel], 1, his d_sound -> nx); } EEG_setChannelNames_selected (him.peek(), L"pc", channelNumbers.peek(), thy dimension); return him.transfer(); } catch (MelderError) { Melder_throw (me, ": not projected."); } }
EEG EEG_and_PCA_to_EEG_whiten (EEG me, PCA thee, long numberOfComponents) { try { if (numberOfComponents <= 0 || numberOfComponents > thy numberOfEigenvalues) { numberOfComponents = thy numberOfEigenvalues; } numberOfComponents = numberOfComponents > my numberOfChannels ? my numberOfChannels : numberOfComponents; autoNUMvector<long> channelNumbers (EEG_channelNames_to_channelNumbers (me, thy labels, thy dimension), 1); autoEEG him = (EEG) Data_copy (me); autoSound white = Sound_and_PCA_whitenSelectedChannels (my sound, thee, numberOfComponents, channelNumbers.peek(), thy dimension); for (long i = 1; i <= thy dimension; i++) { long ichannel = channelNumbers[i]; NUMvector_copyElements<double> (white -> z[i], his sound -> z[ichannel], 1, his sound -> nx); } EEG_setChannelNames_selected (him.peek(), U"wh", channelNumbers.peek(), thy dimension); return him.transfer(); } catch(MelderError) { Melder_throw (me, U": not whitened with ", thee); } }
EEG EEG_to_EEG_bss (EEG me, double startTime, double endTime, long ncovars, double lagTime, const wchar_t *channelRanges, int whiteningMethod, int diagonalizerMethod, long maxNumberOfIterations, double tol) { try { // autowindow if (startTime == endTime) { startTime = my xmin; endTime = my xmax; } // don't allow times outside domain if (startTime < my xmin) { startTime = my xmin; } if (endTime > my xmax) { endTime = my xmax; } long numberOfChannels; autoNUMvector <long> channelNumbers (NUMstring_getElementsOfRanges (channelRanges, my d_numberOfChannels, & numberOfChannels, NULL, L"channel", true), 1); autoEEG thee = my f_extractPart (startTime, endTime, true); if (whiteningMethod != 0) { bool fromCorrelation = whiteningMethod == 2; autoPCA pca = EEG_to_PCA (thee.peek(), thy xmin, thy xmax, channelRanges, fromCorrelation); autoEEG white = EEG_and_PCA_to_EEG_whiten (thee.peek(), pca.peek(), 0); thee.reset (white.transfer()); } autoMixingMatrix mm = Sound_to_MixingMatrix (thy d_sound, startTime, endTime, ncovars, lagTime, maxNumberOfIterations, tol, diagonalizerMethod); autoEEG him = EEG_copyWithoutSound (me); his d_sound = Sound_and_MixingMatrix_unmix (my d_sound, mm.peek()); EEG_setChannelNames_selected (him.peek(), L"ic", channelNumbers.peek(), numberOfChannels); // Calculate the cross-correlations between eye-channels and the ic's return him.transfer(); } catch (MelderError) { Melder_throw (me, ": no independent components determined."); } }