Example of Open Source Literate Dynabook: Understanding Signals

This provides a sample to show how an author or a teacher might use an IPyNotebook as an "Open Source Dynabook" where the the reader/student can interact with his text book to learn in an immersive way the basics of signal processing.


In [26]:
%matplotlib inline
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
from scipy.fftpack import fft, ifft

from IPython.html.widgets import interact, interactive, fixed
from IPython.html import widgets
from IPython.display import clear_output, display, HTML

In [31]:
# Number of samplepoints
N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
# x

In [32]:
def eq1(**ka):  # a1,f1,a2,f2
    y = ka['a1'] * np.sin(ka['f1'] * 2.0*np.pi*x) + ka['a2'] * np.sin(ka['f2'] * 2.0* np.pi * x)  
    plt.plot(x, y); plt.grid(); plt.show()


# eq1(1.0, 50.0, .5, 80.)

interact(eq1,
    # Temp=(0,10),            # slider?
    # Current=(0.,10.,0.01),  # slider?
    # z=True,                 # on/off widget   
    # Text=u'Type here!',       # text box widget
    #Algorithm=['This','That','Other'],
    a1=widgets.FloatSliderWidget(min=0.0, max=1.0, step=0.01, value=1.0, description="Float (a)"),
    a2=widgets.FloatSliderWidget(min=0.0, max=1.0, step=0.01, value=.5, description="Float (a)"),
    f1=widgets.FloatSliderWidget(min=0.0, max=100.0, step=0.1, value=50.0, description="Float (a)"),
    f2=widgets.FloatSliderWidget(min=1, max=100.0, step=0.1, value=80.0, description="Float (a)"))



In [11]:
def show_args(**kwargs):
    
    s = '<h3>Arguments:</h3><table>\n'
    for k,v in kwargs.items():
        s += '<tr><td>{0}</td><td>{1}</td></tr>\n'.format(k,v)
    s += '</table>'
    display(HTML(s))

In [14]:
show_args(a=10, b='Hi There', c=True)


Arguments:

a10
cTrue
bHi There

In [15]:
i = interact(show_args,
         Temp=(0,10),
         Current=(0.,10.,0.01),
         z=True,
         Text=u'Type here!',
         #Algorithm=['This','That','Other'],
         a=widgets.FloatSliderWidget(min=-10.0, max=10.0, step=0.1, value=5.0, description="Float (a)")
         )


Arguments:

Current4.86
TextType here!
zTrue
a2.4
Temp5

Fourier Analysis and the FFT

Fourier analysis is a method for expressing a function as a sum of sine and cosine functions. When both the function and its Fourier transforms are implented with discrete components it is called the Discrete Fourier Transform or DFT. Typically it is implemented using the Fast Fourier Transform algorithm invented by Cooley and Tukey. Below we show the formulation for the FFT in exponential form.

\begin{equation*} y[k] = \sum_{n=0}^{N-1} e^{-2 \pi j \frac{k n}{N} } x[n] \, \end{equation*}

In [ ]:
x = np.array([1.0, 2.0, 1.0, -1.0, 1.5])
y = fft(x)
y

In [ ]:
yinv = ifft(y)
yinv

In [20]:
yf = fft(y)

# Create N/2 evenly spaced numbers over interval: 0.0, 1.0/(2.0*T) 
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)

# Display all the resulting magnitude yf of frequencies from 0 to N/2 Hz.
plt.plot(xf, 2.0/N * np.abs(yf[0:N/2]))
plt.grid(); plt.show()