def _compute_cross_correlation(self, small_ts, input_ts_h5): """ Cross-correlate two one-dimensional arrays. Return a CrossCorrelation datatype with result. """ # (tpts, nodes, nodes, state-variables, modes) result_shape = self._result_shape(small_ts.data.shape) self.log.info("result shape will be: %s" % str(result_shape)) result = numpy.zeros(result_shape) # TODO: For region level, 4s, 2000Hz, this takes ~3hours...(which makes node_coherence seem positively speedy # Probably best to add a keyword for offsets, so we just compute +- some "small" range... # One inter-node correlation, across offsets, for each state-var & mode. for mode in range(result_shape[4]): for var in range(result_shape[3]): data = input_ts_h5.data[:, var, :, mode] data = data - data.mean(axis=0)[numpy.newaxis, :] # TODO: Work out a way around the 4 level loop: for n1 in range(result_shape[1]): for n2 in range(result_shape[2]): result[:, n1, n2, var, mode] = correlate(data[:, n1], data[:, n2], mode="same") self.log.debug("result") self.log.debug(narray_describe(result)) offset = (small_ts.sample_period * numpy.arange(-numpy.floor(result_shape[0] / 2.0), numpy.ceil(result_shape[0] / 2.0))) cross_corr = CrossCorrelation(source=small_ts, array_data=result, time=offset) return cross_corr
def find_delta_with_xcorr(signal1, signal2): if signal1.std() == 0 or signal2.std() == 0: return True, None, None signal1 -= signal1.mean() signal2 -= signal2.mean() signal1 /= signal1.std() signal2 /= signal2.std() nsamples = signal1.shape[0] xcorr = signaltools.correlate(signal1, signal2) # print(xcorr) dt = np.arange(1 - nsamples, nsamples) filtered_xcorr = xcorr[len(xcorr) // 2 - 20:len(xcorr) // 2 + 20] recovered_time_shift = dt[len(xcorr) // 2 - 20 + filtered_xcorr.argmax()] return False, xcorr, recovered_time_shift
def evaluate(self): """ Cross-correlate two one-dimensional arrays. """ cls_attr_name = self.__class__.__name__ + ".time_series" self.time_series.trait["data"].log_debug(owner=cls_attr_name) #(tpts, nodes, nodes, state-variables, modes) result_shape = self.result_shape(self.time_series.data.shape) LOG.info("result shape will be: %s" % str(result_shape)) result = numpy.zeros(result_shape) #TODO: For region level, 4s, 2000Hz, this takes ~3hours...(which makes node_coherence seem positively speedy...) # Probably best to add a keyword for offsets, so we just compute +- some "small" range... # One inter-node correlation, across offsets, for each state-var & mode. for mode in range(result_shape[4]): for var in range(result_shape[3]): data = self.time_series.data[:, var, :, mode] data = data - data.mean(axis=0)[numpy.newaxis, :] #TODO: Work out a way around the 4 level loop: for n1 in range(result_shape[1]): for n2 in range(result_shape[2]): result[:, n1, n2, var, mode] = correlate(data[:, n1], data[:, n2], mode="same") util.log_debug_array(LOG, result, "result") offset = (self.time_series.sample_period * numpy.arange(-numpy.floor(result_shape[0] / 2.0), numpy.ceil(result_shape[0] / 2.0))) cross_corr = temporal_correlations.CrossCorrelation( source=self.time_series, array_data=result, time=offset, use_storage=False) return cross_corr
def evaluate(self): """ Cross-correlate two one-dimensional arrays. """ cls_attr_name = self.__class__.__name__+".time_series" self.time_series.trait["data"].log_debug(owner = cls_attr_name) #(tpts, nodes, nodes, state-variables, modes) result_shape = self.result_shape(self.time_series.data.shape) LOG.info("result shape will be: %s" % str(result_shape)) result = numpy.zeros(result_shape) #TODO: For region level, 4s, 2000Hz, this takes ~3hours... (which makes node_coherence seem positively speedy...) #TODO: Probably best to add a keyword for offsets, so we just compute +- some "small" range... #One inter-node correllation, across offsets, for each state-var & mode. for mode in range(result_shape[4]): for var in range(result_shape[3]): data = self.time_series.data[:, var, :, mode] data = data - data.mean(axis=0)[numpy.newaxis, :] #TODO: Work out a way around the 4 level loop, for n1 in range(result_shape[1]): for n2 in range(result_shape[2]): result[:, n1, n2, var, mode] = correlate(data[:, n1], data[:, n2], mode="same") util.log_debug_array(LOG, result, "result") offset = (self.time_series.sample_period * numpy.arange(-(numpy.floor(result_shape[0] / 2.0)), numpy.ceil(result_shape[0] / 2.0))) cross_corr = temporal_correlations.CrossCorrelation( source = self.time_series, array_data = result, time = offset, use_storage = False) return cross_corr
def center_signal(sig, shift=0.0): ''' Given a signal on a periodic domain with a dominant sine component, remove the phase shift from the signal and return it. DEV: This function seems to cause the direction to randomly switch sides ''' theta_vals = linspace(0, 2 * pi, len(sig)) sig0 = sin(theta_vals) sig0 = heaviside(theta_vals - pi) mdpt = int(len(sig) / 2) max_ind = argmax(abs(correlate(sig, sig0))) max_ind = mod(max_ind, len(sig)) out_sig = hstack((sig[max_ind:], sig[:max_ind])) # this is a pretty shaky way to get this phase shift if sum(out_sig[:mdpt]) > sum(out_sig[mdpt:]): out_sig = hstack((out_sig[mdpt:], out_sig[:mdpt])) return out_sig
for val in data[x][y]: theta = mt.atan(val[1]/val[0]) if val[0] != 0 else (mt.pi/2 if val[1] > 0 else 1.5*mt.pi) if lastTheta is not None and abs(theta - lastTheta) > 1: theta = theta + mt.pi lastTheta = theta polarData[x][y].append([mt.sqrt(val[0]**2 + val[1]**2), theta]) # print(polarData) plt.clf() wave1 = fft([theta[1] for theta in polarData[0][0]]) wave2 = fft([theta[1] for theta in polarData[0][1]]) wave1 = wave1[3:len(wave1)-2] wave2 = wave2[3:len(wave2)-2] plt.plot(wave1) plt.plot(wave2) xcor = np.argmax(correlate(wave1, wave2)) theta = theta*(1-BETA) + xcor*BETA print(theta) # plt.polar(0, 500) # plt.polar(theta, np.mean([dat[0] for dat in polarData[0][0]]), marker='o') # plt.plot([theta[1] for theta in polarData[1][0]]) # plt.plot([theta[1] for theta in polarData[1][1]]) plt.draw() plt.pause(0.0001) # Reset data = [[[], []], [[], []]] elif line.find("#Receiving") != -1: # This is the beginning, let's set defaults ready = True data = [[[], []], [[], []]] # [Antenna0rx, Antenna1rx] each with [Antenna0tx, Antenna1tx] plt.ion()