u = 1 for n in range(2): phi = np.pi / 2 + (2 * n + 1) * np.pi / (2 * N) v = [ 1, (-2 * r1) * np.cos(phi), (r1**2) * ((np.cos(phi))**2) + (r2**2) * ((np.sin(phi))**2) ] p = np.convolve(v, u) u = p #The following was to verify that the roots obtained #are correct #roots(p) p1 = epsilon**2 * np.convolve(cheb(N), cheb(N)) + np.hstack((np.zeros( (1, 2 * N)).ravel(), 1)) #r = roots(p1) #Evaluating the gain of the stable lowpass filter #The gain has to be 1/sqrt(1+epsilon^2) at Omega = 1 G = abs(np.polyval(p, 1j)) / np.sqrt(1 + epsilon**2) #Plotting the magnitude response of the stable filter #and comparing with the desired response for the purpose #of verification Omega = np.linspace(0, 2, 201) H_stable = abs(G / np.polyval(p, 1j * Omega))
r1 = (beta**2 - 1) / (2 * beta) r2 = (beta**2 + 1) / (2 * beta) #Obtaining the polynomial approximation for the low pass #Chebyschev filter to obtain a stable filter u = np.array([1]) for n in range(0, int(N / 2)): #n = 0:(N/2)-1; phi = np.pi / 2 + (2 * n + 1) * np.pi / (2 * N) v = [ 1, -2 * r1 * np.cos(phi), r1**2 * np.cos(phi)**2 + r2**2 * np.sin(phi)**2 ] p = np.convolve(v, u) u = p p1 = epsilon**2 * np.convolve(cheb(N), cheb(N)) + np.concatenate( [np.zeros(2 * N), np.array([1])]) G = abs(np.polyval(p, 1j)) / np.sqrt(1 + epsilon**2) Omega = np.arange(0, 2.01, 0.01) #0:0.01:2 H_stable = abs(G / np.polyval(p, 1j * Omega)) H_cheb = abs(np.sqrt(1 / np.polyval(p1, 1j * Omega))) plt.figure() plt.plot(Omega, H_stable, 'o', label='Design') plt.plot(Omega, H_cheb, label='Specification') plt.xlabel('$\Omega$') plt.ylabel('$|H_{a,LP}(j\Omega)|$') plt.legend() #plt.savefig('../figs/iir/AnalogLP_cheb.eps') #plt.savefig('../figs/iir/AnalogLP_cheb.pdf') subprocess.run(shlex.split("termux-open ../figs/iir/AnalogLP_cheb.pdf")) #else
Define inputs """ Lambda = 1 Q0 = 1 H = 1 kappa = 1e-4 mu = 1e-2 LambdaTilde = 1e-2 # Lambda*Q0*H**2/(kappa*mu) Ny = 33 n = Ny+1 k = 2.0*pi*arange(1, 5, 1) Idn = eye(n) D, zc = cheb(Ny) k_sigma = np.zeros((len(k), 2)) ##################### """ convert cheb grid to physical grid zc = a*z + b zc = [-1, 1], z= [0, 1] zc = 2*z - 1 d()/dz = [d()/dzc]*(dzc/dz) ...chain rule d()/dz = a*D """ a = 2 b = -1
#Obtaining the polynomial approximation for the low pass #Chebyschev filter to obtain a stable filter u = 1 for n in range(2): phi = np.pi/2 + (2*n+1)*np.pi/(2*N) v = [1,(-2*r1)*np.cos(phi),(r1**2)*((np.cos(phi))**2) + (r2**2)*((np.sin(phi))**2)] p = np.convolve(v,u) u = p #The following was to verify that the roots obtained #are correct #roots(p) p1 = epsilon**2*np.convolve(cheb(N),cheb(N)) + np.hstack((np.zeros((1,2*N)).ravel(),1)) #r = roots(p1) #Evaluating the gain of the stable lowpass filter #The gain has to be 1/sqrt(1+epsilon^2) at Omega = 1 G = abs(np.polyval(p,1j))/np.sqrt(1+epsilon**2) #Plotting the magnitude response of the stable filter #and comparing with the desired response for the purpose #of verification Omega = np.linspace(0,2,201) H_stable = abs(G/np.polyval(p,1j*Omega)) H_cheb = abs(np.sqrt(1/np.polyval(p1,1j*Omega)))
from numpy import pi, cos, sin, eye from cheb import * ############################## "Define the inputs" Re = 10000. Reinv = 1./Re # N = 10 Ny = 128 kx = 1. kz = 0. n = Ny+1 ksq = kx*kx + kz*kz Idn = eye(n) D, y = cheb(Ny) u = (1. - y*y) U = (1. - y*y)*Idn DU = (np.matmul(D, u))*Idn D2 = np.matmul(D, D) D2U = (np.matmul(D2, u))*Idn D4 = np.matmul(D2, D2) Los = 1j*kx*u*(ksq*Idn - D2) + 1j*kx*D2U + Reinv*(D4 - 2*ksq*D2 + ksq*ksq*Idn) Lsq = 1j*kx*U + Reinv*(ksq*Idn - D2) L = np.block([[Los, np.zeros((n, n))],[1j*kz*(DU), Lsq]]) M = np.block([[(ksq*Idn - D2), np.zeros((n, n))], [np.zeros((n, n)), Idn ]]) """ BC for O-S test
epsilon = 0.4 N = 4 beta = ((np.sqrt(1+epsilon**2)+ 1)/epsilon)**(1/N) r1 = (beta**2-1)/(2*beta) r2 = (beta**2+1)/(2*beta) u = [1] for n in range(0,int(N/2)): phi = np.pi/2 + ((2*n + 1)*np.pi)/(2*N) v = [1,-2*r1*np.cos(phi),r1**2*np.cos(phi)**2+r2**2*np.sin(phi)**2] p = np.convolve(v,u) u = p p1 = epsilon**2 * np.convolve(cheb(N),cheb(N)) + np.append(np.zeros((2*N,)),[1]) G = (abs(np.polyval(p,1j)))/(np.sqrt(1+epsilon**2)) Omega = np.arange(0,2.01,0.01) H_stable = abs(np.divide(G,np.polyval(p,1j*Omega))) H_cheb = abs(np.sqrt(np.divide(1,np.polyval(p1,1j*Omega)))) plt.figure() plt.plot(Omega,H_stable,'o') plt.plot(Omega,H_cheb) plt.xlabel('$\Omega$') plt.ylabel('$|H_{a,LP}(j\Omega)|$') plt.legend(['Design','Specification']) plt.grid() plt.savefig("../figs/IIR_Cheb_lpDesign.eps")
from cheb import * from numpy import dot,diag,real,argsort,zeros,linspace,polyval,polyfit,where from scipy.linalg import eig from scipy.special import airy import matplotlib.pyplot as plt plt.figure(figsize=(10,8)) for N in range(12,60,12): print N D,x = cheb(N); D2 = dot(D,D); D2 = D2[1:N,1:N] Lam,V = eig(D2,diag(x[1:N])) Lam = real(Lam); ii = where(Lam>0)[0] V = real(V[:,ii]); Lam = Lam[ii] ii = argsort(Lam); ii=ii[4]; Lam=Lam[ii] v = zeros(N+1); v[1:N] = V[:,ii]; v = v/v[N/2]*airy(0.0)[0] xx = linspace(-1.0,1.0,200); vv = polyval(polyfit(x,v,N),xx); plt.subplot(2,2,N/12); plt.plot(xx,vv) plt.title("N = %d eig = %15.10f"%(N,Lam)); plt.show()
u = 1 for n in range(N // 2): phi = np.pi / 2 + (2 * n + 1) * np.pi / (2 * N) v = [ 1, (-2 * r1) * np.cos(phi), (r1**2) * ((np.cos(phi))**2) + (r2**2) * ((np.sin(phi))**2) ] p = np.convolve(v, u) u = p #The following was to verify that the roots obtained #are correct #roots(p) p1 = epsilon**2 * np.convolve( cheb(N), cheb(N)) + np.array([0 for _ in range(2 * N)] + [1]) #r = roots(p1) #Evaluating the gain of the stable lowpass filter #The gain has to be 1/sqrt(1+epsilon^2) at Omega = 1 G = abs(np.polyval(p, 1j)) / np.sqrt(1 + epsilon**2) #Plotting the magnitude response of the stable filter #and comparing with the desired response for the purpose #of verification Omega = np.linspace(0, 2, 201) H_stable = abs(G / np.polyval(p, 1j * Omega)) H_cheb = abs(np.sqrt(1 / np.polyval(p1, 1j * Omega))) plt.plot(Omega, H_stable,
beta = ((np.sqrt(1+epsilon**2)+ 1)/epsilon)**(1/N) r1 = (beta**2-1)/(2*beta) r2 = (beta**2+1)/(2*beta) '''Obtaining the polynomial approximation for the low pass Chebyschev filter to obtain a stable filter''' u = np.array([1]) for n in range(0,int(N/2)): #n = 0:(N/2)-1; phi = np.pi/2 + (2*n+1)*np.pi/(2*N) v = [1,-2*r1*np.cos(phi),r1**2*np.cos(phi)**2+r2**2*np.sin(phi)**2] p = np.convolve(v,u) u = p p1 = epsilon**2*np.convolve(cheb(N),cheb(N)) + np.concatenate([np.zeros(2*N),np.array([1])]) '''Evaluating the gain of the stable lowpass filter The gain has to be 1/sqrt(1+epsilon^2) at Omega = 1''' G = abs(np.polyval(p,1j))/np.sqrt(1+epsilon**2) '''Plotting the magnitude response of the stable filter and comparing with the desired response for the purpose of verification''' Omega = np.arange(0,2.01,0.01) #0:0.01:2 H_stable = abs(G/np.polyval(p,1j*Omega)) H_cheb = abs(np.sqrt(1/np.polyval(p1,1j*Omega))) plt.figure() plt.plot(Omega,H_stable,'o',label='Design')