Blame view

first_order_ident.py 993 Bytes
f27e91828   mer0m   Add files via upload
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
44
45
46
47
48
49
  # -*- coding: utf-8 -*-
  """
  Created on Fri Mar 11 11:04:35 2016
  
  @author: Baptiste Marechal
  """
  
  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):
      sys = signal.lti(K, [tau, 1])
      y = sys.output(u, t, y0)
      return y[1]
  
  '''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
  print(toc)