Main Content

Get Started with Audio Signal Processing Using Raspberry Pi

This example shows how to use the Simulink® Support Package for Raspberry Pi® Hardware to get started with audio signal processing applications on the Raspberry Pi hardware. It also shows how to:

  • Use simulation pacing to slow down a simulation to understand and observe the system behavior. For more information, refer to Simulation Pacing Options.

  • Use a Spectrum Analyzer and a Time Scope (DSP System Toolbox) to observe and study the audio signals in the frequency and time domains.

  • Study the significance of sampling frequency and Nyquist criterion.

Prerequisites

For more information on how to use the 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.

Required Hardware

  • Raspberry Pi hardware board

  • USB microphone

  • Ethernet cable

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

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 a USB microphone to the Raspberry Pi hardware board.

Configure Simulink Model and Calibrate Parameters

Open the raspberrypi_audio_gettingStarted Simulink model.

This example uses the Audio template that can be found under Simulink > Simulink Start Page > Simulink Support Package for Raspberry Pi Hardware.

In the simulation mode, you can use the Slider for adjusting frequency of the Sine Wave (DSP System Toolbox) from 500 Hz to 1000 Hz. In the code generation mode, you can deploy the Simulink model on your Raspberry Pi hardware and interface a microphone to capture acoustic sine waves from the mobile phone or any other source of audio.

Raspberry Pi Sensors and Inputs

Configure these parameters in the Block Parameters dialog box of the ALSA Audio Capture block.

1. Enter the Device name of the USB microphone interfaced with the Raspberry Pi hardware. To know 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: '2,0'
      Channels: {'1'}
      BitDepth: {'16-bit integer'}
  SamplingRate: {'44100'  '48000'}

The device ID is listed in the Device property of the function.

Use this format to enter the Device name.

'plughw:<Device ID>'

For example, for the Device property 2,0, enter 'plughw:2,0'.

The plug plugin automatically converts the rate, format, and channel specified in the ALSA Audio Capture Block Parameters dialog box and makes it compatible with the output of the audio device.

2. Select Device Bit Depth of the audio data that refers to the data type in which the audio device reads or sends data. From the listAudioDevices object, you can select the parameter specified in the BitDepth property.

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

Simulation Sources

Configure these parameters in the Block Parameters dialog box of the Slider block.

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

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

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

  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/10th of the value set in the Sample time parameter.

Algorithm

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

Configure this parameter in the Block Parameters dialog box of the Data Type Conversion block.

  1. Set Output data type to single.

Run Simulink Model with Pacing

Simulation pacing enables you to slow down a simulation to understand and observe the audio signal at the base rate you set in the Simulink model. This helps demonstrate near real-time behavior and inspect your Simulink model while the simulation is in progress.

  1. Position the manual switch in the Algorithm area to receive the simulation output from the Sine Wave block in the Simulation Sources area.

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

  3. In the Simulation Pacing Options dialog box, select Enable pacing to slow down simulation. The specified pace is then automatically applied to the simulation. Select the pace at which the model should run by using the slider or entering the pace in the Simulation time per wall clock second field.

  4. Click Run.

  5. Move the Slider to change the frequency of the Sine Wave and observe the Spectrum Analyzer and the Time Scope blocks. The frequency you set on the slider is the tallest peak on the spectrum analyzer, while the rest of the peaks are the harmonics of this frequency. You can also change the frequency using the slider and observe the shift in the tallest peak on the spectrum analyzer. On the time scope, you can use the cursors to measure the time delay between two peaks of the audio signal. The inverse of this delay is the frequency of the audio signal.

Studying Sampling Frequency and Nyquist Criterion

You can change the sampling frequency of the audio signal and observe the output in the time scope to see whether the audio signal is fully reconstructed or not. The sample time is the frame size divided by the audio sampling frequency. For example, with sample rate of 44,100 Hz and frame size of 4410, the sample time is 0.1 s. You can reduce sample time by increasing the frame size or decreasing the sampling frequency.

Generally, a sampling rate of 44.1 KHz is used to sample an audio signal for musical CDs. Use a frame size of 4410 (1/10th of the sampling rate) to fully reconstruct and represent the audio signal.

Position the manual switch in the Algorithm area to receive the simulation output from the Sine Wave block in the Simulation Sources area. Comment out the blocks inside the Android Inputs and Android Output area.

Time Scope and Spectrum Analyzer Output for Audio Signal with Sampling Frequency of 1500 Hz

For the audio signal frequency set to 1000 Hz and the sampling frequency set to 1500 Hz, it does not meet the Nyquist criterion. Follow this procedure to observe this phenomenon in the simulation mode.

  1. In the Simulation Sources area, in the Sine Wave block, set Sample time to 1/1500 and Sample per frame to 150.

  2. On the Simulation tab of the Simulink model, click Run.

  3. Open the Time Scope and Spectrum Analyzer blocks to observe the audio signal. You can see that the reconstructed audio signal does not nearly resemble the original continuous sine wave.

Time Scope and Spectrum Analyzer Output for Audio Signal with Sampling Frequency of 10000 Hz

Set the audio signal frequency to 1000 Hz and the sampling frequency to 10000 Hz so that the audio signal meets the Nyquist criterion.

1. In the Simulation Sources area, in the Sine Wave block, set Sample time to 1/10000 and Sample per frame to 1000.

2. On the Simulation tab of the Simulink model, click Run.

3. Open the Time Scope and Spectrum Analyzer blocks to observe the audio signal. You can see that the reconstructed audio signal does not nearly resemble the original continuous sine wave.

Time Scope and Spectrum Analyzer Output for Audio Signal with Sampling Frequency of 44100 Hz

Set the audio signal frequency to 1000 Hz and the sampling frequency to 44100 Hz so that the audio signal meets the Nyquist criterion.

4. In the Simulation Sources area, in the Sine Wave block, set Sample time to 1/44100 and Sample per frame to 4410.

5. On the Simulation tab of the Simulink model, click Run.

6. Open the Time Scope and Spectrum Analyzer block to observe the audio signal. You can see that the reconstructed audio signal does not nearly resemble the original continuous sine wave.

Generate WAV File Using createAudio Function

The createAudio.m file includes the createAudio function that is specific to this example. This function generates a WAV file in the current directory of the example. You can execute the function in the MATLAB Command Window in this format.

createAudio(startFreq, endFreq, duration, samplingFreq)

The createAudio function has three input arguments.

  • startFreq — Frequency of the audio signal at which it starts.

  • endFreq — Frequency of the audio signal at which it ends.

  • duration — Total duration of the audio signal generated in seconds.

  • samplingFreq — Sampling frequency of the audio signal.

The createAudio|function generates the audio signal with default amplitude set to |1.

Using this function, generate two audio signals with these specifications.

1. Audio signal with the frequency of 1000 Hz, signal duration of 60 seconds, and sampling frequency of 44100 Hz.

createAudio(1000,1000,60,44100)

This generates a WAV file in the current directory named 1000Hz_1000Hz_60sec.wav.

1. Audio signal that starts at 1000 Hz and ends at 500 Hz, with the signal duration of 60 seconds, and sampling frequency of 44100 Hz.

createAudio(1000,500,60,44100)

This generates a WAV file in the current directory named 1000Hz_500Hz_60sec.wav.

Transfer the 1000Hz_1000Hz_60sec.wav and 1000Hz_500Hz_60sec.wav files to your mobile phone.

Run Simulink Model in External Mode

1. Position the Manual Switch in the Raspberry Pi Sensors and Inputs area to receive the output from the ALSA Audio Capture block in the Raspberry 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 microphone that is interfaced with the Raspberry Pi hardware board.

3. Observe the output on the Spectrum Analyzer and Time Scope blocks. You can observe the noise signals also being captured on the Spectrum Analyzer.

5. Play the 1000Hz_500Hz_60sec.wav file on your mobile phone and place it close to the USB microphone that is interfaced with the Raspberry Pi hardware board.

6. Observe the output in the Spectrum Analyzer and Time Scope blocks. Observe the shift in the peak frequency as it drops from 1000 Hz to 500 Hz on the Spectrum Analyzer.

Other Things to Try

Sample the audio signal at a rate lower than Nyquist criterion and observe the aliasing effect in the Spectrum Analyzer.

See Also