Separate noise from audio with linear filter

This project shows how we can apply spectra analysis technicals to create a digital filter that filter away the unwanted frequency in audio.


Input Audio

Output Audio




By plotting the frequency spectrum of the input signal, we can identify that the noise lies at 835Hz, 1530Hz.

Since sampling frequency is 44100Hz and normalised frequency is calculated by 2* pi * frequency/ sampling frequency, the noise component lie at angular frquency 0.11(835Hz), 0.22(1530Hz). Poles and zeros should be placed at amplitude*e^(angular frequency) on the Pole Zero diagram as shown below. Amplitude of Zeros should be larger than pole in order to attenuate the signal.

The filter will then behave as follows. Normalised frquency is calculated by frequency/ sampling frequency. Therefore, we saw a dip in amplitude at normalised frequency 0.01(835Hz) and 0.03(1530Hz) in the filter amplitude response graph.

Upon applying the filter to the input signal, we can compare the difference between the input and output frequency spectrum. Noise component (835Hz, 1530Hz) no longer exist in the frequency spectrum of the output signal!

The detailed code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
[xn fs]=audioread('input.wav');

T = 1/fs; % Sampling period
L = length(xn); % Length of signal
t = (0:L-1)*T; % Time vector

Li = L
t1 = t(1:Li);
xn1 = xn(1:Li);

Y1 = fft(xn1);
P2a = abs(Y1/Li);
P1a = P2a(1:Li/2+1);
P1a(2:end-1) = 2*P1a(2:end-1);
f1 = fs*(0:(Li/2))/Li;

%in frequency domain
figure(1)
plot(f1,P1a)
title('Sample 3a Spectrum')
xlabel('f (Hz)')
ylabel('|P1(f)|')
xlim([0 5000])


% 147 220 294 349.7 440 835 1530
%Sample 3a remove noise at 835Hz, 1530Hz

f1 = 835
f2 = 1530
fs = 44100


zer = 1*exp(j*2*pi*[-f1/fs f1/fs -f2/fs f2/fs]');
pol = 0.9*exp(j*2*pi*[-f1/fs f1/fs -f2/fs f2/fs]');

zplane(zer,pol)
[b1,a1] = zp2tf(zer,pol,1);
fvtool(b1,a1)

%apply filter
x1f = filter(b1, a1, xn)


%filtered data in frequency
%L1 = round(0.2/T);
Li = L
t1 = t(1:Li);
yn1 = x1f(1:Li);

Y1 = fft(yn1);
P2a = abs(Y1/Li);
P1a = P2a(1:Li/2+1);
P1a(2:end-1) = 2*P1a(2:end-1);
f1 = fs*(0:(Li/2))/Li;

%filtered data in frequency domain
figure(2)
plot(f1,P1a)
title('Filtered Sample 3a Spectrum')
xlabel('f (Hz)')
ylabel('|P1(f)|')
xlim([0 5000])


%playing filtered sample 3a
p = audioplayer(x1f, fs);
play(p);
audiowrite('filtered_output.wav',x1f,fs)



Key Skills developed: