#NEWLINE# def realizeQNTF( ntf , form = 'FB' , rot = False , bn = 0.0 ) : #NEWLINE# #INDENT# "Convert a quadrature NTF into an ABCD matrix\n\n    The basic idea is to equate the value of the loop filter at a set of\n    points in the z-plane to the values of :math:`L_1 = 1-1/H` at those points.\n\n    The order of the zeros is used when mapping the NTF onto the chosen\n    topology.\n\n    Supported structures are:\n\n    * 'FB': Feedback\n    * 'PFB': Parallel feedback\n    * 'FF': Feedforward (bn is the coefficient of the aux DAC)\n    * 'PFF': Parallel feedforward\n\n    Not currently supported - feel free to send in a patch:\n\n    * 'FBD': FB with delaying quantizer\n    * 'FFD': FF with delaying quantizer\n\n    **Parameters:**\n\n    ntf : lti object or supported description\n        The NTF to be converted.\n    form : str, optional\n        The form to be used. See above for the currently supported\n        topologies. Defaults to ``'FB'``.\n    rot : bool, optional\n        Set ``rot`` to ``True`` to rotate the states to make as\n        many coefficients as possible real.\n    bn : float, optional\n        Coefficient of the auxiliary DAC, to be specified for a\n        'FF' form. Defaults to ``0.0``.\n\n    **Returns:**\n\n    ABCD : ndarray\n        ABCD realization of the requested type.\n\n    " #NEWLINE# ( ntf_z , ntf_p , _ ) = _get_zpk ( ntf ) #NEWLINE# order = ntf_p . shape [ 0 ] #NEWLINE# form = form . upper ( ) #NEWLINE# A = np . diag ( ntf_z ) #NEWLINE# subdiag = diagonal_indices ( A , ( - 1 ) ) #NEWLINE# A [ subdiag ] = 1.0 #NEWLINE# if ( ( form == 'PFB' ) or ( form == 'PFF' ) ) : #NEWLINE# #INDENT# for _ in range ( 2 ) : #NEWLINE# #INDENT# fz = ( np . angle ( ntf_z ) / ( 2 * np . pi ) ) #NEWLINE# f0 = fz [ 0 ] #NEWLINE# inband_zeros = ( np . abs ( ( fz - f0 ) ) < np . abs ( ( fz + f0 ) ) ) #NEWLINE# ( ntf_z , inband_zeros ) = _move_zeros_to_the_end ( ntf_z , inband_zeros ) #NEWLINE# #UNINDENT# n_in = np . sum ( inband_zeros ) #NEWLINE# imageband_zeros = np . logical_not ( inband_zeros ) #NEWLINE# n_im = np . sum ( imageband_zeros ) #NEWLINE# if np . any ( np . logical_not ( imageband_zeros [ n_in : ] ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Please put the image-band zeros at the end of' + ' the ntf zeros' ) ) #NEWLINE# #UNINDENT# if ( n_im > 0 ) : #NEWLINE# #INDENT# A [ ( n_in , ( n_in - 1 ) ) ] = 0.0 #NEWLINE# #UNINDENT# #UNINDENT# D = np . zeros ( shape = ( 1 , 2 ) , dtype = 'complex128' ) #NEWLINE# zSet = np . zeros ( ( ( 2 * order ) , ) , dtype = 'complex128' ) #NEWLINE# for i in range ( ( 2 * order ) ) : #NEWLINE# #INDENT# z = ( ( ( 2 * rand ( 1 ) ) - 1 ) + ( 1j * ( ( 2 * rand ( 1 ) ) - 1 ) ) ) #NEWLINE# while ( np . any ( ( np . abs ( ( z - ntf_z ) ) < 0.1 ) ) or ( np . any ( np . abs ( ( z - zSet ) ) ) < 0.1 ) ) : #NEWLINE# #INDENT# z = ( ( ( 2 * rand ( 1 ) ) - 1 ) + ( 1j * ( ( 2 * rand ( 1 ) ) - 1 ) ) ) #NEWLINE# #UNINDENT# zSet [ i ] = z [ 0 ] #NEWLINE# #UNINDENT# L1 = ( 1.0 - ( 1.0 / evalTF ( ntf , zSet ) ) ) #NEWLINE# if ( form == 'FB' ) : #NEWLINE# #INDENT# B = np . zeros ( shape = ( order , 2 ) , dtype = 'complex128' ) #NEWLINE# C = np . hstack ( ( np . zeros ( ( 1 , ( order - 1 ) ) , dtype = 'complex128' ) , np . atleast_2d ( 1 ) ) ) #NEWLINE# F = np . zeros ( ( zSet . shape [ 0 ] , order ) , dtype = 'complex128' ) #NEWLINE# I = np . eye ( order ) #NEWLINE# for i in range ( zSet . shape [ 0 ] ) : #NEWLINE# #INDENT# F [ i , : ] = np . dot ( C , np . linalg . inv ( ( ( zSet [ i ] * I ) - A ) ) ) #NEWLINE# #UNINDENT# B [ : , 1 ] = np . linalg . lstsq ( F , L1 ) [ 0 ] #NEWLINE# if ( rot == True ) : #NEWLINE# #INDENT# ABCD = np . vstack ( ( np . hstack ( ( A , B [ : , 1 ] . reshape ( ( ( - 1 ) , 1 ) ) ) ) , np . hstack ( ( C , np . atleast_2d ( 0 ) ) ) ) ) #NEWLINE# for i in range ( order ) : #NEWLINE# #INDENT# phi = np . angle ( ABCD [ ( i , ( - 1 ) ) ] ) #NEWLINE# ABCD [ i , : ] = ( ABCD [ i , : ] * np . exp ( ( ( - 1j ) * phi ) ) ) #NEWLINE# ABCD [ : , i ] = ( ABCD [ : , i ] * np . exp ( ( ( + 1j ) * phi ) ) ) #NEWLINE# #UNINDENT# ( A , B2 , C , _ ) = partitionABCD ( ABCD ) #NEWLINE# B [ : , 1 ] = np . squeeze ( B2 ) #NEWLINE# #UNINDENT# B [ ( 0 , 0 ) ] = np . abs ( B [ ( 0 , 1 ) ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( form == 'PFB' ) : #NEWLINE# #INDENT# B = np . zeros ( ( order , 2 ) , dtype = 'complex128' ) #NEWLINE# C = np . hstack ( ( np . zeros ( ( 1 , ( n_in - 1 ) ) ) , np . ones ( ( 1 , 1 ) ) , np . zeros ( ( 1 , ( n_im - 1 ) ) ) , np . ones ( ( 1 , 1 ) ) ) ) #NEWLINE# F = np . zeros ( ( zSet . shape [ 0 ] , order ) , dtype = 'complex128' ) #NEWLINE# I = np . eye ( order ) #NEWLINE# for i in range ( zSet . shape [ 0 ] ) : #NEWLINE# #INDENT# F [ i , : ] = np . dot ( C , np . linalg . inv ( ( ( zSet [ i ] * I ) - A ) ) ) #NEWLINE# #UNINDENT# B [ : , 1 ] = np . linalg . lstsq ( F , L1 ) [ 0 ] #NEWLINE# if ( rot == True ) : #NEWLINE# #INDENT# ABCD = np . vstack ( ( np . hstack ( ( A , B [ : , 1 ] . reshape ( ( ( - 1 ) , 1 ) ) ) ) , np . hstack ( ( C , np . atleast_2d ( 0 ) ) ) ) ) #NEWLINE# for i in range ( order ) : #NEWLINE# #INDENT# phi = np . angle ( ABCD [ ( i , ( - 1 ) ) ] ) #NEWLINE# ABCD [ i , : ] = ( ABCD [ i , : ] * np . exp ( ( ( - 1j ) * phi ) ) ) #NEWLINE# ABCD [ : , i ] = ( ABCD [ : , i ] * np . exp ( ( ( + 1j ) * phi ) ) ) #NEWLINE# #UNINDENT# ( A , B2 , C , _ ) = partitionABCD ( ABCD ) #NEWLINE# B [ : , 1 ] = np . squeeze ( B2 ) #NEWLINE# #UNINDENT# B [ ( 0 , 0 ) ] = np . abs ( B [ ( 0 , 1 ) ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( form == 'FF' ) : #NEWLINE# #INDENT# B0 = np . vstack ( ( np . atleast_2d ( 1 ) , np . zeros ( ( ( order - 1 ) , 1 ) , dtype = 'complex128' ) ) ) #NEWLINE# B = B0 . copy ( ) #NEWLINE# B [ ( ( - 1 ) , 0 ) ] = bn #NEWLINE# F = np . zeros ( ( order , zSet . shape [ 0 ] ) , dtype = 'complex128' ) #NEWLINE# I = np . eye ( order ) #NEWLINE# for i in range ( zSet . shape [ 0 ] ) : #NEWLINE# #INDENT# F [ : , i ] = np . squeeze ( np . dot ( np . linalg . inv ( ( ( zSet [ i ] * I ) - A ) ) , B ) ) #NEWLINE# #UNINDENT# L1 = L1 . reshape ( ( ( - 1 ) , 1 ) ) #NEWLINE# C = np . linalg . lstsq ( F . T , L1 ) [ 0 ] . T #NEWLINE# if ( rot == True ) : #NEWLINE# #INDENT# ABCD = np . vstack ( ( np . hstack ( ( A , B ) ) , np . hstack ( ( C , np . atleast_2d ( 0 ) ) ) ) ) #NEWLINE# for i in range ( 1 , ( order - 1 ) ) : #NEWLINE# #INDENT# phi = np . angle ( ABCD [ ( ( - 1 ) , i ) ] ) #NEWLINE# ABCD [ i , : ] = ( ABCD [ i , : ] * np . exp ( ( ( + 1j ) * phi ) ) ) #NEWLINE# ABCD [ : , i ] = ( ABCD [ : , i ] * np . exp ( ( ( - 1j ) * phi ) ) ) #NEWLINE# #UNINDENT# ( A , B , C , _ ) = partitionABCD ( ABCD ) #NEWLINE# #UNINDENT# B = np . hstack ( ( ( - B0 ) , B ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( form == 'PFF' ) : #NEWLINE# #INDENT# B0 = np . vstack ( ( np . atleast_2d ( 1 ) , np . zeros ( ( ( order - 1 ) , 1 ) , dtype = 'complex128' ) ) ) #NEWLINE# B = B0 . copy ( ) #NEWLINE# B [ n_in ] = 1.0 #NEWLINE# F = np . zeros ( ( order , zSet . shape [ 0 ] ) , dtype = 'complex128' ) #NEWLINE# I = np . eye ( order ) #NEWLINE# for i in range ( zSet . shape [ 0 ] ) : #NEWLINE# #INDENT# F [ : , i ] = np . squeeze ( np . dot ( np . linalg . inv ( ( ( zSet [ i ] * I ) - A ) ) , B ) ) #NEWLINE# #UNINDENT# L1 = L1 . reshape ( ( ( - 1 ) , 1 ) ) #NEWLINE# C = np . linalg . lstsq ( F . T , L1 ) [ 0 ] . T #NEWLINE# C = C [ : , : : ( - 1 ) ] #NEWLINE# if ( rot == True ) : #NEWLINE# #INDENT# ABCD = np . vstack ( ( np . hstack ( ( A , B ) ) , np . hstack ( ( C , np . zeros ( ( 1 , 1 ) ) ) ) ) ) #NEWLINE# for i in range ( 1 , ( order - 1 ) ) : #NEWLINE# #INDENT# phi = np . angle ( ABCD [ ( ( - 1 ) , i ) ] ) #NEWLINE# ABCD [ i , : ] = ( ABCD [ i , : ] * np . exp ( ( ( + 1j ) * phi ) ) ) #NEWLINE# ABCD [ : , i ] = ( ABCD [ : , i ] * np . exp ( ( ( - 1j ) * phi ) ) ) #NEWLINE# #UNINDENT# ( A , B , C , _ ) = partitionABCD ( ABCD ) #NEWLINE# #UNINDENT# B = np . hstack ( ( B0 , B ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( ( 'Sorry, form %s is not supported.' % form ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# ABCD = np . vstack ( ( np . hstack ( ( A , B ) ) , np . hstack ( ( C , D ) ) ) ) #NEWLINE# return ABCD