Implement Adaptive Filter for Noise Cancellation Using Raspberry Pi
This example shows how to use the Simulink® Support Package for Raspberry Pi® Hardware to implement an adaptive filter for noise cancellation using DSP System Toolbox™.
The adaptive filter for noise cancellation system uses two microphones. A primary microphone picks up the noisy input signal, while a secondary microphone receives noise that is uncorrelated to the desired input signal but is correlated to the ambient noise picked up by the primary microphone. The adaptive filter minimizes the difference between the ambient noise signal and the voice signal by altering their filter coefficients. Over time, the output signal from the adaptive filter closely approximates the signal you want to reproduce. For more information, see Noise Cancellation in Simulink Using Normalized LMS Adaptive Filter (DSP System Toolbox).
Prerequisites
For more information on how to use Simulink Support Package for Raspberry Pi Hardware to run a Simulink model on Raspberry Pi hardware, see Get Started with Simulink Support Package for Raspberry Pi Hardware.
For more information on how to process audio signals using Raspberry Pi hardware, complete these examples.
- Get Started with Audio Signal Processing Using Raspberry Pi
- Implement Bandpass Filter on Raspberry Pi
Required Hardware
Raspberry Pi hardware board
Two USB microphones
Ethernet cable
Two mobile phones or two sources of audio that play the audio in WAV format
Headphone – USB type or 3.5mm jack
Hardware Setup
In the Hardware Setup dialog box, in the Network Settings screen, select
Connect directly to host computer
to avoid any network latency.Connect both the USB microphones to the Raspberry Pi hardware board.
Place one microphone near the audio source playing the desired audio signal and place the other microphone near the audio source playing the ambient noise signal. Ensure to maintain enough distance between the two microphones so that the ambient noise source creates an interference in the desired audio signal.
Configure Simulink Model and Calibrate Parameters
Open the raspberrypi_audio_adaptiveFilter
Simulink model.
Simulation Sources
The Acoustic Environment subsystem replicates a noisy environmental scenario for simulation. In this subsystem, you can use the Random Source block to generate a Gaussian white noise. To generate an audio signal, use the Manual Switch to select from either the sine wave or play an audio signal using the Audio File Read block. The default position of the switch is the Sine Wave block. The Slider is connected to the frequency of the Sine Wave. The output from the noise source and the audio signal is summed and is one of the inputs to the Voice Signal Source in the Design your algorithm area.
Configure these parameters in the block parameters dialog box of the Random Source (DSP System Toolbox) block.
Set Source type to
Gaussian
.Set Method to
Sum of uniform values
.Set Number of uniform values to sum to
5
.Set Repeatability to
Specify seed
.Set Initial seed to
[23341]
.Set Sample time to
1/8000
.Set Samples per frame to
800
.Set Output data type to
Single
.
The int16 output from the Audio File Read block is converted to single datatype in the Normalized int16 to single subsystem. Double-click the Normalized int16 to single subsystem to open it.
Raspberry Pi Sensors and Inputs
You can use the ALSA Audio Capture blocks to capture the noise and the desired audio signals. Configure these parameters in the Ambient Noise Mic ALSA Audio Capture Block Parameters dialog box.
1. Enter the Device name of the USB microphone interfaced with the Raspberry Pi hardware. To find your device name, execute these commands in the MATLAB® Command Window.
a. Establish a connection with the Raspberry Pi hardware.
r = raspberrypi('Raspberry Pi IP address','Username','Password');
b. List input devices connected to the Raspberry Pi hardware.
a = r.listAudioDevices('capture')
a =
struct with fields: Name: 'USB-Audio-USBPnPSoundDevice' Device: '3,0' Channels: {'1'} BitDepth: {'16-bit integer'} SamplingRate: {'44100' '48000'}
The Device field of the a
output is the device identifier.
Use this format to enter the Device name.
'plughw:<Device ID>'
For example, for the Device property 3,0, enter 'plughw:3,0'.
The plug plugin automatically converts the rate, format, and channel specified in the Block Parameters dialog box of the ALSA Audio Capture block and makes them compatible with the output of the audio device.
2. Specify the data type in which the audio device reads or sends data by selecting a value from the Device Bit Depth drop-down.
3. Enter the Number of Channel (C) supported for the audio device.
4. Set Audio sampling frequency to 8000
.
5. Set Samples per frame to 800
.
Similarly, configure the parameters for the Voice Signal Mic ALSA Audio Capture Block Parameters dialog box.
Design your algorithm here
You can either enable or disable the Normalized LMS block using the Slider Switch. By default, the block is enabled.
The ambient noise and desired audio signals are two inputs to the Normalized LMS block. The Input port of the LMS Filter block receives only the noise signal input while the Desired port of the block receives a summation of the noise and the audio signal. The LMS Filter block adapts the filter weights based on the normalized LMS algorithm for filtering the input signal. Configure these parameters in the LMS Filter (DSP System Toolbox) block.
Set Algorithm to
Normalized LMS
.Set Filter length to
40
.Set Specify step size via to
Input port
.Set Leakage factor to
1.0
.Set Initial value of filter weights to
0
.Select Adapt port.
Set Reset port to
Non-zero sample
.Clear Output filter weights.
The step size indicates how fast the adaptive filter changes its coefficients to produce a desirable output. You can set the Step-size of the LMS Filter block using the drop-down. The default value is Slow Adapt (0.002)
.
Press the Reset Push Button to reset the filter coefficient.
Display and Visualization
In the Display and Visualization area, you can view the Error signal output from the adaptive filter and view them on the Time Scope (DSP System Toolbox) block.
Raspberry Pi Actuators and Outputs
The Error output from the Normalized LMS block is converted from single to int16 datatype in the single to int16 subsystem.
Configure these parameters in the Block Parameters dialog box of the ALSA Audio Playback block.
Enter the Device name of the headphone interfaced with the Raspberry Pi hardware. Use the
listAudioDevices
object to obtain the device name.Set Audio sampling frequency to
8000
.
Run Simulink Model
1. In the Design your algorithm here area, position the Ambient Noise Source and Voice Signal Sources Manual Switches to receive the simulation output.
2. On the Simulation tab of the model, click Run.
3. Open the Time Scope (DSP System Toolbox) block and observe the output. Initially, the signal is all distorted due to the presence of noise in the desired audio signal. After some time, the noise level reduces. You can also press the Reset button on the Simulink model and observe how the noise levels in the audio signal reduce.
Deploy Simulink Model
In the Design your algorithm here area, position the Ambient Noise Source and Voice Signal Sources Manual Switches to receive output from the Raspberry Pi Actuators and Sensors area.
On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Monitor & Tune.
Open the Time Scope (DSP System Toolbox) block and observe the output on the Time Scope.
Other Things to Try
Change the distance between the two microphones and observe the results on the Time Scope.
From the drop-down in the Design your algorithm here area, select the
Medium Adapt
andFast Adapt
options and observe the audio signal output on the Time Scope.