N = len(x) y = arange(0, N, 0.1) fig = plt.figure(0, (16 * 1.5, 16 * 1.5)) fig.text(.5, .95, 'Finding dominant frequency via autocorrelation function analysis', ha='center') for ii, (f_expr, sp1, sp2, sp3) in enumerate([ ('7*sin(5*x)+4*sin(9*x)', 321, 323, 325), ('7*sin(5*x)+6*sin(9*x)', 322, 324, 326), ]): f = eval(f_expr) start = time.time() acf_data = acf(f, y) end = time.time() if not ii: print 'ACF computation time per point: %.3fus' % (1e6 * (end - start) / len(y)) start = time.time() omega = acf_sinefit(f, start_j=1) end = time.time() if not ii: print 'Sine fit computation time: %.3fus' % (1e6 * (end - start)) omega2 = acf_sinefit(f, start_j=int(2 * pi / omega) + 2) a = acf(f, 0.0) sinefit = a * cos(omega * y) * (N - y) / N sinefit2 = a * cos(omega2 * y) * (N - y) / N omega_lst = arange(0, 0.8, 0.001)
from numpy import * from iocbio.ops.autocorrelation import acf, acf_argmax, acf_sinefit # Define a signal: dx = 0.05 x = arange(0,2*pi,dx) N = len(x) f = 7*sin(5*x)+6*sin(9*x) # f is shown in the upper right plot below # Calculate autocorrelation function: y = arange(0,N,0.1) af = acf (f, y, method='linear') # af is shown in the lower right plot below # Find the first maximum of the autocorrelation function: y_max1 = acf_argmax(f, method='linear') # The first maximum gives period estimate for f print 'period=',dx*y_max1 print 'frequency=',2*pi/(y_max1*dx) # Find the second maximum of the autocorrelation function: y_max2 = acf_argmax(f, start_j=y_max1+1, method='linear') print y_max1, y_max2 # Find sine-fit of the autocorrelation function: omega = acf_sinefit(f, method='linear') # The parameter omega in A*cos (omega*y)*(N-y)/N gives # another period estimate for f: print 'period=',2*pi/(omega/dx) print 'frequency=', omega/dx
import time dx = 0.05 x = arange(0,2*pi,dx) N = len(x) y = arange(0,N,0.1) fig = plt.figure (0, (16*1.5,16*1.5)) fig.text(.5,.95, 'Finding dominant frequency via autocorrelation function analysis', ha='center') for ii, (f_expr, sp1, sp2, sp3) in enumerate([('7*sin(5*x)+4*sin(9*x)', 321,323,325), ('7*sin(5*x)+6*sin(9*x)', 322,324,326), ]): f = eval(f_expr) start = time.time() acf_data = acf(f, y) end = time.time() if not ii: print 'ACF computation time per point: %.3fus' % (1e6*(end-start)/len (y)) start = time.time() omega = acf_sinefit(f, start_j=1) end = time.time() if not ii: print 'Sine fit computation time: %.3fus' % (1e6*(end-start)) omega2 = acf_sinefit(f, start_j=int(2*pi/omega)+2) a = acf(f, 0.0) sinefit = a*cos(omega*y)*(N-y)/N sinefit2 = a*cos(omega2*y)*(N-y)/N omega_lst = arange(0, 0.8, 0.001) omega_arr = omega_lst/dx
from numpy import * from iocbio.ops.autocorrelation import acf, acf_argmax, acf_sinefit # Define a signal: dx = 0.05 x = arange(0, 2 * pi, dx) N = len(x) f = 7 * sin(5 * x) + 6 * sin(9 * x) # f is shown in the upper right plot below # Calculate autocorrelation function: y = arange(0, N, 0.1) af = acf(f, y, method='linear') # af is shown in the lower right plot below # Find the first maximum of the autocorrelation function: y_max1 = acf_argmax(f, method='linear') # The first maximum gives period estimate for f print 'period=', dx * y_max1 print 'frequency=', 2 * pi / (y_max1 * dx) # Find the second maximum of the autocorrelation function: y_max2 = acf_argmax(f, start_j=y_max1 + 1, method='linear') print y_max1, y_max2 # Find sine-fit of the autocorrelation function: omega = acf_sinefit(f, method='linear') # The parameter omega in A*cos (omega*y)*(N-y)/N gives # another period estimate for f: print 'period=', 2 * pi / (omega / dx) print 'frequency=', omega / dx