% -*- mode: noweb; noweb-default-code-mode: R-mode; -*- \documentclass[a4paper,12pt,final]{article} \usepackage{python} \usepackage{graphicx} \begin{document} \title{Python Sweave Example \\ Frequency response of a moving average filter} \author{Matti Pastell} \maketitle \textbf{Create 11 point moving average filter and plot its frequency response and print the values.} <>= from pylab import * import scipy.signal as signal #A function to plot frequency and phase response def mfreqz(b,a=1): w,h = signal.freqz(b,a) h = abs(h) return(w/max(w), h) #Make the impulse response function n = 11. b = repeat(1/n, n) #Print impulse response filter print(b) @ \textbf{\\Calculate the frequency response and plot it:} <>= w, h = mfreqz(b) #Plot the function plot(w,h,'k') ylabel('Amplitude') xlabel(r'Normalized Frequency (x$\pi$rad/sample)') title(r'Frequency response of an 11 point moving average filter') savefig('ma_freq.pdf') @ \includegraphics{ma_freq.pdf} \end{document}