Main Content

Implement Bandpass Filter on Raspberry Pi

This example shows how to use the Simulink® Support Package for Raspberry Pi™ Hardware to design a bandpass filter using DSP System Toolbox™.

Prerequisites

Required Hardware

  • Raspberry Pi hardware board

  • USB microphone

  • Ethernet cable

  • Mobile phone or any source of audio that plays the audio in WAV format

Hardware Setup

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

  2. Connect a USB microphone to the Raspberry Pi hardware board.

Configure Simulink Model and Calibrate Parameters

This example uses a preconfigured Simulink model from the Simulink Support Package for Raspberry Pi Hardware.

To open the Simulink model, run this command in the MATLAB® Command Window.

open_system('raspberrypi_audio_digitalFilter')

In simulation mode, you can use a Slider to adjust the frequency of the Sine Wave (DSP System Toolbox) from 1000 Hz to 8000 Hz. In code generation mode, you can deploy the Simulink model on your Raspberry Pi hardware and interface a microphone to generate the acoustic sine waves.

Simulation Sources

The Slider dashboard controls the frequency parameter of the Sine Wave block. Configure these parameters in the Slider Block Parameters dialog box.

  1. Connect the Slider to the Sine Wave Frequency parameter.

  2. Set the Maximum and Minimum parameters to 8000 and 1000, respectively.

Configure these parameters in the Sine Wave (DSP System Toolbox) Block Parameters dialog box.

  1. Set Frequency (Hz) to 1000.

  2. Set Sample time to 1/44100.

  3. Set Samples per frame to 4410. This value is usually the 1/10 of the value set in the Sample time parameter.

Raspberry Pi Sensors and Inputs

You can use the ALSA Audio Capture to interface a microphone with your Raspberry Pi hardware board to capture the audio signal. Configure these parameters in the 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: '2,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, if the Device field is 2,0, enter 'plughw:2,0'.

The plug plugin converts the rate, format, and channel specified in the ALSA Audio Capture Block Parameters dialog box 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. From the listAudioDevices function, you can select the parameter value specified in the BitDepth field.

3. Enter the Number of Channel (C) supported for the audio device. From the listAudioDevices function, you can select the parameter value specified in the Channels field.

Algorithm

The default position of the Manual Switch is on the sine wave data received from the Simulation Sources area.

Design a bandpass filter using the Digital Filter Design (DSP System Toolbox) block. This filter has an order of 260 and allows a range of frequencies to pass above 3500 Hz and below 5500 Hz.

Configure these parameters in the Digital Filter Design (DSP System Toolbox) Block Parameters dialog box.

1. In the Response section, select Bandpass.

2. In the Frequency Specifications section, set these parameters.

a. Units to Hz

b. Fs to 44100

c. Fstop1 to 3000

d. Fpass1 to 3500

e. Fpass2 to 5500

f. Fstop2 to 6000

3. In the Magnitude Specifications section, set these parameters.

a. Astop1 to 95

b. Apass to 1

c. Astop2 to 95

The Current Filter Information section shows the details for the structure, order, stability, and source for the designed filter. The Magnitude Response (dB) section shows the filter corresponding to the above specifications.

The Subsystem block in the Algorithm area receives the Filtered and the Original frequencies as inputs. This subsystem compares the amplitudes of the Original and Filtered signals and outputs a logical 1 for frequencies that lie within the bandpass range.

To open the subsystem, run this command in the MATLAB Command Window.

open_system('raspberrypi_audio_digitalFilter/Subsystem')

Display and Visualization

In the Display and Visualization area, you can view the original signal and the filtered signal on the time scope and spectrum analyzer.

Raspberry Pi Actuators and Outputs

The output of the Subsystem block in the Algorithm area is an input to the LED block in the Raspberry Pi Actuators and Outputs area. The Lamp is also connected to the input received from the LED. The lamp glows if the relative magnitude of the filtered signal is greater than 0.75, which occurs for signals with a frequency lying in the bandpass filter frequency range of the filter.

Run Simulink Model with Pacing

1. Position the Manual Switch in the Simulation Sources area to receive the simulation output from the Sine Wave (DSP System Toolbox) block.

2. On the Simulation tab of the Simulink model, select Run > Simulation Pacing.

3. In the Simulation Pacing Options dialog, select Enable pacing to slow down simulation. When you select this option, the specified pace automatically applies to the simulation.

4. Select the pace at which the model runs by using the slider or entering the pace in the Simulation time per wall clock second field.

5. Click Run.

6. Change the frequency of the Sine Wave by moving the Slider and observe the Spectrum Analyzer and Time Scope.

a. Set the frequency of the Sine Wave (DSP System Toolbox) to value below 3000 Hz, for example, 2400 Hz. Observe the output of the Spectrum Analyzer Original, Time Scope, and Spectrum Analyzer Filtered blocks. The signal does not pass through the bandpass filter. No output is visible in second port of the Time Scope block. The Lamp does not glow.

b. Set the frequency of the Sine Wave (DSP System Toolbox) to any frequency above 5500 Hz, for example, 6500 Hz. Observe the output of the Spectrum Analyzer Original, Time Scope, and Spectrum Analyzer Filtered blocks. The signal does not pass through the bandpass filter. No output is visible is in second port of the Time Scope block. The Lamp does not glow.

c. Set the frequency of the Sine Wave (DSP System Toolbox) to any frequency between 3000 Hz and 5500 Hz, for example, 4000 Hz. Observe the output of the Spectrum Analyzer Original, Time Scope, and Spectrum Analyzer Filtered blocks. The signal passes through the bandpass filter and the Lamp glows.

Simulink Model in Simulation Mode

Spectrum Analyzer Original Output

Spectrum Analyzer Filtered Output

Observe the peak at 4000 Hz.

Time Scope Output

Generate WAV Files Using createAudio Function

Generate three audio signals at 1000 Hz, 4000 Hz, and 8000 Hz using the createAudio function. For more information on the createAudio function, see the Generate .wav File Using createAudio Function section in the Get Started with Audio Signal Processing Using Raspberry Pi example.

1. Create an audio signal with a frequency of 1000 Hz, signal duration of 60 seconds, and sampling frequency of 44100 Hz.

createAudio(1000,1000,60,44100)

The createAudio function generates a WAV file in the current directory of the example with the name 1000Hz_1000Hz_60sec.wav.

2. Create an audio signal with a frequency of 4000 Hz, signal duration of 60 seconds, and sampling frequency of 44100 Hz.

createAudio(4000,4000,60,44100)

The createAudio function generates a WAV file in the current directory of the example with the name 4000Hz_4000Hz_60sec.wav.

3. Create an audio signal with a frequency of 8000 Hz, signal duration of 60 seconds, and sampling frequency of 44100 Hz.

createAudio(8000,8000,60,44100)

The createAudio function generates a WAV file in the current directory of the example with the name 8000Hz_8000Hz_60sec.wav.

Transfer the 1000Hz_1000Hz_60sec.wav, 4000Hz_4000Hz_60sec.wav, and 8000Hz_8000Hz_60sec.wav files to your mobile phone.

Deploy Simulink Model on Raspberry Pi Hardware

1. Position the Manual Switch in the Algorithm area to receive the output from the ALSA Audio Capture block in the Rapsberry Pi Sensors and Inputs area.

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

3. Play the 1000Hz_1000Hz_60sec.wav file on your mobile phone and place it close to the USB microphone interfaced with the Raspberry Pi hardware board.

You can observe this audio signal on the Spectrum Analyzer.

4. Observe the output on the Spectrum Analyzer and Time Scope blocks.

Spectrum Analyzer Filtered Output

The signal is attenuated and not passed through the bandpass filter and the lamp does not glow.

Time Scope Output

5. Play the 4000Hz_4000Hz_60sec.wav file on your mobile phone and place it close to the USB microphone interfaced with the Raspberry Pi hardware board.

You can observe this audio signal on the Spectrum Analyzer.

6. Observe the output on the Spectrum Analyzer and Time Scope blocks.

Spectrum Analyzer Filtered Output

The signal passes through the bandpass filter and a peak is present at 4000 Hz and the lamp glows.

Time Scope Output

7. Play the 8000Hz_8000Hz_60sec.wav file on your mobile phone and place it close to the USB microphone interfaced with the Raspberry Pi hardware board.

Observe the audio signal on the Spectrum Analyzer.

8. Observe the output on the Spectrum Analyzer and Time Scope blocks.

Spectrum Analyzer Filtered Output

The signal is attenuated and not passed through the bandpass filter and the lamp does not glow.

Time Scope Output

Other Things to Try

  • Design a filter other than a bandpass filter using the Digital Filter Design (DSP System Toolbox) block.

  • Design a filter with different stop and pass frequencies for the bandpass filter.

  • Change the order of the bandpass filter and observe the output on spectrum analyzer and time scope for different input frequencies.

See Also