.st0{fill:#FFFFFF;}

Pitch Detection of Speech Signal Using Autocorrelation Method in MATLAB 

 June 9, 2020

By  MayuriParkhe

Introduction

Pitch is a crucial part of voiced speech. It contains speaker-specific information. It is additionally required for speech coding tasks. Therefore, the estimation of pitch is one of the significant issues in speech processing. Pitch detection is of interest whenever a single quasi-periodic sound source is to be studied or modelled, specifically in speech and music. Pitch detection algorithms can be divided into methods that operate in the time domain, frequency domain, or both. We are going to see the time-domain method, which is the Autocorrelation of Speech in MATLAB. 

Prerequisite Required 

I have used MATLAB Version R2019a and the code should be compatible with any version from R2017a.    

Theoretical Explanation

Speech consists of different frequencies that are harmonically related to each other in the form of a series. The lowest frequency of the harmonic series is known as the fundamental frequency or Pitch frequency. It is the fundamental frequency of vibration of vocal cords.  


The number of samples after which the waveform repeats itself is the pitch period in terms of the number of samples. If we know the sampling frequency, we can find the pitch period in seconds. 
The autocorrelation method requires a speech segment with at least two periods to find the pitch period. The Equation for Autocorrelation  


R_{xx}(x)=\sum_{i=1}^{2N+1}(a(i)a(i+k))/2N+1


Let us consider the above as equation (1), where
N = window size  
a = signal samples of the voiced segment 
= zero lag location 

The main peak in the autocorrelation function is at the zero-lag location (k = 0). The location of the next peak gives an estimate of the period, and the height gives an indication of the periodicity of the signal.

MATLAB Implementation 

Get Access to
Code, Sample & Report!

Find pitch periods of a speech signal using the Autocorrelation method; Developed in MATLAB R2019a with Signal Processing Toolbox

Now we will discuss the MATLAB Implementation for this method. 

Step 1: Record the speech signal and store it in the array (Set sample rate fs as 10000) and give filename. 
Function for recording - “audiorecorder(fs,8,1)”  
Function to store data - "getaudiodata" (store it in a variable, 'y') 


Step 2: Write the data to a WAVE file named name.wav in the current folder. 
Function to write data - "audiowrite ('filename', y, fs)" 
Use "audioinfo" to get the information of the speech signal.  


Step 3: Read the data from 'y' and plot it. 
Function to read data - [y, fs] = "audioread" (filename) reads data from the file named filename, and returns sampled data, y, and a sample rate for that data, fs. 
Speech processing fig 1
Step 4: Take 1000 samples of the voiced segment and plot it. 
a = y (4000:5000); (you can take any voiced segment range) 
Speech processing plot 2

Step 5:  Calculate the Autocorrelation of the speech signal 

We have continuous speech signals. We are not going to find autocorrelation using MATLAB function "autocorr" Sample autocorrelation. We will implement the above Eqn.1 for calculations.
  
We will calculate the autocorrelation for, say, 32 overlapping samples. This means that two speech segments, one extending from sample number 1-32, are correlated to the other segment from sample number 2-33, then 3-33, and so on. Hence, the sample shifts in the steps of 1, starting from 1. We will use a shift up to 320 samples to the shift value for which the correlation is the highest. The distance between two successive maxima in correlation will give a pitch period in terms of the number of samples. We will use for loops for this. 
Speech processing figure 3
Step 6: Calculate the pitch period 
Find peaks for the min, mean and max locations. 

Calculate the period by comparing the "time" between peaks 

Speech processing command window

(For all the plots, use the title, xlabel, and ylabel) 

Applications   

Where can we use the Pitch Period Estimation for speech signal in real time-based situationsThere are several applications of the Pitch period Estimation. We will see some of them in Human-robot voice interface and Detection in Industrial Background Noise   

Conclusion  

Pitch frequency cannot be found from the speech signal directly since speech is a time-varying signal. To determine the pitch using the autocorrelation method, we must consider voice with at least two pitch periods. If we consider the unvoiced segment pitch cannot be determined since the unvoiced segment consists of random noise. 

Did you find some helpful content from our video or article and now looking for its code, model, or application? You can purchase the specific Title, if available, and instantly get the download link.

Thank you for reading this blog. Do share this blog if you found it helpful. If you have any queries, post them in the comments or contact us by emailing your questions to [email protected]. Follow us on LinkedIn Facebook, and Subscribe to our YouTube Channel. If you find any bug or error on this or any other page on our website, please inform us & we will correct it.

If you are looking for free help, you can post your comment below & wait for any community member to respond, which is not guaranteed. You can book Expert Help, a paid service, and get assistance in your requirement. If your timeline allows, we recommend you book the Research Assistance plan. If you want to get trained in MATLAB or Simulink, you may join one of our training modules. 

If you are ready for the paid service, share your requirement with necessary attachments & inform us about any Service preference along with the timeline. Once evaluated, we will revert to you with more details and the next suggested step.

Education is our future. MATLAB is our feature. Happy MATLABing!

About the author 

MayuriParkhe

Mayuri Parkhe is a MATLAB Developer at MATLAB Helper.
A B.E. graduate with an EXTC background from the University of Mumbai.
She believes if one wants to set off and go develop something brand new, he doesn't need millions of dollars of capitalization. All the person needs is a comfortable space around you and enough time to work on, and the dedication to go through with it.

  • Excellently explain the logic of pitch detection and autocorrelation method math.

  • Abhishek Agrawal says:

    It was great to know how MATLAB can be used for pitch detection of speech signals using the Autocorrelation method.

  • am getting an error
    Index exceeds matrix dimensions
    on a=y[4000:5000];
    so i have changed to 400:500
    but again an error is coming on
    sum(k)=sum(k)+(a(i)*a(i+k));
    same error
    Index exceeds matrix dimensions
    how to solve it plz help bcoz this is reg my project

    • This must be dependent on your y variable. If you rectify your y variable, it should get solved. If you still have any trouble, do write to us at [email protected]

  • tanu jain says:

    good code for beginners and for research work…

  • Rithika Shenoy says:

    Thank you , glance of this helped me to analyse the steps and applications involved in pitch detection of speech signals using the Autocorrelation method.

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

    Connect with MATLAB Helper ®

    Follow: YouTube Channel, LinkedIn Company, Facebook Page, Instagram Page

    Join Community of MATLAB Enthusiasts: Facebook Group, Telegram, LinkedIn Group

    Use Website Chat or WhatsApp at +91-8104622179

    >