def test_acorr(): arr = np.random.rand(100) for norm in [True, False]: ref = acorr(arr, method=1, norm=norm) for m in range(2, 8): print("%i : 1" % m) np.testing.assert_array_almost_equal( acorr(arr, method=m, norm=norm), ref)
def test_acorr(): arr = np.random.rand(100) for norm in [True,False]: ref = acorr(arr, method=1, norm=norm) for m in range(2,8): print "%i : 1" %m np.testing.assert_array_almost_equal(acorr(arr, method=m, norm=norm), ref)
def test_pdos_1d(): pad=lambda x: pad_zeros(x, nadd=len(x)-1) n=500; w=welch(n) # 1 second signal t=np.linspace(0,1,n); dt=t[1]-t[0] # sum of sin()s with random freq and phase shift, 10 frequencies from # f=0...100 Hz v=np.array([np.sin(2*np.pi*f*t + rand()*2*np.pi) for f in rand(10)*100]).sum(0) f=np.fft.fftfreq(2*n-1, dt)[:n] c1=mirror(ifft(abs(fft(pad(v)))**2.0)[:n].real) c2=correlate(v,v,'full') c3=mirror(acorr(v,norm=False)) assert np.allclose(c1, c2) assert np.allclose(c1, c3) p1=(abs(fft(pad(v)))**2.0)[:n] p2=(abs(fft(mirror(acorr(v,norm=False)))))[:n] assert np.allclose(p1, p2) p1=(abs(fft(pad(v*w)))**2.0)[:n] p2=(abs(fft(mirror(acorr(v*w,norm=False)))))[:n] assert np.allclose(p1, p2)
# Our methods 1 and 2 must result in exactly the same after # norm_int()'ing them (within numerical noise). # In both, we use a Welch window as in fourier.x . # 1) Zero-pad arr at the end and fft directly. For padding, use nadd=N-1 to get # exactly the same signal length as y2 b/c in case of y2: mirror(rand(N)).shape # = (2N-1,), i.e. the point at t=0 apprears only once. Padding increases the # frequency resolution (almost factor 2) to exactly the same df as we get for # method 2 b/c of the mirroring of the signal there. fft_arr = signal.pad_zeros(arr * signal.welch(arr.shape[0]), nadd=arr.shape[0] - 1) y1 = np.abs(fft(fft_arr))**2 # 2) fft the autocorrelation of `arr` fft_arr = pydos.mirror(signal.acorr(arr * signal.welch(arr.shape[0]), method=5)) y2 = np.abs(fft(fft_arr)) # 3) fourier.x # # For the 1d case, we write the time trace in a format suggested in the CPMD # manual and the fourier.x README file: awk '{ print $1, 0.0, 0.0, 0.0, 0.0, # 0.0, $2; }' ENERGIES > ekinc.dat where ENERGIES is a CPMD output file. From # that, only column 1 (time step) and some energy value from column 2 is used. if use_fourier: fourier_in_data = np.zeros((arr.shape[0], 7)) fourier_in_data[:, 0] = np.arange(arr.shape[0]) fourier_in_data[:, 4] = arr fourier_in_data_fn = pj(fourier_dir, 'fourier_in_data_1d.txt') fourier_out_data_fn = pj(fourier_dir, 'fourier_out_data_1d.txt') fourier_in_fn = pj(fourier_dir, 'fourier_1d.in')
arr = np.diff(coords) # "velocity" # Our methods 1 and 2 must result in exactly the same after # norm_int()'ing them (within numerical noise). # In both, we use a Welch window as in fourier.x . # 1) Zero-pad arr at the end and fft directly. For padding, use nadd=N-1 to get # exactly the same signal length as y2 b/c in case of y2: mirror(rand(N)).shape # = (2N-1,), i.e. the point at t=0 apprears only once. Padding increases the # frequency resolution (almost factor 2) to exactly the same df as we get for # method 2 b/c of the mirroring of the signal there. fft_arr = signal.pad_zeros(arr*signal.welch(arr.shape[0]), nadd=arr.shape[0]-1) y1 = np.abs(fft(fft_arr))**2 # 2) fft the autocorrelation of `arr` fft_arr = pydos.mirror(signal.acorr(arr*signal.welch(arr.shape[0]), method=5)) y2 = np.abs(fft(fft_arr)) # 3) fourier.x # # For the 1d case, we write the time trace in a format suggested in the CPMD # manual and the fourier.x README file: awk '{ print $1, 0.0, 0.0, 0.0, 0.0, # 0.0, $2; }' ENERGIES > ekinc.dat where ENERGIES is a CPMD output file. From # that, only column 1 (time step) and some energy value from column 2 is used. if use_fourier: fourier_in_data = np.zeros((arr.shape[0],7)) fourier_in_data[:,0] = np.arange(arr.shape[0]) fourier_in_data[:,4] = arr fourier_in_data_fn = pj(fourier_dir, 'fourier_in_data_1d.txt') fourier_out_data_fn = pj(fourier_dir, 'fourier_out_data_1d.txt') fourier_in_fn = pj(fourier_dir, 'fourier_1d.in')