Why does the brain learn to form a strong connection between some pitches and other pitches like having the internal sense that two notes are a fifth apart?
The reason why we are able to learn relative pitch is found in psychoacoustics.
In order to make sense of the jumble of frequencies that reaches our ear we are able to group certain frequencies and assign them to a single sound source. Our brains use a certain physical property of all natural (harmonic) sounds: that they consist of a certain base frequency and a set of frequencies based on that frequency following the harmonic series. In this way our brain has evolved to use the harmonic series as a filter to distinguish sound sources from each other.
The abstract of this paper gives a better description of what happens: https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2885481/
Harmonic complex tones are a particularly important class of sounds found in both speech and music. Although these sounds contain multiple frequency components, they are usually perceived as a coherent whole, with a pitch corresponding to the fundamental frequency (F0). However, when two or more harmonic sounds occur concurrently, e.g., at a cocktail party or in a symphony, the auditory system must separate harmonics and assign them to their respective F0s so that a coherent and veridical representation of the different sounds sources is formed.
So, our brain is already wired to compare frequencies and especially the frequencies of the harmonic series.
Why do humans have relative pitch?
I think relative pitch comes with the fact that we are able to recognize certain intervals, which then ties to the question of "why do humans need to be able to hear intervals?".
Intervals base the foundation of music with melody and harmony. One idea of why music exists in the first place, in terms of evolution, is so humans can more aptly socialize with each other. And the humans that can't recognize music are rejected from tribes, etc.
Therefore, relative pitch is more of a "side effect" of humans evolving with music. Pitches are also calculated from hairs in the ear which resonate at certain frequencies. The brain then learns about these intervals.
Why does the brain learn to form a strong connection between some pitches
This part is more cultural, European music puts emphasis on the 12 tone intervals. You can recognize these pitches because you've had more practise identifying pitches (E.g. perfect fifth). Enough practice with a more rare interval like natural third will yield similar "connections".
Your idea should be part of your question.
I also noticed from my own senses that what really sounds like an F sharp is a tiny bit higher than what really sounds like a G flat.
This depends on your system of tuning. Also in most tuning systems where the pitch of
G-flat isn't the same as the pitch of
F-sharp is generally lower (not higher).
... that is log of the frequency, then a B and a C
I'm not sure what you mean here but, yes, the relationship between
The brain adapts and starts noticing a pattern in what's entering the ear.
Are you really trying to ask "How does relative pitch work" vs "Why do humans have relative pitch?". Ask a new question if you meant the former.
Because there is a mathematical relationship between frequencies, arising from the harmonic series. This is what causes certain intervals to sound "related".
It works out that (from the harmonic series) a fifth corresponds to multiplying frequency by 3/2 (i.e. the 3rd harmonic lowered by an octave), a major third to 5/4 (5th harmonic lowered by two octaves). (In fact we generally use a slightly compromised version of these numbers to allow us to play chromatically in different keys, but they are very close.)
If you have a guitar you can demonstrate this to yourself by playing up through the harmonics and listening to the pitches produced. It's a useful exercise.
It would be much easier to answer to the question: Why do most people not have perfect pitch?
The question you're asking can be compared with: Why do some people see colours and some not?
Somehow we are all "blind" when we are born - concerning relative pitch and learning to differ sounds in a melody - and we have to learn to "see" (or hear, better listen) like we learn to see in 3 dimensions.
I would pretend: Human don't have relative pitch, a few have perfect pitch - like most of us can see and differentiate colors.
So we have to learn the relative pitch like we learn the mother language. But if our mother and parents don't sing with us baby songs and we are not taught an instrument - also playing autodidactical (self-educated) - most of us don't have relative pitch get laid in the cradle - we have to learn relative pitch like the grammar of a foreign language. And this is a long and hard way to go.
If we have grown up with music and baby songs and other songs it will be much easier to learn the relative pitch as the basic function to this have already been formed.
I don't actually know the answer to that question so I will just make a guess. Actually the following is a simplifying approximation of my real guess.
I learned that when you go up an octave, the frequency doubles and when the frequency multiplies by three halves, the pitch goes up by what you hear as a fifth.
I also noticed from my own senses that what really sounds like an F sharp is a tiny bit higher than what really sounds like a G flat. The mathematics indeed supports that observation. It can be proven using the math alone without reliance on hearing that an F sharp is a tiny bit higher than a G flat but a lot closer together in pitch, that is log of the frequency, then a B and a C.
A sinusoidal sound wave registers as only one pitch in the ear. When you hear a note with a well defined pitch, it's nearly a repeating sound wave and that can be expressed as an infinite sum of sinusoidal sound waves each of which has a frequency that's a multiple of the frequency of the original sound wave.
The brain adapts and starts noticing a pattern in what's entering the ear. Whenever the first harmonic is a certain pitch, the second and third harmonics will always be a pitch corresponding to double and triple the frequency. It registers two notes as being a fifth apart because those pitches are heard together all the time.
Since the log of 3 to the base 2 is irrational, you can get arbitrarily close to any pitch from a given pitch just by multiply and dividing by 2 or 3. The brain doesn't devote much attention to changes in a factor of 5. Actually, I think a change in frequency by a factor of 5/4 can be misheard as a third but it really isn't. It just differs from one by a factor of 81/80.
The natural speaking pitch for every speaker is different (and may be different depending on time of day and other variables), inflection is relative to that pitch. Music is processed by the same complex hearing apparatus that has evolved to deal with speech (among other noises with natural pitch relations). Having to employ absolute pitch in order to be understood would be a nightmare for speakers.