Get Started with Audio Signal Processing Using Android Devices
This example shows how to use the Simulink® Support Package for Android® Devices to get started with audio signal processing applications on Android devices. 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 time scope to observe and study the audio signals in the frequency and time domains, respectively.
Use Time Scope block on Android device.
Study the significance of sampling frequency, Nyquist criterion, and frame size.
Capture and play the audio signals on your Android device in real time.
Prerequisites
For more information on how to use the Simulink Support Package for Android Devices to run a Simulink model on your Android device, see Getting Started with Android Devices.
Required Hardware
Android device such as a phone or tablet
USB cable
Headphones with microphones
Hardware Setup
Connect your Android device to the host computer using the USB cable.
Connect the headphones to your Android device.
Configure Simulink Model and Calibrate Parameters
Open the androidAudioGettingStarted
Simulink model.
Simulation Sources
The Sine Wave block outputs a sinusoidal signal for simulation purpose. Use the Sine Wave:Frequency dashboard panel to control the frequency of this sinusoidal output.
Configure these parameters in the Block Parameters dialog box of the Sine Wave block.
Set Amplitude to
1
.Set Frequency to
1000
.Set Sample time to
1/44100
.Set Samples per frame to
4410
.
To adjust the frequency of the sinusoidal signal, set the Frequency parameter in the Sine Wave block or use the slider on the dashboard panel.
Android Inputs
Use the Audio Capture block to capture audio from the microphone in the deployment mode. This block is configured to its default parameters.
Algorithm
The default position of the manual switch is on the sine wave audio data received from the Simulation Sources area.
Display and Visualization
Use the Time Scope and Spectrum Analyzer blocks to view the audio signals in time and frequency domain, respectively.
Android Output
Use the Audio Playback block to play audio on your Android device.
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.
Position the manual switch in the Algorithm area to receive the simulation output from the Sine Wave block in the Simulation Sources area.
On the Simulation tab of the Simulink model, select Run > Simulation Pacing.
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.
Click Run.
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.
Spectrum Analyzer Output
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.
Time Scope Output
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.
Time Scope 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.
In the Simulation Sources area, in the Sine Wave block, set Sample time to
1500
and Sample per frame to150
.On the Simulation tab of the Simulink model, click Run.
Open the Time Scope block and observe the audio signal. You can see that the reconstructed audio signal does not nearly resemble the original continuous sine wave.
Time Scope 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 10000
and Sample per frame to 1000
.
2. On the Simulation tab of the Simulink model, click Run.
3. Open the Time Scope block and observe the audio signal. You can see that the reconstructed audio signal does not nearly resemble the original continuous sine wave.
Time Scope 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 44100
and Sample per frame to 4410
.
5. On the Simulation tab of the Simulink model, click Run.
6. Open the Time Scope block and observe the audio signal. You can see that the reconstructed audio signal does not nearly resemble the original continuous sine wave.
Run Simulink Model in External Mode
In external mode, you can observe the audio signal output on the application on your Android device as well as the spectrum analyzer and time scope block in the Simulink model.
Position the manual switch in the Algorithm area of the model to receive output from the Sine Wave block in the Simulation Sources area.
On the Hardware tab of the Simulink model, in the Mode section, click
Run on board
. In the Run on Hardware section, click Monitor & Tune. The androidAudioGettingStarted application launches automatically on your Android device.In the Simulink model, change the frequency of the audio signal using the slider and run the model in external mode once again.
Connect headphones to your Android device and you can hear the tone of the audio signal on changing the androidAudioGettingStarted application.
Deploy Simulink Model on Android Device
1. Position the manual switch in the Algorithm area of the model to receive output from the Sine Wave block in the Simulation Sources area.
2. On the Hardware tab of the Simulink model, in the Mode section, click Run on board
. In the Deploy section, click Build, Deploy & Start. The androidAudioGettingStarted application launches automatically on your Android device.
3. On the Dashboard tab of the application, change the frequency of the audio signal using the slider.
4. On the App tab of the application, observe the audio signal. Connect headphones to your Android device and observe a change in the tone of the audio signal.
5. Position the manual switch in the Algorithm area of the model to receive output from the Audio Capture block in the Android Inputs area.
6. On the Hardware tab of the Simulink model, in the Mode section, click Run on board
. In the Deploy section, click Build, Deploy & Start. The androidAudioGettingStarted application launches automatically on your Android device.
Other Things to Try
You can adjust the volume of the Audio Capture block output in the deployment mode. Add a Constant block in the Simulation Sources area and multiply it with the output from the Android Input area as shown. Connect the Constant block to the slider to adjust the volume.