# Cepstrum, peak picking

I try to obtain fundamental frequency from cepstrum. I decide to use peak picking algorithm, which pick maximum value of the Cepstrum (c) with sampling rate fs.

function [f0] = spPitchCepstrum(c, fs)
% search for maximum  between 2ms (=500Hz) and 20ms (=50Hz)
ms2=floor(fs*0.002); % 2ms
ms20=floor(fs*0.02); % 20ms
[maxi,idx]=max(abs(c(ms2:ms20)));
f0 = fs/(ms2+idx-1);
end


It works but I can not understand why f0 is calculated like:

f0 = fs/(ms2+idx-1);


where in denominator is value consist indexes.

Can you somebody explain me why it is calculate in this way?

Cepstrum argument is called quefreency, which is in fact a time domain. So for example if you are looking for the fundamental frequency then you are searching for a peak in a specific range. In your case that's $[0.002; 0.2] \;sec$, which corresponds to $[50; 500] \; Hz$., knowing that $f=\dfrac{1}{t}$.
So when you are searching for fundamental frequency, you can find peak in quefrency domain and then convert it to corresponding frequency. You can also use a different approach. We know that our cepstrum is symmetrical around center (similarly to Fourier Transform), and also we have our sampling frequency $f_s$. Let's say that you have $N$ cepstrum bins, therefore distance between each of them is: $\dfrac{f_s}{N}$. That's why you can find the corresponding fundamental frequency $f_0$ by dividing sampling frequency by the index of a peak.
For example our peak is at index number $10$, and sampling frequency is $1000 Hz$. Corresponding $f_0$ is $100 Hz$. If peak is at bin $50$ (more to the right, meaning lower frequency - remember quefrency is like time), then $f_0 = 20$ Hz. The subraction of 1 in denominator is cause by fact that MATLAB is indexing from 1 not from 0, like most of the programming languages.