Introduction to Python Programming
The First Cell
import numpy as np
import [Link] as plt
FUNCTION: To plot a signal
def my_Plot_Function(xaxis, yaxis, xlabel, ylabel,
title):
[Link](figsize=(12,5))
[Link](xaxis, yaxis, lw=3)
[Link](xlabel)
[Link](ylabel)
[Link](title)
[Link]()
return
FUNCTION: Unit Step
def my_Unit_Step(t):
N = len(t)
xt = []
for n in range(N):
if t[n] < 0:
val = 0
else:
val = 1
[Link](val)
return xt
FUNCTION: Unit Ramp
def my_Unit_Ramp(t):
N = len(t)
xt = []
for n in range(N):
if t[n] < 0:
val = 0
else:
val = t[n]
[Link](val)
return xt
FUNCTION: Rectangular Pulse
def my_Rect(t, T):
N = len(t)
xt = []
for n in range(N):
if t[n] < -T/2 or t[n] > T/2 :
val = 0
else:
val = 1/T
[Link](val)
return xt
FUNCTION: Signum
def my_Signum(t):
N = len(t)
xt = []
for n in range(N):
if t[n] < 0:
val = -1
else:
val = 1
[Link](val)
return xt
FUNCTION: Sine to Square
def my_Sine_to_Square(xt, min, max ):
N = len(xt)
yt = []
for n in range(N):
if xt[n] < 0:
val = min
else:
val = max
[Link](val)
return yt
FUNCTION: Sync
def my_Sync(omega_not, A, t ):
N = len(t)
xt = []
for n in range(N):
if t[n] == 0:
val = A
else:
B = omega_not * t[n]
val = A*[Link](B)/B
[Link](val)
return xt
FUNCTION: Integration
def my_Integral(x,y):
N = len(x)
integral = []
[Link](0)
delta_x = x[1] - x[0]
for i in range(N-1):
temp1 = delta_x * (y[i+1] + y[i]) * 0.5
temp2 = integral[i] + temp1
[Link](temp2)
return integral
FUNCTION: Differentiation
def my_Derivative(x,y):
N = len(x)
dy_by_dx = []
delta_x = x[1] - x[0]
for i in range(N-1):
temp = (y[i+1]-y[i])/delta_x
dy_by_dx.append(temp)
dy_by_dx.append(temp) # repeating the last value,
to maintain the array size
return dy_by_dx
FUNCTION: Uniform Distribution
def my_Uniform_Distribution(a,b, x):
N = len(x)
fx = []
for n in range(N):
if x[n]<a or x[n]>b:
val = 0
else:
val = 1/(b-a)
[Link](val)
return fx
FUNCTION: Exponential Distribution
def my_Exponential_Distribution(a,x):
N = len(x)
fx = []
for n in range(N):
if x[n]<0:
val = 0
else:
val= a*[Link](-a*x[n])
[Link](val)
return fx
FUNCTION: Gaussian Distribution
def my_gaussian(mew,sigma,x):
A = 1/sigma/[Link](2*[Link])
B = [Link](-0.5*(x-mew)*(x-mew)/sigma/sigma)
return A*B
Partial Fraction Representation:
from [Link] import residue
num = [Link]([1]) # Numerator coefficients
(s^1, s^0)
den = [Link]([1, 6, 5]) # Denominator coefficients
(s^2, s^1, s^0)
r, p, k = residue(num,den)
print("Residues (r):", r)
print("Poles (p):", p)
FUNCTION: Pole-zero Plot s-plane
def my_Pole_Zero_Plot(num, den, L):
zeros = [Link](num)
poles = [Link](den)
print('Zeros at:',zeros)
print('Poles at:',poles)
[Link](figsize=(5,5))
[Link]([Link](zeros), [Link](zeros),'o')
[Link]([Link](poles), [Link](poles),'x')
[Link]([-L,L])
[Link]([-L,L])
[Link](0, color='k', lw=0.5)
[Link](0, color= 'k', lw=0.5)
[Link](0.7*L, 0.9*L,'s-plane')
[Link]()
return
FUNCTION: Frequency Response
def my_Frequency_Response_Linear(omega, A, B, C, D):
num_mag = [Link](A*A + B*B)
num_phase = np.arctan2(B,A)
den_mag = [Link](C*C + D*D)
den_phase = np.arctan2(D,C)
Mag = num_mag/den_mag
Phase = num_phase - den_phase
Phase = Phase * 180/[Link]
[Link](figsize=(12,5))
[Link](1,2,1)
[Link](omega, Mag)
[Link]('$ \omega $')
[Link]('$|X(\omega) | $ ')
[Link]('Magnitude Response')
[Link]()
[Link](1,2,2)
[Link](omega, Phase)
[Link]('$ \omega $')
[Link]('$<X(\omega) $ (degrees) ')
[Link]('Phase Response')
[Link]()
return
Example -1: Fourier Series
# Example I: Fourier series of a squarewave
$ X[n] =\displaystyle \sum_{n=1, 3, 5, ...}^{\
infty} \frac{4A}{n\pi}{sin(n\omega_0 t)} $
where A is the amplitude
and $\omega_0$ is the fundamental frequency in
radians/sec
N = 11 # the number of harmonics to be
included
xt , Xn, m = 0, [], []
for n in range(1, N+1, 2):
val = (4*A/n/[Link])
xt = xt + val * [Link]( n * omega_not * t)
[Link](val)
[Link](n)
my_Plot_Fourier_Series_and_Signal(m, Xn, t, xt )
Example - 2: Fourier Series
# Example II: Fourier series of saw-tooth waveform
$ X[n] =\frac{A}{2} +\displaystyle \
sum_{n=1,2,3,...}^{\infty} \frac{(-1)^n A}{n\pi}
{sin(n\omega_0 t)} $
where A is the amplitude
and $\omega_0$ is the fundamental frequency in
radians/sec
N = 10 # the number of harmonics to be included
xt = A/2
Xn= ([xt])
m = ([0])
for n in range(1,N+1):
B = (-1)**(n+1) * (A/n/[Link])
xt = xt + (B * [Link](n*omega_not *t))
[Link](B)
[Link](n)
my_Plot_Fourier_Series_and_Signal(m, Xn, t, xt )
Example - 3: Fourier Series
# Example V: Fourier Full-wave rectified waveform
$ X[n] =\frac{2A}{\pi} + \displaystyle \
sum_{n=1,2,3,...}^{\infty} \frac{4 A}{\pi (1- 4n^2)}
{cos(n\omega_0 t)} $
where A is the amplitude
and $\omega_0$ is the fundamental frequency in
radians/sec
N = 11
xt = (2*A/[Link])
Xn = ([xt])
m = ([0])
for n in range(1,N+1):
B = (4*A/[Link])* (1/(1-4*n*n))
xt = xt + B * [Link](n*omega_not * t)
[Link](B)
[Link](n)
my_Plot_Fourier_Series_and_Signal(m, Xn, t, xt )
Audio Example-1
from [Link] import Audio
N=47000 # number of samples in selected
duration
t = [Link](0,1,N)
Ts = t[2]-t[1]
rate = 1/Ts
print(Ts)
freq = 400.0
signal = [Link](2*[Link]*freq*t)
Audio(data=signal, rate=rate)
Audio Example-2
noise = [Link](N)- 0.5
noisy_signal = signal + 0.4*noise
Audio(data=noisy_signal, rate=rate)
Audio Example-3
from [Link] import Audio
N = 47000 # number of samples in selected
duration
t = [Link](0,1,N)
Ts = t[2]-t[1]
rate = 1/Ts
f1, f2, f3, f4, f5, f6, f7, f8 = 240.0, 270.0,
300.0, 320.0, 360.0, 400.0, 450.0, 480.0
s1 = [Link](2*[Link]*f1*t)
s2 = [Link](2*[Link]*f2*t)
s3 = [Link](2*[Link]*f3*t)
s4 = [Link](2*[Link]*f4*t)
s5 = [Link](2*[Link]*f5*t)
s6 = [Link](2*[Link]*f6*t)
s7 = [Link](2*[Link]*f7*t)
s8 = [Link](2*[Link]*f8*t)
signal_1 = [Link]((s1,s2,s3,s4,s5,s6,s7,s8))
signal_2 = [Link](signal_1)
signal = [Link]((signal_1 , signal_2 ))
Audio(data=signal, rate=rate)
signal = [Link]((s1,s2,s1,s2, s1,s2,
s3,s4,s5,s6,s5,s6,s7,s8))
Audio(data=signal, rate=rate)
Audio Example-4
from [Link] import Audio
N=47000 # number of samples in selected
duration
t = [Link](0,4,N*4)
Ts = t[2]-t[1]
rate = 1/Ts
print(Ts)
f1 = 350.0
f2 = 440.0
s1 = [Link](2*[Link]*f1*t)
s2 = [Link](2*[Link]*f2*t)
signal = s1 + s2
Audio(data=signal, rate=rate)
Audio Example-5
from [Link] import Audio
N=47000 # number of samples in selected
duration
t = [Link](0,1,N)
Ts = t[2]-t[1]
rate = 1/Ts
print(Ts)
f1 = 440.0
f2 = 480.0
s1 = [Link](2*[Link]*f1*t)
s2 = [Link](2*[Link]*f2*t)
s3 = s1 + s2
pause = [Link](N)
signal = [Link]((s3,pause, s3,pause,s3))
Audio(data=signal, rate=rate)
References:
1. Book Chapter: Chapter 5: Buddhi, K. (2024). Pedagogy Improvements After the
International Engineering Educator Certification Program. In: Guralnick, D., Auer,
M.E., Poce, A. (eds) Creative Approaches to Technology-Enhanced Learning for the
Workplace and Higher Education. TLIC 2024. Lecture Notes in Networks and Systems,
vol 1150. Springer, Cham. [Link]
2. Book Chapter: Chapter 8: Buddhi, K. (2023). A Pedagogy for Engineering Concepts
Focusing on Experiential Learning. In: Guralnick, D., Auer, M.E., Poce, A. (eds)
Creative Approaches to Technology-Enhanced Learning for the Workplace and
Higher Education. TLIC 2023. Lecture Notes in Networks and Systems, vol 767.
Springer, Cham. [Link]
3. Book: Digital Signal Processing Concepts using Python‘, B Kanmani; ISTE-WPLP
Learning Material series with specialization in, ‘Electronics and Communication’,
from the Indian Society for Technical Education, (An AICTE Project), published by
Khanna Book Publishing Co. (P) Ltd., New Delhi, First Edition 2023; Reprint 2024;
ISBN 978-93-5538-268-9;
4. YouTube Video – [Link]
5. YouTube Video: [Link]
6. YouTube Video: [Link]
7. Conference Publication: B. Kanmani, "Introducing signals and systems concepts
through analog signal processing first," 2011 Digital Signal Processing and Signal
Processing Education Meeting (DSP/SPE), Sedona, AZ, USA, 2011, pp. 84-89, doi:
10.1109/DSP-SPE.2011.5739191.