Main Content

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

- 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

  1. In the Hardware Setup dialog box, in the Network Settings screen, select Connect directly to host computer to avoid any network latency.

  2. Connect both the USB microphones to the Raspberry Pi hardware board.

  3. 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.

  1. Set Source type to Gaussian.

  2. Set Method to Sum of uniform values.

  3. Set Number of uniform values to sum to 5.

  4. Set Repeatability to Specify seed.

  5. Set Initial seed to [23341].

  6. Set Sample time to 1/8000.

  7. Set Samples per frame to 800.

  8. 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 = listAudioDevices(r,'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.

  1. Set Algorithm to Normalized LMS.

  2. Set Filter length to 40.

  3. Set Specify step size via to Input port.

  4. Set Leakage factor to 1.0.

  5. Set Initial value of filter weights to 0.

  6. Select Adapt port.

  7. Set Reset port to Non-zero sample.

  8. 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.

  1. Enter the Device name of the headphone interfaced with the Raspberry Pi hardware. Use the listAudioDevices function to obtain the device name.

  2. 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

  1. 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.

  2. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Monitor & Tune.

  3. 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 and Fast Adapt options and observe the audio signal output on the Time Scope.

See Also