from __future__ import division from scipy import signal fig, axs = subplots(2,1,sharex=True) subplots_adjust( hspace = .2 ) fig.set_size_inches((5,5)) ax=axs[0] w,h=signal.freqz([1/2., 1/2.],1) # Compute impulse response ax.plot(w,20*log10(abs(h))) ax.set_ylabel(r"$20 \log_{10} |H(\omega)| $",fontsize=18) ax.grid() ax=axs[1] ax.plot(w,angle(h)/pi*180) ax.set_xlabel(r'$\omega$ (radians/s)',fontsize=18) ax.set_ylabel(r"$\phi $ (deg)",fontsize=18) ax.set_xlim(xmax = pi) ax.grid() # fig.savefig('figure_00@.png', bbox_inches='tight', dpi=300) Ns=30 # length of input sequence n= arange(Ns) # sample index x = cos(arange(Ns)*pi/2.) y= signal.lfilter([1/2.,1/2.],1,x) fig,ax = subplots(1,1) fig.set_size_inches(10,3) ax.stem(n,x,label='input',basefmt='b-') ax.plot(n,x,':') ax.stem(n[1:],y[:-1],markerfmt='ro',linefmt='r-',label='output') ax.plot(n[1:],y[:-1],'r:') ax.set_xlim(xmin=-1.1) ax.set_ylim(ymin=-1.1,ymax=1.1) ax.set_xlabel("n",fontsize=18) ax.legend(loc=0) ax.set_xticks(n) ax.set_ylabel("amplitude",fontsize=18); # fig.savefig('figure_00@.png', bbox_inches='tight', dpi=300) from matplotlib import gridspec fig=figure() fig.set_size_inches((10,5)) gs = gridspec.GridSpec(2,2) gs.update( wspace=0.5, hspace=0.5) ax = fig.add_subplot(subplot(gs[0,0])) ma_length = 8 # moving average filter length w,h=signal.freqz(ones(ma_length)/ma_length,1) ax.plot(w,20*log10(abs(h))) ax.set_ylabel(r"$ 20 \log_{10}|H(\omega)| $",fontsize=18) ax.set_xlabel(r"$\omega$",fontsize=18) ax.vlines(pi/3,-25,0,linestyles=':',color='r',lw=3.) ax.set_ylim(ymin=-25) ax.grid() ax = fig.add_subplot(subplot(gs[0,1])) ax.plot(w,angle(h)/pi*180) ax.set_xlabel(r'$\omega$ (radians/s)',fontsize=18) ax.set_ylabel(r"$\phi $ (deg)",fontsize=16) ax.set_xlim(xmax = pi) ax.set_ylim(ymin=-180,ymax=180) ax.vlines(pi/3,-180,180,linestyles=':',color='r',lw=3.) ax.grid() ax = fig.add_subplot(subplot(gs[1,:])) Ns=30 n= arange(Ns) x = cos(arange(Ns)*pi/3.) y= signal.lfilter(ones(ma_length)/ma_length,1,x) ax.stem(n,x,label='input',basefmt='b-') ax.plot(n,x,':') ax.stem(n[ma_length-1:],y[:-ma_length+1],markerfmt='ro',linefmt='r-',label='output') ax.plot(n[ma_length-1:],y[:-ma_length+1],'r:') ax.set_xlim(xmin=-1.1) ax.set_ylim(ymin=-1.1,ymax=1.1) ax.set_xlabel("n",fontsize=18) ax.set_xticks(n) ax.legend(loc=0) ax.set_ylabel("amplitude",fontsize=18); # fig.savefig('figure_00@.png', bbox_inches='tight', dpi=300) h=ones(ma_length)/ma_length # filter sequence yc=fft.ifft(fft.fft(h,len(x)+len(h)-1)*np.conj(fft.fft(x,len(x)+len(h)-1))).real fig,ax=subplots() fig.set_size_inches((10,2)) ax.plot(n,yc[ma_length-1:],'o-',label='DFT method') ax.plot(n,y,label='lfilter') ax.set_title('DFT method vs. signal.lfilter',fontsize=18) ax.set_xlabel('n',fontsize=18) ax.set_ylabel('amplitude',fontsize=18) ax.legend(loc=0) ax.set_xticks(n); # fig.savefig('figure_00@.png', bbox_inches='tight', dpi=300) %qtconsole