Skip to content

Python-based Digital Signal Processing Materials for use in DSP classes

Notifications You must be signed in to change notification settings

laseraph/PythonDSP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PythonDSP

Python-based Digital Signal Processing Materials for use in DSP classes
This repository contains a collection of Python functions designed to simulate fundamental Digital Signal Processing (DSP) operations. It is intended for students and educators to visualize and manipulate discrete-time signals, replicating standard MATLAB DSP toolboxes in a Pythonic environment.

📦 Dependencies

To run the scripts and functions in this repository, you will need:

  • NumPy: For array manipulation and mathematical operations.
  • Matplotlib: For plotting and visualizing signals.

To install the required libraries, run:

pip install numpy matplotlib

🚀 Usage

Import the functions from the main module sigfunctions.py into your script:

import numpy as np  
import matplotlib.pyplot as plt  
from sigfunctions import stepseq, sigshift, sigadd

📚 Function Reference

Signal Generation

impseq(n1, n2, n0)

Generates a unit sample sequence (impulse) $\delta[n-n_0]$ over the interval $n_1 \le n \le n_2$.

  • Returns: n (time vector), x (signal array)

stepseq(n1, n2, n0)

Generates a unit step sequence $u[n-n_0]$ over the interval $n_1 \le n \le n_2$.

  • Returns: n (time vector), x (signal array)

Signal Operations

sigshift(x, m, k)

Shifts a signal $x[n]$ by $k$ units, resulting in $y[n] = x[n-k]$.

  • Returns: y (shifted signal), n (new time vector)

sigfold(x, n)

Folds (time-reverses) a signal about $n=0$, resulting in $y[n] = x[-n]$.

  • Returns: n (folded time vector), y (folded signal)

evenodd(x, n)

Decomposes a real signal $x[n]$ into its even and odd components.

  • Returns: xe (even part), xo (odd part), m (symmetric time vector)

Arithmetic & Systems

sigadd(x1, n1, x2, n2)

Adds two signals $x_1[n]$ and $x_2[n]$ with different time supports.

  • Automatically aligns time vectors and handles zero-padding.
  • Returns: y (summed signal), n (common time vector)

sigmult(x1, n1, x2, n2)

Multiplies two signals $x_1[n]$ and $x_2[n]$ element-wise.

  • Returns: y (product signal), n (common time vector)

conv_ext(x, nx, h, nh)

Computes the convolution $y[n] = x[n] * h[n]$ with correct time index calculation.

  • Returns: y (convolved signal), ny (resulting time vector)

corr_ext(x, nx, h, nh)

Computes the correlation $y[n] = x[n] $\star$ h[n]$ with correct time index calculation.

  • Returns: y (convolved signal), ny (resulting time vector)

Transforms

DTFT(x, n1, n2, k, delta)

Computes the Discrete-Time Fourier Transform.

  • Parameters: k (frequency index range), delta (frequency step size in radians).
  • Returns: w (frequency vector), X (computed DTFT values)

approxCTFT(x, t_start, t_end, num_points, w_max, delta)

Numerical approximation of the Continuous-Time Fourier Transform.

  • Returns: t (time vector), x (sampled signal), W (freq vector), Xa (approximate CTFT)

DFT(xn, N)

Computes the Discrete Fourier Transform.

  • Returns: Xk (DFT coeff. array over $0 \leq k \leq N-1$)

IDFT(Xn, N)

Computes the inverse Discrete Fourier Transform.

  • Returns: xn (N-point sequence over $0 \leq n \leq N-1$)

📝 Example

import numpy as np  
import matplotlib.pyplot as plt  
from sigfunctions import stepseq, sigadd

# 1. Generate two step sequences  
n1, x1 = stepseq(0, 10, 0)  
n2, x2 = stepseq(5, 15, 0)

# 2. Add them together (handles different time ranges automatically)  
y, n = sigadd(x1, n1, x2, n2)

# 3. Plot  
plt.stem(n, y)  
plt.title("Signal Addition Example")  
plt.xlabel("n")  
plt.ylabel("Amplitude")  
plt.show()

About

Python-based Digital Signal Processing Materials for use in DSP classes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages