<jupyter_start><jupyter_text>Ampl_xcorr test<jupyter_code>#Import Modules
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

# Import NeuroDSP 
from neurodsp import sim
from neurodsp.filt import filter_signal
from neurodsp.sim import sim_combined
from neurodsp.utils import create_times
from neurodsp.timefrequency import amp_by_time
from neurodsp.plts.time_series import plot_time_series

# Import amp xcorr
from Amp_xcorr import *<jupyter_output><empty_output><jupyter_text>Simulate periodic signal<jupyter_code># set the random seed, for consistency simulating data
sim.set_random_seed(0)

# define length and sample freq
n_seconds = 20
fs = 1000

# create time vector
times = create_times(n_seconds, fs)
# desired lag
lag_in_ms = 50
lag_in_samples = round(lag_in_ms*(fs/1000))

# define simulated oscillation w/noise
components = {'sim_powerlaw' : {'exponent' : -1.4},
              'sim_oscillation' : {'freq' : 7.5}}
variances = [2, .2]

# simulate oscillation w/noise
lead_sig = sim_combined(n_seconds, fs, components, variances)
lag_sig = np.roll(lead_sig, lag_in_samples) #circular shift 50ms

# plot
plot_time_series(times, [lead_sig, lag_sig], ['Lead', 'Lag'])<jupyter_output><empty_output><jupyter_text>Compute amplitude crosscorrelation<jupyter_code># compute amplitude crosscorrelation
max_xcorr, max_xcorr_lag = amp_xcorr(lead_sig, lag_sig, fs, [4, 12])

print('Max correlation: ', max_xcorr)
print('Lag: ', max_xcorr_lag)<jupyter_output>Max correlation:  0.9951817798077868
Lag:  -51.0<jupyter_text>Bootstrap<jupyter_code># compute bootstrap
bs_dist = bootstrap_xcorr(lead_sig, lag_sig, fs, [4, 12])
 
# plot distribution with orginial xcorr
plt.hist(bs_dist)
plt.xlim([-.2, 1.1])
plt.axvline(x=max_xcorr, color= 'r', linestyle='-')
plt.xlabel('Crosscorrelation')
plt.ylabel('Number of Resamples')
plt.legend(['Original xcorr', 'Bootstrap dist'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10930962343096234, 'fraction_numerical': 0.02615062761506276, 'mean_word_length': 6.872427983539095, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '338491', 'license': 'MIT', 'n_tokens_mistral': 759, 'n_tokens_neox': 685, 'n_words': 161, 'path': 'Amp_xcorr_test.ipynb', 'repo_name': 'MicTott/Amp_xcorr', 'size': '1912'}