/* This is for multi-channel "sounds" like EEG signals. The cross-correlation between channel i and channel j is defined as sum(k=1..nsamples, (z[i][k] - mean[i])(z[j][k + tau] - mean[j]))*samplingTime */ autoCrossCorrelationTable Sound_to_CrossCorrelationTable (Sound me, double startTime, double endTime, double lagStep) { try { if (endTime <= startTime) { startTime = my xmin; endTime = my xmax; } long lag = (long) floor (lagStep / my dx); // ppgb: voor al dit soort dingen geldt: waarom afronden naar beneden? long i1 = Sampled_xToNearestIndex (me, startTime); if (i1 < 1) { i1 = 1; } long i2 = Sampled_xToNearestIndex (me, endTime); if (i2 > my nx) { i2 = my nx; } i2 -= lag; long nsamples = i2 - i1 + 1; if (nsamples <= my ny) { Melder_throw (U"Not enough samples, choose a longer interval."); } autoCrossCorrelationTable thee = CrossCorrelationTable_create (my ny); NUMcrossCorrelate_rows (my z, my ny, i1, i2, lag, thy data, thy centroid, my dx); thy numberOfObservations = nsamples; return thee; } catch (MelderError) { Melder_throw (me, U": CrossCorrelationTable not created."); } }
/* This is for multi-channel "sounds" like EEG signals. The cross-correlation between channel i and channel j is defined as sum(k=1..nsamples, (z[i][k] - mean[i])(z[j][k + tau] - mean[j]))*samplingTime */ CrossCorrelationTable Sound_to_CrossCorrelationTable (Sound me, double startTime, double endTime, double lagTime) { try { if (endTime <= startTime) { startTime = my xmin; endTime = my xmax; } long lag = lagTime / my dx; long i1 = Sampled_xToNearestIndex (me, startTime); if (i1 < 1) { i1 = 1; } long i2 = Sampled_xToNearestIndex (me, endTime); if (i2 > my nx) { i2 = my nx; } i2 -= lag; long nsamples = i2 - i1 + 1; if (nsamples <= my ny) { Melder_throw ("Not enough samples, choose a longer interval."); } autoCrossCorrelationTable thee = CrossCorrelationTable_create (my ny); NUMcrossCorrelate_rows (my z, my ny, i1, i2, lag, thy data, thy centroid, my dx); thy numberOfObservations = nsamples; return thee.transfer(); } catch (MelderError) { Melder_throw (me, ": CrossCorrelationTable not created."); } }
/* Calculate the CrossCorrelationTable between the channels of two multichannel sounds irrespective of the domains. * Both sounds are treated as if their domain runs from 0 to duration. * Outside the chosen interval the sounds are assumed to be zero */ autoCrossCorrelationTable Sounds_to_CrossCorrelationTable_combined (Sound me, Sound thee, double relativeStartTime, double relativeEndTime, double lagStep) { try { if (my dx != thy dx) { Melder_throw (U"Sampling frequencies must be equal."); } if (relativeEndTime <= relativeStartTime) { relativeStartTime = my xmin; relativeEndTime = my xmax; } long ndelta = (long) floor (lagStep / my dx), nchannels = my ny + thy ny; long i1 = Sampled_xToNearestIndex (me, relativeStartTime); if (i1 < 1) { i1 = 1; } long i2 = Sampled_xToNearestIndex (me, relativeEndTime); if (i2 > my nx) { i2 = my nx; } i2 -= ndelta; long nsamples = i2 - i1 + 1; if (nsamples <= nchannels) { Melder_throw (U"Not enough samples"); } autoCrossCorrelationTable him = CrossCorrelationTable_create (nchannels); autoNUMvector<double *> data (1, nchannels); for (long i = 1; i <= my ny; i++) { data[i] = my z[i]; } for (long i = 1; i <= thy ny; i++) { data[i + my ny] = thy z[i]; } NUMcrossCorrelate_rows (data.peek(), nchannels, i1, i2, ndelta, his data, his centroid, my dx); his numberOfObservations = nsamples; return him; } catch (MelderError) { Melder_throw (me, U": CrossCorrelationTable not created."); } }