import numpy as np from numpy import * import matplotlib matplotlib.use('TkAgg') from matplotlib import pyplot as plt from matplotlib import animation # import scipy from scipy import * from scipy.optimize import leastsq h2r = 2.*3.1415926 w = np.multiply([440, 100, 800],h2r) a = [1.0, 0.4, 0.6] t = linspace(0,0.05,num=2000) x = zeros([len(w),len(t)]) for i in range(len(w)): x[i]=a[i]*sin(w[i]*t) sig = sum(x,0) ts = sort(np.random.rand(1600,1)*max(t),axis=0) xs = np.interp(ts,t,sig) ff = fft(x[1,:]) fig = plt.figure() ax = plt.axes() t0 = t[-1]/len(t) ax.plot(arange(len(t))/t0,real(ff)) plt.show() # A*x - b = 0 # guess = [1.0,440*3.1415926/180.0,0.4,100*3.1415926/180.0,0.6,2000*3.1415926/180.0] # guess = [0.9,430*h2r,0.4,100*h2r,0.6,800*h2r] # # yy = xx[0]*np.sin(ts*xx[1])+xx[2]*np.sin(ts*xx[3])+xx[4]*np.sin(ts*xx[5]) # # print yy # # def func(xx): # # return (xx[0]*np.sin(ts*xx[1])+xx[2]*np.sin(ts*xx[3])+xx[4]*np.sin(ts*xx[5])[:,0] # optimize_func = lambda xx: (xx[0]*np.sin(ts*xx[1])+xx[2]*np.sin(ts*xx[3])+xx[4]*np.sin(ts*xx[5])-xs)[:,0] # est = leastsq(optimize_func, guess)[0] # xfit = est[0]*np.sin(t*est[1])+est[2]*np.sin(t*est[3])+est[4]*np.sin(t*est[5]) # # Calculate minimum L2 norm of x from SVD # print [a[0],w[0],a[1],w[1],a[2],w[2]] # print est # Calculate minimum L1 norm of x # approx L1 norm, minimize exact penalty, increase alpha # abs(x) ~= 1/alpha*(log(1+e^(-alpha*x) + log(1+e^(alpha*x)) # A = np.matrix(np.append(ones([N,1]), x.reshape(N,1), 1)) # U, s, V = np.linalg.svd(A) # w = np.matrix(np.append(np.diag(one_over(s)), zeros([M, N-M]),1)) # y = np.matrix(y).T # xx = V.T*w*U.T*y # b = xx[0,0] # a = xx[1,0] # fig = plt.figure() # ax = plt.axes(xlim=[min(t),max(t)]) # # ax.plot(t,x[0,:],t,x[1,:],t,x[2,:],t,sum(x,0)) # ax.plot(t,sig) # ax.plot(ts,xs,'ko') # ax.plot(t,xfit,'r.') # plt.show()