In [1]:
%matplotlib inline
%load_ext autoreload
%autoreload 2
import matplotlib
matplotlib.rcParams['text.latex.unicode'] = True
In [2]:
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 7 14:10:25 2018
@author: kirby
"""
import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
import mousai as ms
from scipy import pi, sin
from scipy.fftpack import *
plt.close('all')
def duff_osc_ss(x, params):
omega = params['omega']
t = params['cur_time']
xdot = np.array([[x[1]],[-x[0]-.1*x[0]**3-.1*x[1]+1*sin(omega*t)]])
return xdot
def duff_osc2(x, v, params):
omega = params['omega']
t = params['cur_time']
return np.array([[-x-0.1*x**3-.1*v+1*sin(omega*t)]])
print('second order')
tfso, xfso, e, amps, phases = ms.hb_freq(duff_osc2, sp.array([[.1,-.1,0]]), omega = 0.1, \
eqform='second_order', num_harmonics=1, num_time_steps = 51, f_tol = 1e-10)
plt.figure()
#plt.plot(time, xc.T, t, x.T, '*')
time, xc = ms.time_history(tfso,xfso)
plt.plot(time, xc.T, label = 'second order 1 harmonic')
print('on to state space')
t, x, e, amps, phases = ms.hb_freq(duff_osc_ss, sp.array([[0,1,-1],[.1,-.1,0]]), omega = 0.1, \
eqform='first_order', num_harmonics=1, num_time_steps = 51, f_tol = 1e-7)
#print(x,e)
print(e)
print('Constant term of FFT of signal should be zero: ', ms.fftp.fft(x)[0,0])
time, xc = ms.time_history(t,x)
#plt.plot(time, xc.T, t, x.T, '*')
plt.plot(time, xc.T, label = 'state space 1 harmonic')
print('on to state space')
t, x, e, amps, phases = ms.hb_freq(duff_osc_ss, sp.array([[0,1,-1],[.1,-.1,0]]), omega = 0.1, \
eqform='first_order', num_harmonics=3, num_time_steps = 51, f_tol = 1e-7)
print(e)
print('Constant term of FFT of signal should be zero: ', ms.fftp.fft(x)[0,0])
time, xc = ms.time_history(t,x)
plt.plot(time, xc.T, label = '3 harmonics')
t, x, e, amps, phases = ms.hb_time(duff_osc_ss, sp.array([[0,1,-1],[.1,-.1,0]]), omega = 0.1, eqform='first_order',
num_harmonics=5)
print(x,e)
print('Constant term of FFT of signal should be zero: ', ms.fftp.fft(x)[0,0])
time, xc = ms.time_history(t,x)
#plt.figure()
#plt.plot(time, xc.T, t, x.T, '*')
plt.plot(time, xc.T,'-.', label = '5 harmonics')
#plt.plot(time, xc.T, '3 harmonics')
time, xc = ms.time_history(tfso,xfso)
plt.plot(time, xc.T, '--', label = 'second order 1 harmonic')
#plt.grid(True)
#plt.legend(['hb_freq Displacement','hb_freq Velocity','hb_time Displacement','hb_time Velocity'])
plt.legend()
plt.grid()
In [3]:
t, x, e, amps, phases = ms.hb_freq(duff_osc_ss, sp.array([[0,1,-1],[.1,-.1,0]]), .1, \
eqform='first_order', num_harmonics=1, num_time_steps = 51, f_tol = 1e-7)
time, xc = ms.time_history(t,x)
plt.plot(time, xc.T)
plt.grid(True)
In [4]:
e
Out[4]:
In [5]:
tfso, xfso, e, amps, phases = ms.hb_freq(duff_osc2, sp.array([[.1,-.1,0]]), .1, \
eqform='second_order', num_harmonics=1, num_time_steps = 51)
print('Equation errors (should be zero): ', e)
print('Constant term of FFT of signal should be zero: ', ms.fftp.fft(x)[0,0])
In [6]:
tfso, xfso, e, amps, phases = ms.hb_freq(duff_osc2, num_variables=1, omega = 0.2, \
eqform='second_order', num_harmonics=5, mask_constant=False, num_time_steps = 501, f_tol = 1e-10)
print('Equation errors (should be zero): ', e)
print('Constant term of FFT of signal should be zero: ', ms.fftp.fft(x)[0,0])
In [7]:
e
Out[7]:
In [8]:
xfso
Out[8]:
In [9]:
time, xc = ms.time_history(tfso,xfso)
plt.plot(time,xc.T)
plt.grid(True)
plt.title('Second order with 3 harmonics')
Out[9]:
In [10]:
t, x, e, amps, phases = ms.hb_freq(duff_osc_ss, sp.array([[0,1,-1],[.1,-.1,0]]), .1, \
eqform='first_order', num_harmonics=1, num_time_steps = 51)#, f_tol = 1e-12)
print('x = ', x)
In [11]:
e
Out[11]:
In [12]:
time, xc = ms.time_history(t,x)
plt.plot(time,xc.T)
plt.grid(True)
plt.title('State space with 3 harmonics')
Out[12]:
In [13]:
t, x, e, amps, phases = ms.hb_freq(duff_osc_ss, num_variables = 2, omega = 0.1,
eqform='first_order', num_harmonics=5, num_time_steps=51, mask_constant=True, f_rtol = 1e-8)#, f_rtol=1e-8)
time, xc = ms.time_history(t, x)
plt.plot(time, xc.T, label='5 harmonics')
Out[13]:
In [14]:
t, x, e, amps, phases = ms.hb_freq(duff_osc_ss, x0 = x, omega = 0.1,
eqform='first_order', num_harmonics=5, num_time_steps=501, mask_constant=True)#, f_rtol = 1e-8)#, f_rtol=1e-8)
time, xc = ms.time_history(t, x)
plt.plot(time, xc.T, label='5 harmonics')
Out[14]:
In [15]:
e[0,:]
Out[15]:
In [16]:
print('ss 1')
t, x, e, amps, phases = ms.hb_freq(duff_osc_ss, sp.array([[0, 1, -1], [.1, -.1, 0]]), .1,
eqform='first_order', num_harmonics=1, num_time_steps=501)
time, xc = ms.time_history(t, x)
plt.plot(time, xc.T, label='1 harmonics')
print('ss 3')
t, x, e, amps, phases = ms.hb_freq(duff_osc_ss, sp.array([[0, 1, -1], [.1, -.1, 0]]), .1,
eqform='first_order', num_harmonics=3, num_time_steps=501) # , f_tol = 1e-8)
time, xc = ms.time_history(t, x)
print(e)
plt.plot(time, xc.T, label='3 harmonics')
print('ss 5')
t, x, e, amps, phases = ms.hb_freq(duff_osc_ss, x, .1,
eqform='first_order', num_harmonics=5, num_time_steps=501, mask_constant=True, f_tol=1e-10)
print(e)
time, xc = ms.time_history(t, x)
plt.plot(time, xc.T, label='5 harmonics')
plt.legend()
Out[16]:
In [17]:
plt.plot(np.abs(fft(x).T))
Out[17]:
In [18]:
e
Out[18]:
In [19]:
X = fft(x)
X[:,2:9]= 0*X[:,2:9]
x = ifft(X)
In [20]:
x
Out[20]:
In [21]:
time, xc = ms.time_history(t, x)
plt.plot(time, xc.T, label='5 harmonics')
plt.legend()
Out[21]:
In [22]:
e
Out[22]:
In [23]:
np.max(e)
Out[23]:
In [24]:
import scipy.integrate as odeint
In [25]:
help(odeint.solve_ivp)
In [26]:
def duff_osc_ss(t, x):
omega = 0.1
xdot = np.array([x[1],-x[0]-.1*x[0]**3-.1*x[1]+1*sin(omega*t)])
return xdot
In [27]:
sol = odeint.solve_ivp(fun = duff_osc_ss, t_span=(0,600), y0 = np.array([0,0]))
In [28]:
plt.plot(sol.t,sol.y.T)
plt.axis([500,600,-1,1])
plt.grid(True)
In [29]:
time
y = sin(time)
plt.plot(time, sin(time))
Out[29]:
In [30]:
import scipy.fftpack as fftp
In [31]:
ffty = fftp.fft(y)
In [32]:
plt.plot(np.abs(ffty))
Out[32]:
In [33]:
plt.plot(ms.fft_to_rfft(ffty))
Out[33]:
In [34]:
plt.plot(rfft(y))
Out[34]:
In [35]:
plt.plot(rfft(y+1)[:2],'*')
Out[35]:
In [36]:
y.size
Out[36]:
In [37]:
plt.plot(rfft(np.cos(time))[:25],'*',rfft(y)[:25],'x')
plt.grid()
In [38]:
rfft = ms.fft_to_rfft(ffty)
rfft
Out[38]:
In [39]:
plt.plot(rfft/np.max(np.abs(rfft)))
Out[39]:
In [40]:
from scipy.fftpack import *
In [41]:
a = np.array([[9, -9, 1, -1],[9, -9, 1, -1]])
fft(a)
Out[41]:
In [42]:
rfft(a)
Out[42]:
In [44]:
fft(a)
Out[44]:
In [45]:
rfft(a)
Out[45]:
In [46]:
time = np.array([0,1,2,3])
In [ ]:
plt.plot(time_e, x.T*3.2/5, time, a.T, '*')
In [ ]:
x[0,0]
In [ ]:
a[0,0]
In [ ]:
rfft(a)
In [ ]:
rfft(x)
In [ ]:
plt.plot(irfft(x).T)
In [ ]:
x.shape
In [ ]:
time
In [ ]:
np.sin(time)
In [ ]:
tt = np.linspace(0,np.pi*2,101)
In [ ]:
np.zeros((2,2)).shape
In [ ]:
plt.plot(tt,sin(tt))
In [ ]:
rft = rfft(np.cos(tt))
rft
In [ ]:
rft[3:]= rft[3:]*0
rft
In [ ]:
plt.plot(irfft(rft))
In [ ]:
a
In [ ]:
time
In [ ]:
plt.plot(time, a.T, '*')
In [ ]:
b = irfft(np.hstack((rfft(a), a*0)))
In [ ]:
plt.plot(b.T)
In [ ]: