so in the main section of videos i've
got an example of how you can go about
filtering an audio file example i gave
an audio file it's a way you can filter
any data however now that example was
given using a lot of
syntax coming from mathematica and i
thought in case people are more familiar
with python i could run through
how you would do the same example using
python
so here's a python notebook that i'll
make available online if you want to
play with it
and we begin by importing a whole bunch
of
things from scipy and numpy that we need
so that's just the import part and now
we get to the
path involves audio files so i've got an
audio file here called noisy
noisyclaps.wave
and so i can import that of course if
you're doing this yourself the path will
vary
and i'm using this reading command here
that reads away file and the outputs are
audio in and sample rate the sample rate
is the number of samples per second
so in hertz and the audio in is just a
an array of numbers that correspond to
the amplitude of the audio
sampled at each point in the in in this
in the time space so sampled at
the sample rate so i'll read that in and
then i've got a command here to
play the audio at the correct sample
rate and we can listen to it
so we've got some clapping some shouting
and a horrible high-pitched scream that
covers the whole thing
and the goal here is to try and remove
that high-pitched scream
so to begin we need to define a whole
bunch of things that we need
for our fourier analysis of the system
we'll start by defining the nyquist
frequency which is
half of the sampling rate and we'll also
find out the number of points
of data that we have so we know we're
sampling at the sampling rate we
don't know exactly how long the data
file is
but we can find the total number of
points and that's given
by this command here so this is zero
selecting along the correct dimension of
this array
and then we know that the total number
of points must be
the sorry the total time of the
audio file must be the number of points
divided by the sampling rate
so we can define a vector of time data
using the linspace command from 0 up to
the total time
which is the number of points divided by
the sampling rate
and the number of points we have in this
vector is just the number of points
that we've got in our audio file so this
is the the time axis if you like
that goes along with the amplitude
in this array here the frequency axis
is going to be given by the um
a vector that goes from negative nice
frequency to positive nicos frequency
and it'll have the same number of points
in that
array so this is our frequency axis here
for when we take the fourier transform
this is how we take the fourier
transform we use this fft command here
of the audio import
and then we're going to call the fourier
transform data
fft audio in so it's a fast fourier
transform
of the input audio data okay
now we can plot our data in time and
frequency domain and see
how things look so this first plot here
is just the amplitude as a function of
time
the second plot here is the spectrum and
the third plot here is
a zoom up on the spectrum around 6500
hertz and we'll see why in a moment
the other command i've used here which i
should point out is a command called fft
shift now if you recall that the
[Music]
fast fourier transform produces a vector
of data in frequency space that starts
at zero goes up to the positive nicos
frequency
then in the middle of that vector it
jumps back down to the negative
micros frequency and climbs back up to
zero this fft shift
pushes all the data around so that the
zero frequency is in the center of this
data array okay
so oops forgot to do this part
okay now we can plot all of our data
so here's the time domain we can see the
claps
regular intervals this part here is the
shouting and this part here
is presumably the high pitch screen
if we look at the fourier transform
then we see two enormous peaks around
six and a half
thousand hertz at negative and positive
and if we zoom up in around positive six
and a half thousand hertz we see this
big peak here
so this is the the horrible screaming
noise
that we're going to try and eliminate
okay so the other way you can look at
this data which is really useful is to
plot a spectrogram
now spectrogram shows how the
frequencies change over time so
basically all you're doing
is carving up the time data into small
pieces and taking the fourier transform
of each of those small pieces
and then you can plot them so around
this
time here we have a clap and we can see
the clap
as you might imagine is like a delta
function it gives you a burst
of lots of frequencies fairly evenly
distributed then we have
um silence apart from this enormous
screaming noise at six and a half
thousand hertz this part here
corresponds to the person going woo
and so you can see that's this the
imprint of their voice there so here you
can see how the
frequency spectrum changes as a function
of time
this is the transform of the whole thing
this is taking transforms along
time steps so you can see how it's
varying the color scale here is in
db so it's a logarithmic scale so each
time you go up by
20 db the amplitude of the sound is
increasing by a factor of a hundred
so you can see this six and a half
thousand hertz part here is really
very intense okay so we need now is a
filter
to get rid of this so we're going to do
the simplest possible thing we'll define
a vector that is one
everywhere apart from a range of
frequencies
around plus and minus six and a half
thousand hertz so
on this graph here it'll be one
one one one one one we get to this spike
here it'll be zero
and we get over here it'll be zero but
one everywhere else
and the trick i'm going to use to define
that vector
is i'm going to define a gaussian
function so i'm using the exponential
here
i'm going to take the exponential of
the frequency data minus six and a half
thousand
and if i take the absolute value here
i'll get gaussian peaks at plus and
minus
six and a half thousand hertz the width
of those gaussian peaks
i can change depending on this factor
here
and then i can round that so if it's
a number bigger than a half it'll be
round to one less than half round to
zero
and i can subtract all of that from one
and this will give me a vector that is
one everywhere
apart from around this region here
i'm also adding a tiny little offset
here to stop
things being completely zero because
that will make some of the logarithmic
plotting a bit unhappy
so that's the filter and we can
define that filter and then
apply that filter now i'm going to show
this
on logarithmic scale here so the orange
is the filter data
and we can see around this frequency
here we've filtered everything out i'm
only
plotting the positive frequency axis
here the unfiltered data
is this blue part here so we can see
we've completely eliminated it
the filter data however isn't sort of
continuous
along here we can see we've got this big
spike going downwards
instead of just being a nice sort of uh
smooth spectrum around this area here
and this is the
cost you pay for filtering you're doing
it in a dumb kind of way you can't
directly subtract off this signal we're
just going to kill everything around
that signal it's a
you know it's a if you use weed killer
you're going to kill the weeds as long
along with some of the plants you
actually might want to keep
okay we can also look at this plot here
just for fun the real and imaginary
parts
of the frequency domain so the
fast fourier transform the blue is the
real part and
the yellow there is the imaginary part
and we can see if we zoom in there's an
area there which has been filtered to
zero
and previously that had enormous spike
on it okay
so that's how it looks now we can define
the fourier transform in the reverse
direction to get back
filtered audio data so this is the
input audio multiplied by the filter and
then we do the inverse fourier transform
ifft
to get the inverted data
now this first plot here this is the
time domain
um the real part of the time domain and
we can see
claps and shouting and we've got rid of
that big ball of fuzz that we had
if we go back up here we can see this
big bull of fuzz here so that's the real
part
now the input data of course was all
real but when we do a forward transform
filter and then reverse transform
we get little numerical errors coming
back and so it's important to remember
just to
take the real part and plot that it's
also worth checking the imaginary part
and making sure that is much much
smaller
than the real part if it's not something
might have gone wrong
so that's why i've plotted here the real
part and the imaginary part we can see
the imaginary part spans
between minus 40 and plus 40 which
sounds like a big number
but it's much much smaller than the plus
or minus
20 something thousand of the real part
so
we can basically ignore the imaginary
part from now on and just work with the
real part so having checked the
measuring part to make sure it's
small enough if it weren't small we'd
have to go back and look and see
if we've messed up the filtering somehow
okay
now we can play the audio output
and we can hear that the six and a half
thousand hertz screaming has largely
gone away
if you listen carefully
you can still hear some artifacts and
that's because when we've filtered we've
done something fairly dumb like i said
you know you use a weed killer you kill
weeds and the plants around it
and so we've got this there is an
artifact here at that frequency so we
can still hear something going on in
that frequency
um however it is much better than the
input if you remember
that sounded terrible so we've
certainly had an impact we can also plot
the
spectrogram of the output and you can
see it six and a half thousand hertz
here there's
still something going on but it's
largely been eliminated and we can hear
the rest of the audio
far more clearly