Octave error - STFT parameters when detecting pitch in guitar audio signals I am working with this STFT method to detect pitch in monophonic guitar audio signals. 

The method is working correctly for medium pitches (above 150Hz). However, in low notes I have some issues.

For example, I have an audio file that had the A2, A#2 and B2 notes played. So, the pitches detected should be approximately 110, 116, 123. When I ran the STFT with 40000 as a sampling rate, there was an octave error in all of them. The notes detected were A3, A#3 and B3.

I though I should see why this happens and I went in my STFT, and printed, for each note played, the 5 strongest bins in terms of magnitude. Those were the results:

First note: [539.49506, 975.88092, 419.11325, 326.00168, 217.16208]

Second note: [0.0, 0.0, 573.76556, 229.9518, 117.29102]

Third note: [0.0, 240.0067, 0.0, 118.64342, 0.0]

As you can see, for the first note, the correct pitch is not even amongst the 5 max. magnitudes. For the second and third notes, they are amongst the top 3 but not enough to become the selected pitches.

I tried to do the same with a sampling rate of 30000, and I still had the same problem, but this time the correct pitch was in the top 5 for the first note.

I know how to fix this problem, so my question is more theoretical. How can the sampling rate affect this? Why did this happen with 40000 and not 30000? In the end, how should I decide on my SR final value?

As a follow-up question, I see that I can also choose on other parameters like the n_fft (number of FFT bins or FFT size) and hop_length. How should I choose on those? What are upsides and downsides for high/low values (if the answer is too long to answer, then a reference would be great).
The octave error is because you are using a spectral frequency estimator (STFT), not a psychoacoustic pitch detection or estimation method.  The spectral frequency peaks in the sound are often not the pitch frequency, but instead often one of the many harmonics.  As you found, the fundamental pitch frequency can even be completely missing.  This is especially true for guitar notes, voices, and sounds from other large stringed instruments.

The solution is not to play with sample rate and STFT FFT window parameters, but to look up more reliable pitch detection and estimation algorithms.