<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import collections <EOL> from warnings import warn <EOL> import numpy as np <EOL> from scipy . linalg import inv , norm , orth <EOL> from scipy . signal import tf2zpk , zpk2ss <EOL> from . _utils import _get_zpk , carray <EOL> def simulateDSM ( u , arg2 , nlev = <NUM_LIT:2> , x0 = <NUM_LIT:0.> ) : <EOL> """<STR_LIT>""" <EOL> nlev = carray ( nlev ) <EOL> u = np . array ( u ) if not hasattr ( u , '<STR_LIT>' ) else u <EOL> if not max ( u . shape ) == np . prod ( u . shape ) : <EOL> warn ( "<STR_LIT>" ) <EOL> if u . ndim == <NUM_LIT:1> : <EOL> u = u . reshape ( ( <NUM_LIT:1> , - <NUM_LIT:1> ) ) <EOL> nu = u . shape [ <NUM_LIT:0> ] <EOL> nq = <NUM_LIT:1> if np . isscalar ( nlev ) else nlev . shape [ <NUM_LIT:0> ] <EOL> if ( hasattr ( arg2 , '<STR_LIT>' ) and not arg2 . inputs == <NUM_LIT:1> ) or ( hasattr ( arg2 , '<STR_LIT>' ) and not arg2 . outputs == <NUM_LIT:1> ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if isinstance ( arg2 , np . ndarray ) : <EOL> ABCD = np . asarray ( arg2 , dtype = np . float64 ) <EOL> if ABCD . shape [ <NUM_LIT:1> ] != ABCD . shape [ <NUM_LIT:0> ] + nu : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> form = <NUM_LIT:1> <EOL> else : <EOL> zeros , poles , k = _get_zpk ( arg2 ) <EOL> form = <NUM_LIT:2> <EOL> order = carray ( zeros ) . shape [ <NUM_LIT:0> ] if form == <NUM_LIT:2> else ABCD . shape [ <NUM_LIT:0> ] - nq <EOL> if not isinstance ( x0 , collections . Iterable ) : <EOL> x0 = x0 * np . ones ( ( order , ) , dtype = np . float64 ) <EOL> else : <EOL> x0 = np . array ( x0 ) . reshape ( ( - <NUM_LIT:1> , ) ) <EOL> if form == <NUM_LIT:1> : <EOL> A = ABCD [ : order , : order ] <EOL> B = ABCD [ : order , order : order + nu + nq ] <EOL> C = ABCD [ order : order + nq , : order ] <EOL> D1 = ABCD [ order : order + nq , order : order + nu ] <EOL> else : <EOL> A , B2 , C , D2 = zpk2ss ( poles , zeros , - <NUM_LIT:1> ) <EOL> C , D2 = np . real_if_close ( C ) , np . real_if_close ( D2 ) <EOL> Sinv = orth ( np . hstack ( ( np . transpose ( C ) , np . eye ( order ) ) ) ) / norm ( C ) <EOL> S = inv ( Sinv ) <EOL> C = np . dot ( C , Sinv ) <EOL> if C [ <NUM_LIT:0> , <NUM_LIT:0> ] < <NUM_LIT:0> : <EOL> S = - S <EOL> Sinv = - Sinv <EOL> A = np . dot ( np . dot ( S , A ) , Sinv ) <EOL> B2 = np . dot ( S , B2 ) <EOL> C = np . hstack ( ( np . ones ( ( <NUM_LIT:1> , <NUM_LIT:1> ) ) , np . zeros ( ( <NUM_LIT:1> , order - <NUM_LIT:1> ) ) ) ) <EOL> D2 = np . zeros ( ( <NUM_LIT:0> , ) ) <EOL> B1 = - B2 <EOL> D1 = <NUM_LIT:1> <EOL> B = np . hstack ( ( B1 , B2 ) ) <EOL> N = u . shape [ <NUM_LIT:1> ] <EOL> v = np . empty ( ( nq , N ) , dtype = np . float64 ) <EOL> y = np . empty ( ( nq , N ) , dtype = np . float64 ) <EOL> xn = np . empty ( ( order , N ) , dtype = np . float64 ) <EOL> xmax = np . abs ( x0 ) <EOL> for i in range ( N ) : <EOL> y0 = np . real ( np . dot ( C , x0 ) + np . dot ( D1 , u [ : , i ] ) ) <EOL> y [ : , i ] = y0 <EOL> v [ : , i ] = ds_quantize ( y0 , nlev ) <EOL> x0 = np . dot ( A , x0 ) + np . dot ( B , np . concatenate ( ( u [ : , i ] , v [ : , i ] ) ) ) <EOL> xn [ : , i ] = np . real_if_close ( x0 . T ) <EOL> xmax = np . max ( np . hstack ( ( np . abs ( x0 ) . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:1> ) ) , xmax . reshape ( ( - <NUM_LIT:1> , <NUM_LIT:1> ) ) ) ) , <EOL> axis = <NUM_LIT:1> , keepdims = True ) <EOL> return v . squeeze ( ) , xn . squeeze ( ) , xmax , y . squeeze ( ) <EOL> def ds_quantize ( y , n ) : <EOL> """<STR_LIT>""" <EOL> v = np . zeros ( y . shape ) <EOL> for qi in range ( n . shape [ <NUM_LIT:0> ] ) : <EOL> if n [ qi ] % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> v [ qi ] = <NUM_LIT:2> * np . floor ( <NUM_LIT:0.5> * y [ qi ] ) + <NUM_LIT:1> <EOL> else : <EOL> v [ qi ] = <NUM_LIT:2> * np . floor ( <NUM_LIT:0.5> * ( y [ qi ] + <NUM_LIT:1> ) ) <EOL> L = n [ qi ] - <NUM_LIT:1> <EOL> v [ qi ] = np . sign ( v [ qi ] ) * np . min ( ( np . abs ( v [ qi ] ) , L ) ) <EOL> return v </s>
