0% found this document useful (0 votes)
8 views7 pages

FIR Filter Lab with Python Techniques

This lab focuses on FIR filter convolution using Python, enabling students to design and analyze low pass, high pass, and band pass filters. It includes practical applications for synthetic signals, audio, and images, utilizing libraries such as NumPy and Matplotlib. Students will gain hands-on experience in digital signal processing concepts and techniques through various experiments and assignments.

Uploaded by

innocenthitman45
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

FIR Filter Lab with Python Techniques

This lab focuses on FIR filter convolution using Python, enabling students to design and analyze low pass, high pass, and band pass filters. It includes practical applications for synthetic signals, audio, and images, utilizing libraries such as NumPy and Matplotlib. Students will gain hands-on experience in digital signal processing concepts and techniques through various experiments and assignments.

Uploaded by

innocenthitman45
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Department of Intelligent System

The University of Lahore

Subject: Numerical Computing System / Numerical Computing with Python

Python Lab 07 – FIR Filter Convolution with Synthetic Signals, Audio, and Images

Instructor: Asim Maqsood

Department of Intelligent Systems

Prerequisites: Basic signals/systems, Fourier transform, sampling.

Tools Required: Python (NumPy, Matplotlib), Jupyter Notebook or VS Code

Lab Objectives

After completing this lab, students will be able to:

1. Understand FIR filters and convolution

2. Design FIR Low Pass, High Pass, and Band Pass filters

3. Analyze frequency response of FIR filters

4. Apply FIR filtering to:

o Synthetic signals

o Audio signals

o Images

5. Implement DSP concepts using Python

Software Requirements

• Python 3.x

• NumPy

• SciPy

• Matplotlib

• Soundfile / [Link]

• OpenCV (for images)


Install (if needed):

pip install numpy scipy matplotlib soundfile opencv-python

Theory (Brief)

FIR Filter

An FIR (Finite Impulse Response) filter output is given by:

where:

• (x[n]) → input signal

• (h[n]) → FIR filter coefficients

• (y[n]) → filtered output

Convolution

Convolution mathematically applies the FIR filter to the signal.

A. Discrete-Time Convolution Demo

import numpy as np

import [Link] as plt

from [Link] import convolve

# Input: finite pulse

x = [Link]([[Link](10), [Link](5)])

# Filter: 3-point averager

h = [Link](3)/3

y = convolve(x, h, mode='full')

[Link](2,1,1)

[Link](x); [Link]("Input x[n]")

[Link](2,1,2)

[Link](y); [Link]("Output y[n] after 3-point averaging")


[Link]()

Experiment 1: FIR Filter with Synthetic Signal

Step 1: Generate Synthetic Signal

import numpy as np

import [Link] as plt

fs = 1000

t = [Link](0, 1, fs, endpoint=False)

x = [Link](2*[Link]*50*t) + 0.5*[Link](2*[Link]*200*t)

[Link](t, x)

[Link]("Synthetic Signal")

[Link]("Time (s)")

[Link]("Amplitude")

[Link]()

[Link]()

Step 2: Design FIR Low Pass Filter

from [Link] import firwin, freqz

cutoff = 100

numtaps = 51

lpf = firwin(numtaps, cutoff, fs=fs)

Step 3: Convolution

y = [Link](x, lpf, mode='same')

[Link](t, x, label="Original")

[Link](t, y, label="Filtered")

[Link]()

[Link]("FIR Low Pass Filtering")

[Link]()
[Link]()

Experiment 2: Frequency Response of FIR Filters

Low Pass Filter

w, h = freqz(lpf, fs=fs)

[Link](w, [Link](h))

[Link]("Low Pass FIR Frequency Response")

[Link]("Frequency (Hz)")

[Link]("Magnitude")

[Link]()

[Link]()

High Pass Filter

hpf = firwin(numtaps, cutoff, fs=fs, pass_zero=False)

w, h = freqz(hpf, fs=fs)

[Link](w, [Link](h))

[Link]("High Pass FIR Frequency Response")

[Link]()

[Link]()

Band Pass Filter

bpf = firwin(numtaps, [80, 180], fs=fs, pass_zero=False)

w, h = freqz(bpf, fs=fs)

[Link](w, [Link](h))

[Link]("Band Pass FIR Frequency Response")

[Link]()

[Link]()

B. Voice Signal Convolution (Echo)


from [Link] import wavfile

fs, voice = [Link]("voice_sample.wav")

voice = [Link](float)

# Echo filter: delay 0.2s, strength 0.9

P = int(0.2*fs)

h = [Link](P+1)

h[0] = 1

h[-1] = 0.9

y = convolve(voice, h, mode='full')

Experiment 3: FIR Filtering of Audio Signal

Read Audio File

from [Link] import wavfile

fs_audio, audio = [Link]("[Link]")

audio = audio / [Link]([Link](audio))

Apply FIR Low Pass Filter

lpf_audio = firwin(101, 3000, fs=fs_audio)

audio_filt = [Link](audio, lpf_audio, mode='same')

Plot Audio Signals

[Link](audio[:5000], label="Original")

[Link](audio_filt[:5000], label="Filtered")

[Link]()

[Link]("Audio FIR Filtering")

[Link]()

Save Filtered Audio

[Link]("filtered_audio.wav", fs_audio, audio_filt.astype(np.float32))


D. Image Convolution

from skimage import data, color

from [Link] import convolve2d

img = color.rgb2gray([Link]())

# First-difference filter (horizontal edges)

h = [Link]([[1, -1]])

y = convolve2d(img, h, mode='same')

[Link](1,2,1); [Link](img, cmap='gray'); [Link]("Original")

[Link](1,2,2); [Link](y, cmap='gray'); [Link]("Filtered (horizontal edges)")

[Link]()

Experiment 4: FIR Filtering of Image

Read Image

import cv2

img = [Link]("[Link]", cv2.IMREAD_GRAYSCALE)

2D FIR Filter (Smoothing)

kernel = [Link]((5,5)) / 25

img_filt = cv2.filter2D(img, -1, kernel)

Display Results

[Link](figsize=(8,4))

[Link](1,2,1)

[Link](img, cmap='gray')

[Link]("Original Image")

[Link]('off')

[Link](1,2,2)

[Link](img_filt, cmap='gray')
[Link]("FIR Filtered Image")

[Link]('off')

[Link]()

Lab Tasks / Assignments

1. Change filter order and observe output

2. Compare LPF, HPF, and BPF on same signal

3. Apply FIR filter to noisy audio

4. Design FIR edge detection filter for images

5. Plot magnitude and phase response

Learning Outcomes

• Practical understanding of convolution

• FIR filter design skills

• Signal and image processing using Python

• Frequency-domain analysis

You might also like