def get_lag(sig1, sig2, srate=500): corr = correlate(sig1, sig2, mode="full") # corr /= np.max(corr) lags = correlation_lags(len(sig1), len(sig2), mode="full") / srate * 1000 lag = lags[np.argmax(corr)] return lag, lags, corr
def xcorr(x, y, mode='full', scaleopt='None'): rxy = signal.correlate(x, y, mode=mode) lags = signal.correlation_lags(len(x), len(y), mode=mode) if scaleopt == 'normalized': rxx = signal.correlate(x, x, mode='valid') ryy = signal.correlate(y, y, mode='valid') rxy /= np.sqrt(rxx * ryy) return rxy, lags
def align_fit_timeseries(fit1, fit2, dt): corr = correlate(fit1[0], fit2[0]) lags = correlation_lags(fit1[0].shape[0], fit2[0].shape[0]) best_lag = lags[corr.argmax()] if fit1.index[0] > fit2.index[0]: record_start_lag = np.where(fit2.index >= fit1.index[0])[0][0] else: record_start_lag = -np.where(fit1.index >= fit2.index[0])[0][0] net_lag = record_start_lag + best_lag return net_lag*dt, record_start_lag*dt, lags*dt
def xcorr(x, y, normalize=True, lags=True): rxx0 = np.max(signal.correlate(x, x)) ryy0 = np.max(signal.correlate(y, y)) rxy = signal.correlate(x, y) if normalize: rxy = rxy / np.sqrt(rxx0 * ryy0) if lags: return rxy, signal.correlation_lags(len(x), len(y)) else: return rxy
def receive(self) -> Tuple[Signal, Symbols, np.ndarray, RadarCube]: # There must be a recent transmission being cached in order to correlate if self.__transmission is None: raise RuntimeError( "Receiving from a matched filter joint must be preceeded by a transmission" ) # Receive information _, symbols, bits = Modem.receive(self) # Re-sample communication waveform signal = self._receiver.signal.resample(self.sampling_rate) resolution = self.range_resolution num_propagated_samples = int(2 * self.max_range / resolution) # Append additional samples if the signal is too short required_num_received_samples = self.__transmission.num_samples + num_propagated_samples if signal.num_samples < required_num_received_samples: signal.append_samples( Signal( np.zeros( (1, required_num_received_samples - signal.num_samples), dtype=complex), self.sampling_rate, signal.carrier_frequency)) # Remove possible overhead samples if signal is too long # resampled_signal.samples = resampled_signal.samples[:, :num_samples] correlation = abs( correlate( signal.samples, self.__transmission.samples, mode='valid', method='fft').flatten()) / self.__transmission.num_samples lags = correlation_lags(signal.num_samples, self.__transmission.num_samples, mode='valid') # Append zeros for correct depth estimation #num_appended_zeros = max(0, num_samples - resampled_signal.num_samples) #correlation = np.append(correlation, np.zeros(num_appended_zeros)) angle_bins = np.array([0.]) velocity_bins = np.array([0.]) range_bins = .5 * lags * resolution cube_data = np.array([[correlation]], dtype=float) cube = RadarCube(cube_data, angle_bins, velocity_bins, range_bins) return signal, symbols, bits, cube
def xcorr(x,y): """ Perform Cross-Correlation on x and y x : 1st signal y : 2nd signal returns lags : lags of correlation corr : coefficients of correlation """ corr = signal.correlate(x, y, mode="full") lags = signal.correlation_lags(len(x), len(y), mode="full") return lags, corr
def get_offset(spectrum, m, k, nurot, buoy_r, folded=False): """Determines the value of the offset by cross-correlating the stretched observed spectrum with the stretched TAR model computed for the parameters (m, k, nurot, buoyancy radius). Args: spectrum (Spectrum): m (int): Azimuthal order. k (int): Ordering index (Lee & Saio 97). nurot (float): Rotation frequency (in c/d). buoy_r (float): Buoyancy radius (in d). folded (bool): If True, it is assumed that the spectrum is folded in the inertial frame. Returns: float: Offset (in d). """ # Observed spectrum: switch to corotating frame and stretch the spectrum periods_co = in2co(spectrum.periods, m, k, nurot, folded) obs_stretched = stretch(m, k, periods_co, Eigenvalue(m, k), nurot) # Generate spectrum model spectrum_model = Spectrum() spectrum_model.generate( m, k, nurot * FACTOR_ROT, buoy_r * 86400, offset=0, nmax=110 ) spectrum_model = spectrum_model.filter(periodmax=np.max(spectrum.periods) + 0.5) # Stretch it mod_stretched = stretch(m, k, spectrum_model.periods_co, Eigenvalue(m, k), nurot) # Artifically broaden peaks in the spectra for the cross-correlation # (because the generated TAR model is not perfectly modelling the observed # periods) period_max = np.max(obs_stretched) + 0.5 sigma = 1e-3 sampling_rate = 3e4 obs_broaden = generate_spectrum(obs_stretched, sigma, period_max, sampling_rate) mod_broaden = generate_spectrum(mod_stretched, sigma, period_max, sampling_rate) # Compute the cross-correlation of the two broaden spectra correlation = correlate(obs_broaden, mod_broaden, mode="full") lags = correlation_lags(obs_broaden.size, mod_broaden.size, mode="full") / ( sampling_rate * buoy_r ) # Offset is between 0 and 1 (by definition) lag0 = len(lags) // 2 lag_P0 = lag0 + int(buoy_r * sampling_rate) offset = lags[lag0:lag_P0][np.argmax(correlation[lag0:lag_P0])] return offset
def _processData(self): source1 = self._dataSource[0]._data.loc[:, self._s1col].values source2 = self._dataSource[1]._data.loc[:, self._s2col].values if len(source1) and len(source2): lags = correlation_lags(len(source1), len(source2)) corr = correlate(source1, source2) xcorr = pd.DataFrame(index=lags, data=corr, columns=['XCorr']) else: xcorr = pd.DataFrame([], columns=['XCorr'], dtype='float64') return xcorr
def plotAkf(self,sampleFreq=1000): fig,ax=plt.subplots() tmp=copy.deepcopy(self.deviationFromNominal[:4194304]) akf = correlate(tmp, tmp, mode='full') akf =akf/np.max(akf) gausnoise=np.random.normal(scale=43e-9,size=tmp.size) gausnoise=gausnoise/np.max(gausnoise) akf_gausNoise = correlate (gausnoise,gausnoise, mode='full') akf_gausNoise=akf_gausNoise/np.max(akf_gausNoise) deltaT=1/sampleFreq lag=correlation_lags(tmp.size,tmp.size)*deltaT ax.plot(lag,akf,label=r'\textbf{\textbf{Aufgeizeichneter \textit{Jitter}}}') ax.plot(lag, akf_gausNoise, label=r'\textbf{Nomalverteilter Jitter} $\sigma = 43~\mathrm{ns}$') ax.set_xlabel(r'\textbf{Zeitverschiebungen} $\tau$ \textbf{in } s') ax.set_ylabel(r'\textbf{Autokorrelations Funktion} $AKF$ \textbf{in } R.U s') ax.grid() fig.show()
def compute_max_corr_1segment(segment, segments): maxcorr, maxcorr_lag = np.empty(len(segments)), np.empty(len(segments)) for j in range(len(segments)): a = segment b = segments[j] normalized_a = np.float32((a - np.mean(a)) / np.std(a)) normalized_b = np.float32((b - np.mean(b)) / np.std(b)) corr = np.float32( np.correlate(normalized_a, normalized_b, 'full') / max(len(a), len(b))) maxcorr[j] = np.float32(max(corr)) lag = signal.correlation_lags(normalized_a.size, normalized_b.size, mode='full') maxcorr_lag[j] = np.float16(lag[np.argmax(corr)]) return maxcorr, maxcorr_lag
ax2.set_ylim(ylim) ax.set_xlabel('Frequency (kyr$^{-1}$)') ax.set_yticks([]) ax2.set_yticks([]) fig.legend(bbox_to_anchor=(1,1), bbox_transform=ax.transAxes) plt.tight_layout() plt.savefig('../pgfs/band_choice_ecc_benth.pgf') # ------------ Fig for filter and lag -------------- band_e = sosfiltfilt(butter(2, [0.006,0.016], 'bandpass', output='sos', fs=10), ecc) band_b = sosfiltfilt(butter(2, [0.006,0.016], 'bandpass', output='sos', fs=10), benf(mil_t)) cor = correlate(norm(band_b[:-5000]),norm(band_e[:-5000])) cor/=np.max(cor) lags = correlation_lags(len(band_e[:-5000]),len(band_e[:-5000]))/10 max_cor_arg = np.argmax(cor) fig, axs = plt.subplots(2,1,figsize=SUB2,gridspec_kw={'height_ratios':[2,1]}) axs[0].invert_xaxis() ax2 = axs[0].twinx() axs[0].plot(-mil_t,band_e,'C0',label='Eccentricity',linewidth=2) ax2.plot(-mil_t,band_b,'C1',label='Ice Volume',linewidth=2) fig.legend(loc="upper left", bbox_to_anchor=(0,1), bbox_transform=axs[0].transAxes) # Align two plots starting y val #eps_lim = axs[0].get_ylim() #oce_lim = ax2.get_ylim() #low_oce_lim = (eps_lim[0]*(oce_lim[1]-ocean[0]) + eps_lim[1]*ocean[0] - # oce_lim[1]*eps(t[0]))/(eps_lim[1]-eps(t[0])) #ax2.set_ylim([low_oce_lim,oce_lim[1]]) # Plot middle section of cross cor
# detrend detrended_lfp = signal.detrend( expobj.lfp_signal[expobj.frame_start_time_actual:expobj. frame_end_time_actual])[slice] * -1 # downsample LFP signal to the same # of datapoints as # of frames in 2p calcium trace CaUpsampled1 = signal.resample(expobj.meanRawFluTrace, len(detrended_lfp))[slice] pj.make_general_plot([CaUpsampled1], figsize=[20, 3]) pj.make_general_plot([detrended_lfp], figsize=[20, 3]) # use numpy or scipy.signal .correlate to correlate the two timeseries correlated = signal.correlate(CaUpsampled1, detrended_lfp) lags = signal.correlation_lags(len(CaUpsampled1), len(detrended_lfp)) correlated /= np.max(correlated) f, axs = plt.subplots(nrows=3, ncols=1, figsize=[20, 9]) axs[0].plot(CaUpsampled1) axs[1].plot(detrended_lfp) # axs[2].plot(correlated) axs[2].plot(lags, correlated) f.show() #%% 1) defining trials to run for analysis # this list should line up with the analysis list for run_post4ap_trials trials ls = [ ['RL108 t-013'], ['RL108 t-011'],
repeaters here 37.151583 -121.609917 (SE of SJ) 37.659389 -121.933028 (NE of Fremont) """ # The first reference # diff the signal dPhase1_benGS = np.diff(np.unwrap(np.angle(benCplx[: nS_each - gate]))) dPhase1_domGS = np.diff(np.unwrap(np.angle(domCplx[: nS_each - gate]))) # remove the mean dPhase1_benGS = dPhase1_benGS - np.mean(dPhase1_benGS) dPhase1_domGS = dPhase1_domGS - np.mean(dPhase1_domGS) # correlate the signals dPhaseXcorr1 = signal.correlate(dPhase1_benGS, dPhase1_domGS) dPhaseXcorr1_lags = signal.correlation_lags(len(dPhase1_benGS), len(dPhase1_domGS)) dPhaseXcorr1_max = np.max(dPhaseXcorr1) refSig1Lag_samples = np.abs(dPhaseXcorr1_lags[np.argmax(dPhaseXcorr1)]) refSig1Lag_time = refSig1Lag_samples / sampleRate # same process, for the second signal dPhase2_benGS = np.diff(np.unwrap(np.angle(benCplx[-nS_each + gate :]))) dPhase2_domGS = np.diff(np.unwrap(np.angle(domCplx[-nS_each + gate :]))) dPhase_benGS = dPhase2_benGS - np.mean(dPhase2_benGS) dPhase_domGS = dPhase2_domGS - np.mean(dPhase2_domGS) dPhaseXcorr2 = signal.correlate(dPhase2_benGS, dPhase2_domGS) dPhaseXcorr2_lags = signal.correlation_lags(len(dPhase2_benGS), len(dPhase2_domGS)) dPhaseXcorr_max2 = np.max(dPhaseXcorr1)
inte[1:] = np.cumsum(ecc*et*dt)[:-1] return inte * c/(tau*et) + O_init/et + O0 - O0/et t = np.linspace(-800,0,100000) dt = [t[1]-t[0]] tau = 10 c = 200 O0 = -7 O_init = c*eps(t[0]) + O0 ocean = solve_ocean(t,tau,eps(t),O_init,c,O0) #Correlate with the vertically aligned ocean cor = correlate((ocean-O0)/c,eps(t)) cor/=np.max(cor) lags = correlation_lags(len(t),len(t))*dt fig, axs = plt.subplots(2, 1,figsize=SUB2) ax2 = axs[0].twinx() axs[0].plot(-t,eps(t),'C0',label='Eccentricity',linewidth=2) ax2.plot(-t,ocean,'C1',label='Ocean Temp',linewidth=2) fig.legend(loc="upper right", bbox_to_anchor=(1,1), bbox_transform=axs[0].transAxes) # Align two plots starting y val eps_lim = axs[0].get_ylim() oce_lim = ax2.get_ylim() low_oce_lim = (eps_lim[0]*(oce_lim[1]-ocean[0]) + eps_lim[1]*ocean[0] - oce_lim[1]*eps(t[0]))/(eps_lim[1]-eps(t[0])) ax2.set_ylim([low_oce_lim,oce_lim[1]]) # Plot middle section of cross cor ql = int(len(lags)/2.5) axs[1].plot(lags[ql:-ql],cor[ql:-ql],'C2',linewidth=2)
doven = date[0] + dtoven signal_date = np.concatenate( [doven, doven + np.timedelta64(1, 'D'), doven + np.timedelta64(2, 'D')]) signal = np.concatenate([oven, oven, oven]) # Up sample les données du four from scipy import interpolate dt = (signal_date - signal_date[0]).astype(np.float) / 1e9 #secondes f = interpolate.interp1d(dt, signal) dt_upsample = (date - date[0]) signal_upsample = f(dt_upsample) # CORRÉLATION signal_noise = new_temp[:, 490] corr = correlate(signal_noise, signal_upsample) lags = correlation_lags(len(signal_upsample), len(signal_noise)) * 2 #minutes corr /= np.max(corr) n = int(len(lags) / 2) fig, (ax_noise, ax_corr) = plt.subplots(2, 1, figsize=(10, 5)) ax_noise.plot(date, signal_noise, label='mesure DTS') ax_noise.plot(date, signal_upsample, label='Four') ax_noise.legend() ax_noise.set_title('Signaux') ax_noise.set_xlabel('Date (mois-jour heure)') ax_corr.plot(lags[n - 100:n + 100], corr[n - 100:n + 100]) ax_corr.axvline(lags[np.argmax(corr)], color='k', linestyle='--') ax_corr.set_title('Corrélation croisée') ax_corr.set_xlabel('Lag (minutes)') ax_noise.margins(0, 0.1) ax_corr.margins(0, 0.1)