Commit 8fe481d7425fb86b830c3abc34259e55d617c992
Committed by
GitHub
1 parent
5802551652
Exists in
master
Update first_order_ident.py
Showing 1 changed file with 1 additions and 6 deletions Inline Diff
first_order_ident.py
# -*- coding: utf-8 -*- | 1 | 1 | # -*- coding: utf-8 -*- | |
""" | 2 | |||
Created on Fri Mar 11 11:04:35 2016 | 3 | |||
4 | ||||
@author: Baptiste Marechal | 5 | |||
""" | 6 | |||
7 | 2 | |||
from scipy import signal, linspace, pi, randn, ones | 8 | 3 | from scipy import signal, linspace, pi, randn, ones | |
from scipy.optimize import curve_fit | 9 | 4 | from scipy.optimize import curve_fit | |
import matplotlib.pyplot as plt | 10 | 5 | import matplotlib.pyplot as plt | |
import time | 11 | 6 | import time | |
12 | 7 | |||
tic = time.time() | 13 | 8 | tic = time.time() | |
14 | 9 | |||
'''function to optimize''' | 15 | 10 | '''function to optimize''' | |
def func(u, K, tau, y0): | 16 | 11 | def func(u, K, tau, y0): | |
sys = signal.lti(K, [tau, 1]) | 17 | 12 | sys = signal.lti(K, [tau, 1]) | |
y = sys.output(u, t, y0) | 18 | 13 | y = sys.output(u, t, y0) | |
return y[1] | 19 | 14 | return y[1] | |
20 | 15 | |||
'''input square signal''' | 21 | 16 | '''input square signal''' | |
global t | 22 | 17 | global t | |
t = linspace(0, 20, 1000) | 23 | 18 | t = linspace(0, 20, 1000) | |
Umes = -0.5*(signal.square(2*pi*0.1*t)+ones(len(t)))+ones(len(t))+randn(len(t))/50 | 24 | 19 | Umes = -0.5*(signal.square(2*pi*0.1*t)+ones(len(t)))+ones(len(t))+randn(len(t))/50 | |
25 | 20 | |||
'''noisy output signal''' | 26 | 21 | '''noisy output signal''' | |
p = [1, 1, 0] | 27 | 22 | p = [1, 1, 0] | |
Ymes = func(Umes, *p)+randn(len(Umes))/50 | 28 | 23 | Ymes = func(Umes, *p)+randn(len(Umes))/50 | |
29 | 24 | |||
'''input and output signals plot''' | 30 | 25 | '''input and output signals plot''' | |
plt.plot(t, Umes, label = 'Umes') | 31 | 26 | plt.plot(t, Umes, label = 'Umes') | |
plt.plot(t, Ymes, label = 'Ymes') | 32 | 27 | plt.plot(t, Ymes, label = 'Ymes') | |
33 | 28 | |||
34 | 29 | |||
'''optimization with non-linear least squares method''' | 35 | 30 | '''optimization with non-linear least squares method''' | |
popt, cov = curve_fit(func, Umes, Ymes) | 36 | 31 | popt, cov = curve_fit(func, Umes, Ymes) | |
print(p) | 37 | 32 | print(p) |