Prelab 2 - Notch Filter Design

Summary

You will investigate the basics of audio processing and filter design in Python.

Downloads

Download the with_hum.wav and without_hum.wav file to your computer.

Submission Instruction

Refer to the Submission Instruction page for more information.

Part 1 - Jupyter Notebook

We will be using Jupyter Notebook for all of our prelabs (or labs if you prefer). Jupyter Notebook is embedded in the anaconda python distribution, it is popular because it is based on interactive python that supports inline plotting and Markdown/Latex documentation. You can display intermediate results easily for quickly prototyping your ideas and you can also store them without the need to re-run everything.

Create New Notebook

From the command line (or your windows start menu), type:

1
jupyter notebook

to open the jupyter notebook in your browser:

Inside the just-opened webpage, Choose New -> Python[default]. A jupyter notebook will be opened in a new tab.

Go to File->Rename and rename it to Prelab 2.

In the first cell in the opened notebook, type ls and press Shift + Enter(shortcut to run the selected cell). It should list all the file in your current directory.

Make sure that you are in the same folder where the wav files are stored. Run the following commands to find out your current working directory.

1
2
import os
print(os.getcwd())

Load audio files

To import the wave file and read both sampling rate and raw data, you might use:

1
2
from scipy.io.wavfile import read
sampling_rate, data = read('with_hum.wav')

And to play the audio file:

1
2
from IPython.display import Audio
Audio('with_hum.wav')

A small control panel should appear below the cell, click on the button to play the audio. Play both wav files to see the difference.

Inline Plotting

As in Lab 1, we use matplotlib.pyplot module to display the figure in Jupyter Notebook.

You may need to include this line at the beginning of the notebook to make the plot visible in the notebook:

1
%matplotlib inline

Part 2 - Frequency Response

One of the basic approach to analyse some signal is to convert it to frequency domain and analyse its frequency response. Apply fourier transform to convert a signal to frequency domain.

Assignment 1

Plot the magnitude of the frequency response of the signal with the 400 Hz humming (with_hum.wav).

Hint

Use numpy.fft.fft() function, examples are available at the bottom of the linked page.

Part 3 - Notch Filter Design

We want to filter out the 400 Hz humming interference out from the with_hum.wav so we want to design a bandstop filter that will surpress signals around that frequency.

Assignment 2

Design a bandstop filter to filter out the humming interference. Plot the magnitude and phase of the frequency response of the filter.

Hint

Use scipy.signal.butter() and scipy.signal.freqz() functions to design and plot the filter response.

Attention

All of our filters and any further processing will be in the digital domain, not analog domain.

Question

Brifely discribe your filter design. Why does your filter have the number of taps that it has? Could you achieve the same effect with less taps? What are the practical effects of using less taps?

Part 4 - Apply Filtering

Filter the 400 Hz humming interference out from the with_hum.wav using your bandstop filter from Exercise 2. Play the output audio and you should hear the humming interference died out.

Assignment 3

Plot the frequency response of the filtered signal. Play the filtered signal again.

Hint

Use scipy.signal.lfilter() function to apply filtering.

Grading

Prelab 2 will be graded as follows:

  • Assignment 1 [0.5 point]

    A plot of the magnitude of the frequency response of the original signal (with the 400 Hz humming) [0.5 point]

  • Assignment 2 [1 point]

    A plot of the magnitude and phase of your bandstop filter's frequency response [0.5 point]

    Short answer question [0.5 point]

  • Assignmet 3 [0.5 point]

    A plot of the magnitude of the frequency response of the filtered signal (with the 400 Hz humming canceled out) [0.5 point]