Audio recordings sometimes have 60Hz hum noise, caused by leaving the microphone cable to close to a power line. This MP will explore methods for getting rid of the hum noise, especially the low-frequency harmonics. This MP was originally designed by Prof. Doug Jones at the University of Illinois.

In [1]:

```
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.figure
import wave
%matplotlib inline
```

In [2]:

```
import importlib
import submitted
importlib.reload(submitted)
```

Out[2]:

<module 'submitted' from '/Users/jhasegaw/Dropbox/mark/teaching/ece401/ece401labs/21fall/mp6/src/submitted.py'>

This MP will explore one audio recording in detail, so that you can see where the low-frequency peaks are. The main peak, at 120Hz, could easily be filtered out from a large number of audio files, all at once.

In [3]:

```
with wave.open('input.wav','rb') as w:
fs = w.getframerate()
nsamps = w.getnframes()
x = np.frombuffer(w.readframes(nsamps),dtype=np.int16).astype('float32')
```

In [4]:

```
t = np.linspace(0,nsamps/fs,nsamps)
fig=plt.figure(figsize=(14,4))
ax=fig.subplots()
ax.plot(t[(0.5<t)&(t<1.1)],x[(0.5<t)&(t<1.1)])
```

Out[4]:

[<matplotlib.lines.Line2D at 0x7fd39e58e5e0>]

Notice, in the waveform above, there is a periodic component in the "silence" before and after the speech begins. There seem to be five or six periods in 0.05sec, so maybe the period is around 0.01sec, or around 100Hz. We can listen to it using the following code:

In [5]:

```
import IPython.display as ipd
ipd.Audio(x, rate=fs)
```

Out[5]:

Rather than printing every plot using separate blocks for both the distributed solution and your solution, instead, each of the following blocks will include lines to plot either one or the other. In order to switch between your solution and the distributed solution, you'll need to comment out the other one.

In [6]:

```
import h5py
solutions = h5py.File('solutions.hdf5','r')
```

First, let's plot a spectrum, and try to find the frequencies of the noise components.

In [7]:

```
importlib.reload(submitted)
help(submitted.todo_spectrum)
```

In [8]:

```
importlib.reload(submitted)
spec=submitted.todo_spectrum(x)
#spec = solutions['spec'][:]
f = np.linspace(0,fs,nsamps,endpoint=False)
fig=plt.figure(figsize=(14,4))
ax=fig.subplots()
ax.plot(f[f<500],spec[f<500])
```

Out[8]:

[<matplotlib.lines.Line2D at 0x7fd3a04ab970>]

- It looks like there's a huge power-line noise component at about 120Hz.
- There doesn't seem to be any 60Hz component; the recording device seems to have filtered out all energy below about 80Hz.
- There is also energy at about 180Hz, which might be the third harmonic of the power line noise, but it might also be the pitch of a female talker. Let's leave that energy in the signal.
- Instead, let's eliminate three of the other small peaks that occur between 90Hz and 120Hz.
- We'll use the "todo_findpeak" function to find the frequencies of those peaks (if you've written it).

In [9]:

```
importlib.reload(submitted)
help(submitted.todo_findpeak)
```

In [10]:

```
importlib.reload(submitted)
noise1 = submitted.todo_findpeak(spec, fs, 0, 100)
noise2 = submitted.todo_findpeak(spec, fs, noise1+1, 100)
noise3 = submitted.todo_findpeak(spec, fs, 100, 110)
noise4 = submitted.todo_findpeak(spec, fs, 100, 150)
freqs = np.array([noise1,noise2,noise3,noise4])
#freqs = solutions['freqs'][:]
print(freqs)
```

[ 90.28444206 96.42624084 103.18221949 119.7650762 ]

We'll create four notch filters: one for each of the frequencies we want to eliminate. For each notch filter, we need to create zeros and poles for the Z transform, then convert them to filter coefficients.

In [11]:

```
importlib.reload(submitted)
help(submitted.todo_zeros)
```

In [12]:

```
importlib.reload(submitted)
z = submitted.todo_zeros(freqs,fs)
#z = solutions['z'][:]
print(z)
```

In [13]:

```
importlib.reload(submitted)
help(submitted.todo_poles)
```

In [14]:

```
importlib.reload(submitted)
p = submitted.todo_poles(z,20,fs)
#p = solutions['p'][:]
np.log(np.abs(p))
```

Out[14]:

array([[-0.00392699, -0.00392699, -0.00392699, -0.00392699], [-0.00392699, -0.00392699, -0.00392699, -0.00392699]])

In [15]:

```
def pole_zero_plot(z,p):
fig=plt.figure(figsize=(10,8))
ax=fig.subplots()
ax.plot([0,1e-6],[-2,2],'k-',[-2,2],[0,0],'k-') # plot the axes
omega = np.linspace(0,2*np.pi,200)
ax.plot(np.cos(omega),np.sin(omega),'k-') # plot the unit circle
ax.set_aspect('equal') # make it look like a circle
zeromarker = matplotlib.markers.MarkerStyle(marker='o',fillstyle='none')
polemarker = matplotlib.markers.MarkerStyle(marker='x',fillstyle='none')
ax.scatter(x=np.real(z),y=np.imag(z),s=40,marker=zeromarker)
ax.scatter(x=np.real(p),y=np.imag(p),s=40,marker=polemarker)
return(ax)
ax=pole_zero_plot(z,p)
```

At that scale, the poles and zeros are all right on top of each other! Let's try to zoom in.

In [16]:

```
ax=pole_zero_plot(z,p)
ax.set_xlim([0.95,1.05])
ax.set_ylim([-0.05,0.05])
```

Out[16]:

(-0.05, 0.05)

Finally, let's create the filter coefficients. There are four filters, each of which has feedforward coefficients (b[:,k]) and feedback coefficients (a[:,k]).

In [17]:

```
importlib.reload(submitted)
help(submitted.todo_coefficients)
```

In [18]:

```
importlib.reload(submitted)
a = submitted.todo_coefficients(p)
#a = solutions['a'][:]
b = submitted.todo_coefficients(z)
#b = solutions['b'][:]
for whichfreq in range(4):
print('For frequency',whichfreq,'numerator coefficients are',b[:,whichfreq])
print(' and denominator coefficients are',a[:,whichfreq])
```

In [19]:

```
fig = plt.figure(figsize=(10,8))
axs=fig.subplots(len(freqs),2)
for row in range(len(freqs)):
axs[row,0].stem(b[:,row],use_line_collection=True)
axs[row,0].set_title('Feedforward coefficients, filter %d'%(row))
axs[row,1].stem(a[:,row],use_line_collection=True)
axs[row,1].set_title('Feedback coefficients, filter %d'%(row))
```

Let's plot the frequency response that we would get by running all four filters, one after the other.

In [20]:

```
importlib.reload(submitted)
help(submitted.todo_freqresponse)
```

In [21]:

```
importlib.reload(submitted)
H = np.ones(nsamps,dtype='complex')
for k in range(len(freqs)):
omega, Hnew = submitted.todo_freqresponse(a[:,k],b[:,k],nsamps)
H *= Hnew
#H = solutions['H'][:]
fig=plt.figure(figsize=(14,4))
ax=fig.subplots()
plt.plot(f[f<400],np.abs(H[f<400]))
```

Out[21]:

[<matplotlib.lines.Line2D at 0x7fd3a15758e0>]

OK, now let's actually run all four filters, one after the other, and take a look at the resulting spectrum and signal, to see if the noise has been removed.

In [22]:

```
importlib.reload(submitted)
help(submitted.todo_filter)
```

In [23]:

```
importlib.reload(submitted)
y = x
for k in range(len(freqs)):
y = submitted.todo_filter(y,a[:,k],b[:,k])
#y = solutions['y']
print('Last five samples of y are',y[-6:-1])
```

Last five samples of y are [-373.09300398 -357.83001544 -342.58330979 -323.41297782 -304.5079235 ]

In [24]:

```
importlib.reload(submitted)
yspec=submitted.todo_spectrum(y)
fig=plt.figure(figsize=(14,4))
ax=fig.subplots()
ax.plot(f[f<500],yspec[f<500])
```

Out[24]:

[<matplotlib.lines.Line2D at 0x7fd3a153d850>]

In [25]:

```
t = np.linspace(0,nsamps/fs,nsamps)
fig=plt.figure(figsize=(14,4))
ax=fig.subplots()
ax.plot(t[(0.5<t)&(t<1.1)],y[(0.5<t)&(t<1.1)])
```

Out[25]:

[<matplotlib.lines.Line2D at 0x7fd3a0c47580>]

That's all! This is the basic technology behind utilities like `noiseremove,`

in case you don't feel like doing it all by hand next time.

In [ ]:

```
```