I've only used MATLAB as a calculator, so I'm not as well versed in the program. I hope a kind person may be able to guide me on the way since Google currently is not my friend.
I have a wav
file in the link below, where there is a human voice and some noise in the background. I want the noise removed. Is there anyone who can tell me how to do it in MATLAB?
This is a pretty imperfect solution, especially since some of the noise is embedded in the same frequency range as the voice you hear on the file, but here goes nothing. What I was talking about with regards to the frequency spectrum is that if you hear the sound, the background noise has a very low hum. This resides in the low frequency range of the spectrum, whereas the voice has a more higher frequency. As such, we can apply a bandpass filter to get rid of the low noise, capture most of the voice, and any noisy frequencies on the higher side will get cancelled as well.
Here are the steps that I did:
audioread
.audioplayer
object.audioplayer
object.Let's go then!
Step #1
audioread
will read in an audio file for you. Just specify what file you want within the''
. Also, make sure you set your working directory to be where this file is being stored.clearvars, close all
just do clean up for us. It closes all of our windows (if any are open), and clears all of our variables in the MATLAB workspace.f
would be the signal read into MATLAB whilefs
is the sampling frequency of your signal.f
here is a 2D matrix. The first column is the left channel while the second is the right channel. In general, the total number of channels in your audio file is denoted by the total number of columns in this matrix read in throughaudioread
.Step #2
This step will allow you to create an
audioplayer
object that takes the signal you read in (f
), with the sampling frequencyfs
and outputs an object stored inpOrig
. You then usepOrig.play
to play the file in MATLAB so you can hear it.Step #3
stem
is a way to plot discrete points in MATLAB. Each point in time has a circle drawn at the point with a vertical line drawn from the horizontal axis to that point in time.subplot
is a way to place multiple figures in the same window. I won't get into it here, but you can read about howsubplot
works in detail by referencing this StackOverflow post I wrote here. The above code produces the plot shown below:The above code is quite straight forward. I'm just plotting each channel individually in each subplot.
Step #4
The code that will look the most frightening is the code above. If you recall from signals and systems, the maximum frequency that is represented in our signal is the sampling frequency divided by 2. This is called the Nyquist frequency. The sampling frequency of your audio file is 48000 Hz, which means that the maximum frequency represented in your audio file is 24000 Hz.
fft
stands for Fast Fourier Transform. Think of it as a very efficient way of computing the Fourier Transform. The traditional formula requires that you perform multiple summations for each element in your output. The FFT will compute this efficiently by requiring far less operations and still give you the same result.We are using
fft
to take a look at the frequency spectrum of our signal. You callfft
by specifying the input signal you want as the first parameter, followed by how many points you want to evaluate at with the second parameter. It is customary that you specify the number of points in your FFT to be the length of the signal. I do this by checking to see how many rows we have in our sound matrix. When you plot the frequency spectrum, I just took one channel to make things simple as the other channel is the same. This serves as the first input intofft
. Also, bear in mind that I divided byN
as it is the proper way of normalizing the signal. However, because we just want to take a snapshot of what the frequency domain looks like, you don't really need to do this. However, if you're planning on using it to compute something later, then you definitely need to.I wrote some additional code as the spectrum by default is uncentered. I used
fftshift
so that the centre maps to 0 Hz, while the left spans from 0 to -24000Hz while the right spans from 0 to 24000 Hz. This is intuitively how I see the frequency spectrum. You can think of negative frequencies as frequencies that propagate in the opposite direction. Ideally, the frequency distribution for a negative frequency should equal the positive frequency. When you plot the frequency spectrum, it tells you how much contribution that frequency has to the output. That is defined by the magnitude of the signal. You find this by taking theabs
function. The output that you get is shown below.If you look at the plot, there are a lot of spikes around the low frequency range. This corresponds to your humming whereas the voice probably maps to the higher frequency range and there isn't that much of it as there isn't that much of a voice heard.
Step #5
By trial and error and looking at Step #5, I figured everything from 700 Hz and down corresponds to the humming noise while the higher noise contributions go from 12000 Hz and higher.
Step #6
You can use the
butter
function from the Signal Processing Toolbox to help you design a bandpass filter. However, if you don't have this toolbox, refer to this StackOverflow post on how user-made function that achieves the same thing. However, the order for that filter is only 2. Assuming you have thebutter
function available, you need to figure out what order you want your filter. The higher the order, the more work it'll do. I choosen = 7
to start off. You also need to normalize your frequencies so that the Nyquist frequency maps to 1, while everything else maps between 0 and 1. Once you do that, you can callbutter
like so:The
bandpass
flag means you want to design a bandpass filter,beginFreq
andendFreq
map to the normalized beginning and ending frequency you want to for the bandpass filter. In our case, that'sbeginFreq = 700 / Nyquist
andendFreq = 12000 / Nyquist
.b,a
are the coefficients used for a filter that will help you perform this task. You'll need these for the next step.Step #7
You use
filter
to filter your signal using what you got from Step #6.fOut
will be your filtered signal. If you want to hear it played, you can construct andaudioplayer
based on this output signal at the same sampling frequency as the input. You then usep.play
to hear it in MATLAB.Give this all a try and see how it all works. You'll probably need to play around the most in Step #6 and #7. This isn't a perfect solution, but enough to get you started I hope.
Good luck!