piccolbo/Pweave

View on GitHub
doc/examples/ma.rstw

Summary

Maintainability
Test Coverage
Pweave Example - Frequency response of a moving average filter
==============================================================

:Author: Matti Pastell <matti.pastell@helsinki.fi>
:Website: http://mpastell.com

**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 and use terminal formatted output (=doctest block.)**

<<term = True>>=
n = 11.
n
b = repeat(1/n, n)
b
@

**Calculate the frequency response and plot it:**

<<fig = True, caption = 'Frequency response of an 11 point moving average filter'>>=
w, h = mfreqz(b)
#Plot the function
plot(w,h,'k')
ylabel('Amplitude')
xlabel(r'Normalized Frequency (x$\pi$rad/sample)')
show()
@


**The first 10 values of the frequency response (w,h) as a table, notice that the code is hidden in the output document.**

.. csv-table::
   :header: "Amplitude", "Frequency"
   :widths: 15, 15

<<results = 'rst', echo = False>>=
print
for i in range(10):
    print('  ' , round(h[i],2) , ',' , round(w[i],2))
@