In this post I’ll cover how to remove noise in an audio file using SoX. I’ll be using a Raspberry Pi which does not have an audio input by default. To get audio, I used an simple USB audio microphone to capture audio. The OS I’m running on my Pi is Ubuntu Mate and there was no need for anything to get the microphone to run.
Once it was plugged in, I ran this command to view the properties of it. The information will be needed later when capturing audio with FFMpeg.
arecord -l card 0: Device [USB PnP Sound Device], device 1: USB Audio [USB Audio] Subdevices: 1/1 Subdevice #0: subdevice #0
I then used this command to capture the audio on the Raspberry Pi.
ffmpeg -f alsa -ac 1 -ar 44100 -i hw:0,1 -t 60 Audio_Pi.wav ### Here is a breakdown of the FFMpeg command. # -f alsa = Input format is direct from ALSA # -ac 1 = Audio Channel 1 (Mono) # -ar 44100 = Audio Sampling Rate of 44100 bits per second # -i hw:0,1 = ALSA input device hardware, Card 0, Device 1, see arecord -l results above # -t 60 = Capture for a time duration of 60 seconds # Audio_Pi.wav = The saved file that the audio data will be stored to
Something stood out when I did a spectrogram, all of the harmonic distortion. There was also a background noise across the entire spectrum. I wasn’t sure the source of the noise so I did a silenced microphone recording. Basically this was placing electric tape on the microphone opening and installing the microphone on a USB extension cable. Then I wrapped the microphone in a towel. The results still showed the hiss and harmonic distortion. Here are the commands I used to capture then cut a segment that would become the foundation of the noise profile.
ffmpeg -f alsa -ac 1 -ar 44100 -i hw:0,1 -t 60 Audio_Pi_SilencedMic.wav ffmpeg -i "Audio_Pi_SilencedMic.wav" -ss 00:00:10 -t 00:00:10 -acodec copy "Audio_Pi_SilencedMicSegment.wav"
The noise profile is created with SoX. It is a reference when applying the noise filter on an audio file. Here is the command to create the noise profile.
sox "Audio_Pi_SilencedMicSegment.wav" -n noiseprof "Audio_Pi_SilencedMicSegment.prof"
Now with the noise profile created, I then applied the SoX noise filtering command.
sox "Audio_Pi.wav" "Audio_Pi_NoNoise.wav" noisered "Audio_Pi_SilencedMicSegment.prof" 0.016
The results were impressive. But after some amplification of the results, I began to notice artifacts introduced into the audio. Still, to create simple noise filtering with command line tools on the Raspberry Pi, SoX does a decent job.
Thanks goes out to Andrew Zhang for his post, http://goodagood.blogspot.com/2017/01/audio-recording-from-usb-microphone.html. There are several posts covering noise filtering with SoX, far too many for me to give credit here, thank you. I hope you have enjoyed this and I look forward to having you back again.