Shift Audio Signal Pitch Using MATLAB Function Block on Android Device
This example shows you how to shift the pitch of an audio signal on an Android™ device by using a MATLAB® Function block with the Simulink® Support Package for Android Devices.
In this example, you will learn how to:
Acquire audio from a microphone connected to the Android device.
Shift the pitch of the acquired audio using the MATLAB Function block.
Play the processed audio data on the speaker of the Android device.
Pitch shifting is the ability to modify the pitch of an audio signal to either increase or decrease the pitch. For example, when a fast car passes you in the street, the pitch of the sound from the car increases as the vehicle approaches you and decreases as the vehicle moves away from you. As the source of the audio moves closer or further away from the receiver, the successive signals reach the receiver at either smaller or larger intervals of time. This time difference causes change in the frequency of the audio being heard by the receiver. You can perform a similar pitch-shifting operation by splitting an audio signal into two components, delaying the signals using varying time periods, and then adding them back together. This process simulates the effect of either increasing or decreasing the pitch depending on how the two signals are delayed or overlapped. To ensure uniform power levels, the individual gains for the split signals must be modulated.
To run the model, download and install:
Headphones with in-line microphone (recommended)
Step 1: Connect Android Device for Pitch Shift
Before you start this example, we recommend you complete the Getting Started with Android Devices example.
1. Connect the micro-USB end of the USB cable to the Android device and the regular USB end of the USB cable to the computer.
2. Connect a pair of headphones to the headphone jack of the device.
3. Using the Hardware Setup screen, configure the Android network.
Step 2: Open Pitch Shift Simulink Model
Open the Pitch Shift model.
The model is divided into these areas based on the functionality of the blocks in each area.
The Audio Capture block captures live audio from the microphone connected to the Android device.
This section includes blocks using which you can specify the parameters such as pitch, overlap, and sampling frequency of the audio data. You can tune these parameters to modify the pitch shift effect on the device screen.
Pitch Shift Algorithm
This section includes a MATLAB Function block that implements the delay-based pitch shift algorithm on the acquired audio data.
The inputs to the function are:
capture: The output of the Audio Capture block as an N-by-2 matrix, where N is the samples per audio channel.
pitch: The pitch shift in semi-tones ranging from
overlap: The overlap of the delay line between the two split components. The value ranges from
Fs: The sampling frequency of audio data.
resetFlag: A variable to reset the operation of the
pitchShiftfunction. In this example, the flag is
0indicating that the function is not reset.
Mute & channel selector subsystem creates Bypass and Mute buttons on the device screen. You can toggle between the original and the processed audio by clicking the Bypass button. You can mute the audio by clicking the Mute button.
Audio Playback block plays the audio on the speaker of the device.
Step 4: Configure Pitch Shift Simulink Model
1. On the Modeling tab of the toolstrip , select Model Settings.
2. In the Configuration Parameters dialog box, select Hardware Implementation.
3. Set the Hardware board parameter to
Android Device. This selection automatically populates the parameters in the Hardware board settings with the default values for the device.
4. From the Groups list under Target hardware resources, select Device options.
5. From the Device list, select your Android device. If your device is not listed, click Refresh.
Note: If your device is not listed even after clicking Refresh, ensure that you have enabled the USB debugging option on your device. To enable USB debugging, enter
androidhwsetup in the MATLAB® Command Window and follow the onscreen instructions.
5. Click Apply. Click OK to save your changes.
Step 5: Deploy the Pitch Shift Simulink Model on the Android Device
1. On the model toolbar, set the Simulation mode to
2. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Build, Deploy & Start. The lower-left corner of the model window displays status while the support package generates the code. After successfully generating the code, the support package loads and runs the code on the device. The generated application starts acquiring live audio from the microphone, shifts the pitch of the acquired audio, and then plays the processed audio through the speaker of the Android device. You can now hear the pitch-shifted audio through the pair of headphones connected to the device.
3. In the generated application, change the pitch and the overlap values by using the slider and observe the changes in the pitch-shifted audio.
You can also run this model in external mode to tune the pitch and overlap parameter values in the model and observe the changes in the characteristics of the output audio during simulation.
Other Things to Try
Replace the pitch shift algorithm with any other algorithm that has better performance with respect to your requirements.
Add a low-pass infinite impulse response (IIR) filter to the model to improve the performance of the algorithm.