diff options
author | andreas128 <Andreas> | 2017-01-29 15:18:40 +0000 |
---|---|---|
committer | andreas128 <Andreas> | 2017-01-29 15:18:40 +0000 |
commit | 6e0b2512e45b7a6ca03187814742cb0fe08964cb (patch) | |
tree | 763a30041c7bb539a45a3af6df17465ea6a13c7a /src/dab_util.py | |
parent | a88e67cb485d6b4b7bc21aa3c9dedbab37190cb9 (diff) | |
download | ODR-StaticPrecorrection-6e0b2512e45b7a6ca03187814742cb0fe08964cb.tar.gz ODR-StaticPrecorrection-6e0b2512e45b7a6ca03187814742cb0fe08964cb.tar.bz2 ODR-StaticPrecorrection-6e0b2512e45b7a6ca03187814742cb0fe08964cb.zip |
Add Amp characterization in sync-measurement.ipynb
Diffstat (limited to 'src/dab_util.py')
-rw-r--r-- | src/dab_util.py | 69 |
1 files changed, 67 insertions, 2 deletions
diff --git a/src/dab_util.py b/src/dab_util.py index 2ad2af6..aa2030f 100644 --- a/src/dab_util.py +++ b/src/dab_util.py @@ -25,7 +25,7 @@ def freq_to_fft_sample(freq, fft_size, sampling_rate): freq_ratio = 1.0 * fft_size / sampling_rate return int(freq * freq_ratio + fft_size / 2) -def crop_signal(signal, n_window = 1000, n_zeros = 1000, debug = False): +def crop_signal(signal, n_window = 1000, n_zeros = 120000, debug = False): #signal = signal[-10:-1] mag = abs(signal) window = np.ones(n_window) / float(n_window) @@ -38,5 +38,70 @@ def crop_signal(signal, n_window = 1000, n_zeros = 1000, debug = False): plt.plot(mag < thr) plt.plot((idx_start,idx_start), (0,0.1), color='g', linewidth=2) plt.plot((idx_end,idx_end), (0,0.1), color='r', linewidth=2) - signal = signal[idx_start - n_zeros: idx_end + n_zeros] + signal = signal[max(0,idx_start - n_zeros): min(idx_end + n_zeros, signal.shape[0] -1)] return signal + +def lagcorr(x,y,lag=None,verbose=False): + '''Compute lead-lag correlations between 2 time series. + + <x>,<y>: 1-D time series. + <lag>: lag option, could take different forms of <lag>: + if 0 or None, compute ordinary correlation and p-value; + if positive integer, compute lagged correlation with lag + upto <lag>; + if negative integer, compute lead correlation with lead + upto <-lag>; + if pass in an list or tuple or array of integers, compute + lead/lag correlations at different leads/lags. + + Note: when talking about lead/lag, uses <y> as a reference. + Therefore positive lag means <x> lags <y> by <lag>, computation is + done by shifting <x> to the left hand side by <lag> with respect to + <y>. + Similarly negative lag means <x> leads <y> by <lag>, computation is + done by shifting <x> to the right hand side by <lag> with respect to + <y>. + + Return <result>: a (n*2) array, with 1st column the correlation + coefficients, 2nd column correpsonding p values. + + Currently only works for 1-D arrays. + ''' + + import numpy + from scipy.stats import pearsonr + + if len(x)!=len(y): + raise Exception('Input variables of different lengths.') + + #--------Unify types of <lag>------------- + if numpy.isscalar(lag): + if abs(lag)>=len(x): + raise Exception('Maximum lag equal or larger than array.') + if lag<0: + lag=-numpy.arange(abs(lag)+1) + elif lag==0: + lag=[0,] + else: + lag=numpy.arange(lag+1) + elif lag is None: + lag=[0,] + else: + lag=numpy.asarray(lag) + + #-------Loop over lags--------------------- + result=[] + if verbose: + print '\n#<lagcorr>: Computing lagged-correlations at lags:',lag + + for ii in lag: + if ii<0: + result.append(pearsonr(x[:ii],y[-ii:])) + elif ii==0: + result.append(pearsonr(x,y)) + elif ii>0: + result.append(pearsonr(x[ii:],y[:-ii])) + + result=numpy.asarray(result) + + return result |