2

I'm starting a project on Python where I need to develop a pitch-detection system, basically what I have to do is to record a sound coming from a guitar string, then Identify which is the tone of that sound.

I have read and searched through websites (including stackoverflow) so I can understand the main ideas of important things like: FFT, Time-domaing, Frecuency-domain, Harmonics, pitch detection algorithms, octave-errors and so on.

After my research I found that I could use HPS (Harmonic Product Spectrum) Algorithm and that algorithm belongs to a frecuency-domain approach, that means that I have to (In general steps):

  1. Record the sound from the guitar (avoid external noises).
  2. Use FFT function so I can transform that audio from a time-domain to a frecuency-domain (that's what FFT does).
  3. After I get that data (an array) then I have to use HPS so I can find the highest tone which it will be the tone string sound.

My problem starts in the last step, I have read the ecuation of the HPS and some lectures about that, but I still can't understand it and develop my own function.

Am I missing something or something that I don't understand and I think I do? I just can't find a way to program my own HPS algorithm.

help-info.de
  • 6,695
  • 16
  • 39
  • 41
Teddy S.
  • 33
  • 8
  • 1
    http://stackoverflow.com/questions/39230595/how-to-get-the-fundamental-frequency-using-harmonic-product-spectrum asked a question about HPS in C# and I wrote the algorithm in Python: https://gist.github.com/fasiha/957035272009eb1c9eb370936a6af2eb includes code and results for an voice audio clip, and compares HPS to a couple of other spectral estimators (Welch and Blackman-Tukey). – Ahmed Fasih Sep 08 '16 at 19:50
  • Thanks @AhmedFasih, I'm not infrot of my PC right know, but I'll test it as soon as possible, but meanwhile I have a question, when we use Hamming-window, Do we need to do it before executing FFT or after that?. – Teddy S. Sep 08 '16 at 20:40
  • Before. But windowing (Hamming or otherwise) isn’t absolutely required—windowing degrades your ability to pinpoint exactly what frequency a single sinusoid is at but improves your ability to detect a very quiet sinusoid that is very close in frequency to a much louder one (i.e., Hamming windowing widens the mainlobe but suppresses sidelobes, if those words mean anything to you). If you’re confident your signal will have only one voice, you can definitely omit windowing. – Ahmed Fasih Sep 09 '16 at 03:34
  • Re: windowing: A single voiced pitch can contain far more than one frequency peak (e.g. overtones), all of which will have side-lobes, and thus side-lobe suppression may not be completely without value. – hotpaw2 Sep 09 '16 at 05:34
  • Can you be more specific about precisely what details you don't understand about the HPS algorithm or pitch estimation? Otherwise, your question is too broad. – hotpaw2 Sep 09 '16 at 05:37
  • @hotpaw2 sorry for late response, I have read articles like [link](http://musicweb.ucsd.edu/~trsmyth/analysis/Harmonic_Product_Spectrum.html) and even in stackoverflow and I don't get how to develop my own code by myself, even though in every plase I read something like: "easy to implement". – Teddy S. Sep 09 '16 at 19:38
  • Is there something you don't understand about other implementations, such as: http://stackoverflow.com/questions/39230595/how-to-get-the-fundamental-frequency-using-harmonic-product-spectrum ? – hotpaw2 Sep 09 '16 at 20:01
  • @hotpaw2 I haven't tryed Ahmed Fasih solution yet (I apologize for that), but if you are talking about the first code in the question I didn't understand:1) where did he have the problem in his code, 2) How does his downsample work 3) I almost understood his for loop inside armonicProductSpectrum function, but the part that confuses me is this that in the article it says: R number of harmonics being considered, why is he using array.Length (which is hps5.Length at the end of the day) as R instead of data.length, those are my main issues trying to understand that code. – Teddy S. Sep 09 '16 at 20:33

2 Answers2

1

In the HPS quesion here:

How to get the fundamental frequency using Harmonic Product Spectrum? ,

the number of harmonics considered is 5 (R = 5); and the 5 harmonic spectrums are in hps2 thru hps5 (plus the original FFT spectrum) after downsampling by sequential harmonic ratios.

Then the 5 downsampled spectrums are summed.

Then the entire HPS summing array length is searched to find where the peak or maxima in the summed 5 harmonics is located.

The downsampling and search for the optimal HPS estimate might not be done optimally in that example. But that's a different Q&A (some of which is already in the answers to the above SO question).

Community
  • 1
  • 1
hotpaw2
  • 70,107
  • 14
  • 90
  • 153
0

I've done this before in few ways (either FFT which is working in Frequency domain or Autocorrelation and AMDF which are working in Time domain). For me personally Autocorrelation is favourite since it's simple and clear to implement and in your use case, analyzing guitar strings, worked with 100% accuracy. So I can recommend it to you. I've shared my code before and you can find it fully explained on the following link: Android: Finding fundamental frequency of audio input

Community
  • 1
  • 1
Alexander
  • 165
  • 9