Adaptive Filter Application by harrde

From Class Wiki
Jump to navigation Jump to search

Back to my page

Problem Statement

Come up with your own application and do a simulation in MATLAB.

Solution

Suppose that you want to record bird calls so that you can listen to them later and learn to identify birds only by their call. The problem is that you live by a huge freeway and all the noise from the cars and trucks makes it hard for you to get a good recording. The MATLAB code below can take a recorded bird call and remove the noise. This lets you get good bird call recordings.

% Bird call recording simulation where we filter out noise
clear all

% This is the bird call that we want to record.
load chirp;
s = y';
Ls = length(y);

% Creating noise
T = 1/Fs;
t = 0:T:3;
noise = sin(2*pi*100*t)+cos(2*pi*200*t);
n = noise;

bn = 3*[0 0 0 0 0 0 0 0 0 0 0 0 0 0 .5 1 .5]; %This amplifies noise
an = [1];
nf = filter(bn,an,n); 

y = s(1:length(s)) + n(1:length(s)); %Here is the noise plus bird call

wavplay(y,Fs,'sync') %Here's what the bird call plus noise sounds like

N = 20;         % Length of adaptive filter

% LMS algorithm for adaptive noise cancellation
h = zeros(N,1);
mu = 1/(10*N*var(n));

for k=N:Ls
	xk = n(k:-1:(k-N+1));
	nhat(k) = h'*xk';
	e(k) = - y(k) + nhat(k);
	h = h - mu*e(k)*xk';  
end

% Here is the cleaned signal with beautiful bird call;
wavplay(e,Fs)

Run this code on a computer with speakers or headphones. First you will hear the original recorded bird call with all the noise. Then you will hear the bird call after the noise has been filtered out by an adaptive filter.