Blame view

first_order_ident.py 913 Bytes
f27e91828   mer0m   Add files via upload
1
  # -*- coding: utf-8 -*-
f27e91828   mer0m   Add files via upload
2
3
4
5
6
7
8
9
10
11
  
  from scipy import signal, linspace, pi, randn, ones
  from scipy.optimize import curve_fit
  import matplotlib.pyplot as plt
  import time
  
  tic = time.time()
  
  '''function to optimize'''
  def func(u, K, tau, y0):
c21bab35d   bmarechal   replace 4 spaces ...
12
13
14
  	sys = signal.lti(K, [tau, 1])
  	y = sys.output(u, t, y0)
  	return y[1]
f27e91828   mer0m   Add files via upload
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  
  '''input square signal'''
  global t
  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
  
  '''noisy output signal'''
  p = [1, 1, 0]
  Ymes = func(Umes, *p)+randn(len(Umes))/50
  
  '''input and output signals plot'''
  plt.plot(t, Umes, label = 'Umes')
  plt.plot(t, Ymes, label = 'Ymes')
  
  
  '''optimization with non-linear least squares method'''
  popt, cov = curve_fit(func, Umes, Ymes)
  print(p)
  print(popt)
  
  '''estimated response plot'''
  Yfit = func(Umes, *popt)
  plt.plot(t, Yfit, label ='Yfit')
  
  plt.legend()
  plt.grid()
  plt.show()
  
  toc = time.time() - tic
8fe481d74   mer0m   Update first_orde...
44
  print(toc)