Content and Objectives

  • Show effects of changing width and amplitude of a rectangular pulse in the frequency domain

In [8]:
import numpy as np

import matplotlib.pyplot as plt
import matplotlib
from ipywidgets import interactive
import ipywidgets as widgets

# showing figures inline
%matplotlib inline

In [62]:
def plot_rect(A,T, t0):
    x_range = np.arange(-20,20,0.05)
    y = np.zeros(len(x_range))
    start_rect = -T/2 + t0
    stop_rect = +T/2 + t0
    y[(x_range > start_rect) & (x_range < stop_rect)] = A
    
    plt.figure(1,figsize=(15,6))
    plt.rcParams.update({'font.size': 14})
    plt.subplot(1,2,1)
    plt.plot(x_range, y, linewidth=2)
    plt.xlim((-20,20))
    plt.grid(True)
    plt.xlabel("Zeit t (s)")
    plt.ylabel("y = g(t)")

    plt.subplot(1,2,2)
    f_range = np.linspace(-3,3,len(x_range))
    G = np.abs(A*np.sinc(T*f_range))
    plt.plot(f_range, G, linewidth=2)
    plt.grid(True)
    plt.xlabel("Frequenz f (Hz)")
    plt.ylabel("y = |G(f)|/T")
    plt.show()

In [63]:
mystyle={'description_width': 'initial'}
interactive_update = interactive(plot_rect, \
                                 A = widgets.FloatSlider(min=0.0,max=5.0,step=0.1,value=1, continuous_update=False, style=mystyle, description='Amplitude A',layout=widgets.Layout(width='50%')), \
                                 T = widgets.FloatSlider(min=1, max=20, value=10, continuous_update=False, style=mystyle, description='Pulse Width T',layout=widgets.Layout(width='50%')), \
                                 t0 = widgets.FloatSlider(min=-8, max=8, step=0.1, value=0, continuous_update=False, style=mystyle, description='Offset t0',layout=widgets.Layout(width='50%')))

output = interactive_update.children[-1]
output.layout.height = '400px'
interactive_update