ax21 = fig2.add_subplot(211) W, H = sig.freqz(h) F = W /(2 * pi) plot(F, abs(H)) ax21.set_ylabel(r'$|H(\mathrm{e}^{\mathrm{j} 2 \pi F})| \rightarrow$') #ax2.xaxis.set_label_coords(2, 0.5, transform=ax1.transData) #ax2.yaxis.set_label_coords(-0.3, 2.3, transform=ax1.transData) #ax2.set_ylim([-2,3]) ax22 = fig2.add_subplot(212) plot(F, angle(H) /pi *180) ax22.set_ylabel(r'$\angle H(\mathrm{e}^{\mathrm{j} 2 \pi F})\; \mathrm {in} \; \deg \rightarrow$') ax22.set_xlabel(r'$F \rightarrow$') if EXPORT: fig2.savefig(BASE_DIR + FILENAME +"_Hf" + FMT) fig3 = figure(num=3) ax3 = fig3.add_subplot(111) dsp.zplane(h, zpk=False, plt_ax = ax3) ax3.set_xlabel(r'reell $\rightarrow$') ax3.set_ylabel(r'imaginÀr $ \rightarrow$') if EXPORT: fig3.savefig(BASE_DIR + FILENAME +"_PN" + FMT) ax3.set_ylim([-1.1,1.1]) ax3.set_xlim([-2.5,1]) plt.show()
################################################################# # # # Plot the Results # # # ################################################################# plt.close('all') # close all "old" figures #mlab.close(all=True) # alle Mayavi (3D) Fenster schließen #========================================= ## Pol/Nullstellenplan #========================================= if SHOW_POLE_ZERO == True: plt.figure(1) [z, p, k] = dsp.zplane(bb, aa) plt.title('Pol/Nullstellenplan') plt.grid('on') # plt.text(-0.95,0.0 ,'(2)') plt.axis([-1.1, 1.1, -1.1, 1.1]) plt.tight_layout() if DEF_PRINT == True: plt.savefig((PRINT_PATH +'PZ' + PRINT_TYPE)) #========================================= ## Impulsantwort #========================================= if SHOW_IMPZ == True: plt.figure(2) [h, td]=dsp.impz(bb, aa, f_S) #Impulsantwort / Koeffizienten
################################################################# # # # Plot the Results # # # ################################################################# plt.close('all') # close all "old" figures #mlab.close(all=True) # alle Mayavi (3D) Fenster schließen #========================================= ## Pol/Nullstellenplan #========================================= if SHOW_POLE_ZERO == True: plt.figure(1) [z, p, k] = dsp.zplane(bb, aa) plt.title('Pol/Nullstellenplan') plt.grid('on') # plt.text(-0.95,0.0 ,'(2)') plt.axis([-1.1, 1.1, -1.1, 1.1]) plt.tight_layout() if DEF_PRINT == True: plt.savefig((PRINT_PATH + 'PZ' + PRINT_TYPE)) #========================================= ## Impulsantwort #========================================= if SHOW_IMPZ == True: plt.figure(2) [h, td] = dsp.impz(bb, aa, f_S) #Impulsantwort / Koeffizienten
Hmax = max(H_abs); Hmax_dB = 20*log10(Hmax) Wmax = W[np.argmax(H_abs)] # frequency where |H(Omega)| is max. H_angle = np.unwrap(angle(H)) H_abs = abs(H)/Hmax print('--------------------------------') print('zeta[i] = ',zeta[i]) print('W_3dB[i] = ', W_3dB[i]) print('W_max, H_max (calc.) = ',W_max[i], H_max[i]) print('Wmax, Hmax (sim.) = ',Wmax, Hmax) print('H_max_rel = ', H_max_rel) #=============================================================== ## Poles and zeros #=============================================================== figure(1) dsp.zplane(bb, aa, analog=True, style = 'square', anaCircleRad=W_c, mpc = (1-i/len(zeta),i/len(zeta),i/len(zeta)), plabel = zeta_label, lw = 3) axzp = plt.gca() axzp.set_xlabel(r'$\sigma \; \rightarrow$',fontsize=18) # axzp.set_xlabel(r'$\sigma \,/ \omega_{3dB}\; \rightarrow$',fontsize=18) axzp.set_ylabel(r'$j \omega \; \rightarrow$',fontsize=18) # axzp.set_ylabel(r'$j \omega \,/ \omega_{3dB}\; \rightarrow$',fontsize=18) axzp.legend(loc = 'best') plt.tight_layout() #=============================================================== ## Magnitude Response #=============================================================== fig2 = figure(2) ax21 = fig2.add_subplot(111) l1, = ax21.plot(W,H_abs, label = zeta_label)
from matplotlib.pyplot import (figure, plot, stem, grid, xlabel, ylabel, subplot, title, clf, xlim, ylim) import sys sys.path.append('..') import dsp_fpga_lib as dsp # Definiere Nullstellen auf EK: N = np.asarray([np.exp(1j * 0.3 * 2 * pi), np.exp(-1j * 0.3 * 2 * pi)]) # Pole: gleicher Winkel, kleinerer Radius: P = 0.95 * N # "Ausmultiplizieren" von P/N ergibt Koeff. b = np.poly(N) a = np.poly(P) figure(1) dsp.zplane(N, P, zpk=True) figure(2) subplot(211) # Frequenzgang an 2048 Punkten: [W, H] = sig.freqz(b, a, 2048) F = W / (2 * pi) plot(F, 20 * log10(abs(H))) grid(True) subplot(212) plot(F, angle(H)) xlabel('F ->') grid(True) plt.tight_layout() # Testfreq. (normierte Kreisfreq.): W_test = array([0, 0.29, 0.3, 0.31, 0.5]) * 2 * pi # Frequenzgang bei Testfrequenzen:
[Lb,F_b] = sig.buttord(F_DB,F_SB,A_DB,A_SB) #[b, a] = sig.butter(Lb, F_b) #=== IIR-Wrapper (nur Python) ===== #[b, a] = sig.iirdesign(F_DB, F_SB, # A_DB, A_SB, ftype='ellip') ############################################ print('Filterkoeffizienten:') print('a = ', a); print('b = ', b) ## Calculate H(w), w = 0 ... pi, 1024 Pts. [w, H] = sig.freqz(b, a, 1024) # Translate w to physical frequencies: f = w / (2 * pi) * f_S ############## Plot the Results ######### ## Pol/Nullstellenplan fig1 = figure(1); ax1 = fig1.add_subplot(111) [z, p, k] = dsp.zplane(b, a, plt_ax = ax1) ## ----- Impulsantwort ----- figure(2); grid('on') [h, td] = dsp.impz(b, a, f_S) #Impulsantwort / Koeffizienten [ml, sl, bl] = stem(td, h) plt.setp(ml, 'markerfacecolor', 'r', 'markersize', 8) title(r'Impulsantwort h[n]') ## ----- Linear frequency plot ----- figure(3); grid('on') plot(f, abs(H)) title(r'Betragsfrequenzgang') ## Log. Frequenzgang mit Spezifikationen figure(5) subplot (211) plot(f,20 * log10(abs(H)), 'r'); plt.grid('on') plot([0, f_DB],[-A_DB, -A_DB],'b--') # untere Spec-Grenze
#=== IIR-Wrapper (nur Python) ===== #[b, a] = sig.iirdesign(F_DB, F_SB, # A_DB, A_SB, ftype='ellip') ############################################ print('Filterkoeffizienten:') print('a = ', a) print('b = ', b) ## Calculate H(w), w = 0 ... pi, 1024 Pts. [w, H] = sig.freqz(b, a, 1024) # Translate w to physical frequencies: f = w / (2 * pi) * f_S ############## Plot the Results ######### ## Pol/Nullstellenplan fig1 = figure(1) ax1 = fig1.add_subplot(111) [z, p, k] = dsp.zplane(b, a, plt_ax=ax1) ## ----- Impulsantwort ----- figure(2) grid('on') [h, td] = dsp.impz(b, a, f_S) #Impulsantwort / Koeffizienten [ml, sl, bl] = stem(td, h) plt.setp(ml, 'markerfacecolor', 'r', 'markersize', 8) title(r'Impulsantwort h[n]') ## ----- Linear frequency plot ----- figure(3) grid('on') plot(f, abs(H)) title(r'Betragsfrequenzgang') ## Log. Frequenzgang mit Spezifikationen figure(5) subplot(211)
W_max = 2 # normalized circular frequency; W = 2 pi f tau W = np.linspace(0, W_max, 201) # start, stop, step. endpoint is included [W,H] = sig.freqs(bb, aa, W) # Calculate H(w) at the frequency points w1 #[w,H]=sig.freqs(bb,aa) # calculate H(w) at 200 frequencies "around the # interesting parts of the response curve" f = W H_abs = abs(H) H_max = max(H_abs); H_max_dB = 20*log10(H_max) W_max = W[np.argmax(H_abs)] # frequency where |H(Omega)| is max. H_angle = np.unwrap(angle(H)) #==================================================== figure(1) dsp.zplane(bb, aa, analog=True, style = 'square', anaCircleRad=W_c, mzc = (0,0.5,0) ) axzp = plt.gca() axzp.set_xlabel(r'$\sigma \, / \,\omega_n \; \rightarrow$',fontsize=18) axzp.set_ylabel(r'$j \omega \, / \,\omega_n \; \rightarrow$',fontsize=18) plt.tight_layout() #==================================================== fig2 = figure(2) ax21 = fig2.add_subplot(111) ax22 = ax21.twinx() l1, = ax21.plot(W,abs(H)) l2, = ax22.plot(W,H_angle/pi,'g') ax21.set_xlabel(r'$\omega\, / \,\omega_n \; \rightarrow$') ax21.set_ylabel(r'$|H(j \omega)| \; \rightarrow$') ax22.set_ylabel(r'$\angle H(j \omega)/\pi \; \rightarrow$')
from matplotlib.pyplot import (figure, plot, stem, grid, xlabel, ylabel, subplot, title, clf, xlim, ylim) import sys sys.path.append('..') import dsp_fpga_lib as dsp alpha = 0.9; f_S = 1 b = [1, 0] # z + 0 # b = [1 0 0] # z^2 + 0 a = [1, -alpha] # z - 0.9; Add., 1 Verzögerung #a = [1 +alpha] # z + 0.9; Subtr., 1 Verz. #a = [1 0 -alpha] # z^2 - 0.9; Add., 2 Verz. #a = [1 0 +alpha] # z^2 - 0.9; Subtr., 2 Verz. dsp.zplane(b,a)#, plt_ax = ax) # Plotte P/N Diagramm # H(f) entlang der oberen Hälfte des EK: [W,H] = sig.freqz(b,a,1024, f_S) fig2 = figure(2) plot(W/(2*pi),abs(H),linewidth = 2) # H(F) xlabel(r'$F$ bzw. $\Omega / 2 \pi$') ylabel(r'$|H(F)| \; \rightarrow$') # Berechne 20 Werte der Impulsantwort: [himp,t] = dsp.impz(b,a,20,f_S) figure(3) (ml, sl, bl) = stem(t,himp) # Impulsantwort plt.setp(ml,'markerfacecolor','r', 'markersize',8) plt.setp(sl,'linewidth',2) xlabel('$n$'); ylabel(r'$h[n]$') plt.show()
TC = np.empty(max) # time constants # go through all the powers of two for l in range(max): # H(z)=B/A=2**-l/(z-(1-2**l)) # first order b = [2**-l, 0] # first order a = [1, -(1 - (2**-l))] # first order # b=[2**-(2*l),0,0] # second order # a=[1,-2*(1-2**-l),(1-2**-l)**2] # second order plt.figure(0) flog = np.logspace(-8, 1, 1000) [f, Hf] = signal.freqz(b, a, flog) Hf = 20 * np.log10(abs(Hf)) fHz = (f / (2 * np.pi)) * f_s plt.plot(fHz, Hf) plt.figure(1) zplane(b, a) fc[l] = fHz[np.argmin(abs(Hf + 3))] TC[l] = 1 / (2 * np.pi * fc[l]) print(f"2^{l}: fc= {fc[l]}Hz TC={TC[l]} sec ") plt.figure(0) plt.minorticks_on() plt.grid(b=True, which='both', axis='both') plt.xscale('log') plt.xlim(10E-4, 450E3) plt.show()
Ts = 1/200.0 # sampling period f1 = 50.0 # signal frequency phi0 = 0 # signal initial phase Tmax = 6.0/f1 # time for 6 signal periods N_Ts = Tmax / Ts # No. of samples in Tmax # -- Sampled input signal and filter coeffs. n = arange(0,round(N_Ts)) # sample n xn = 1.5 + 0.5*cos(2.0*pi*f1*n*Ts + phi0) b = np.ones(4)/4; a = 1 # MA-filter, N = 5 #b = np.convolve([1,1,1],[1,1,1]); a = 1 #b = [1, 0]; a = [1, -0.9] # lossy integr. # # ----- P/Z-Plot ----- figure(1) title('Pole/Zero-Plan') dsp.zplane(b,a) # ----- frequency response ----- figure(2) [W, H] = sig.freqz(b, a, whole=0); f = W / (Ts * 2 * pi) (w,Asig) = sig.freqz(b,a, f1*Ts*2*pi) H_mx = np.max(abs(H)) H = H / H_mx; Asig = abs(Asig)/H_mx # subplot(311) plot(f,abs(H)) ylabel(r'$|H(e^{j \Omega})| \rightarrow$') title('Frequency Response') plt.annotate('Attenuation @ %.1f Hz \n \ = %1.3f (%3.1f dB)'%(f1,Asig,20*log10(Asig)),\ (f1, Asig),(0.5,0.5),textcoords='axes fraction',\
H_abs = abs(H) / Hmax print('--------------------------------') print('zeta[i] = ', zeta[i]) print('W_3dB[i] = ', W_3dB[i]) print('W_max, H_max (calc.) = ', W_max[i], H_max[i]) print('Wmax, Hmax (sim.) = ', Wmax, Hmax) print('H_max_rel = ', H_max_rel) #=============================================================== ## Poles and zeros #=============================================================== figure(1) dsp.zplane(bb, aa, analog=True, style='square', anaCircleRad=W_c, mpc=(1 - i / len(zeta), i / len(zeta), i / len(zeta)), plabel=zeta_label, lw=3) axzp = plt.gca() axzp.set_xlabel(r'$\sigma \; \rightarrow$', fontsize=18) # axzp.set_xlabel(r'$\sigma \,/ \omega_{3dB}\; \rightarrow$',fontsize=18) axzp.set_ylabel(r'$j \omega \; \rightarrow$', fontsize=18) # axzp.set_ylabel(r'$j \omega \,/ \omega_{3dB}\; \rightarrow$',fontsize=18) axzp.legend(loc='best') plt.tight_layout() #=============================================================== ## Magnitude Response #===============================================================
from matplotlib.pyplot import (figure, plot, stem, grid, xlabel, ylabel, subplot, title, clf, xlim, ylim) import sys sys.path.append('..') import dsp_fpga_lib as dsp # Definiere Nullstellen auf EK: N = np.asarray([np.exp(1j*0.3*2*pi), np.exp(-1j*0.3*2*pi)]) # Pole: gleicher Winkel, kleinerer Radius: P = 0.95 * N # "Ausmultiplizieren" von P/N ergibt Koeff. b = np.poly(N); a = np.poly(P) figure(1) dsp.zplane(N,P,zpk = True) figure(2) subplot(211) # Frequenzgang an 2048 Punkten: [W,H] = sig.freqz(b,a,2048) F = W / (2* pi) plot(F, 20*log10(abs(H))); grid(True) subplot(212) plot(F,angle(H)) xlabel('F ->'); grid(True) plt.tight_layout() # Testfreq. (normierte Kreisfreq.): W_test = array([0, 0.29, 0.3, 0.31, 0.5])*2*pi # Frequenzgang bei Testfrequenzen: [H_test, W_test]=sig.freqz(b,a,W_test) print('H_test =', H_test)
#=======================================================1 # Hilbert-Transformer: zero at f = 0 and f = fS/2 # -> antisymmetric, odd numtaps b = sig.firwin2(L, [0, 0.01, 0.5, 0.99, 1], [0, 1, 1, 1, 0], antisymmetric=True) #b = sig.remez(L, [0, 0.1, 0.11, 0.4, 0.41, 0.5], [0,1,0], [0.1,10,0.1],type = 'hilbert') b = b / sum(abs(b)) print(b) [w, H] = sig.freqz(b, a, 1024) # Translate w to physical frequencies: f = w / (2 * pi) * f_S ############## Plot the Results ######### ## Pol/Nullstellenplan figure(1) [z, p, k] = dsp.zplane(b, a) ## ----- Impulsantwort ----- figure(2) [h, td] = dsp.impz(b, a, f_S) #Impulsantwort / Koeffizienten [ml, sl, bl] = stem(td, h) plt.setp(ml, 'markerfacecolor', 'r', 'markersize', 8) title(r'Impulsantwort h[n]') ## ----- Linear frequency plot ----- figure(3) plot(f, abs(H)) title(r'Betragsfrequenzgang') ## Log. Frequenzgang mit Spezifikationen figure(4) subplot(211) plot(f, 20 * log10(abs(H)), 'r') plt.grid('on')
#=======================================================1 # Hilbert-Transformer: zero at f = 0 and f = fS/2 # -> antisymmetric, odd numtaps b = sig.firwin2(L, [0,0.01, 0.5, 0.99, 1], [0,1, 1, 1,0], antisymmetric = True) #b = sig.remez(L, [0, 0.1, 0.11, 0.4, 0.41, 0.5], [0,1,0], [0.1,10,0.1],type = 'hilbert') b = b / sum(abs(b)) print (b) [w, H] = sig.freqz(b, a, 1024) # Translate w to physical frequencies: f = w / (2 * pi) * f_S ############## Plot the Results ######### ## Pol/Nullstellenplan figure(1) [z, p, k] = dsp.zplane(b, a) ## ----- Impulsantwort ----- figure(2) [h, td] = dsp.impz(b, a, f_S) #Impulsantwort / Koeffizienten [ml, sl, bl] = stem(td, h) plt.setp(ml, 'markerfacecolor', 'r', 'markersize', 8) title(r'Impulsantwort h[n]') ## ----- Linear frequency plot ----- figure(3) plot(f, abs(H)) title(r'Betragsfrequenzgang') ## Log. Frequenzgang mit Spezifikationen figure(4) subplot (211) plot(f,20 * log10(abs(H)), 'r'); plt.grid('on') plot([0, f_DB],[-A_DB, -A_DB],'b--') # untere Spec-Grenze