I have this piece of song for guitar:
It has chords written on top of each bar.
But I have pieces without chords on top of them:
Is there a way to know the chords based on the notes?
Assigning chords to a melody is an art in itself, and there's no "correct" way to do it. There are countless ways to harmonize a melody with chords, but it can be learned.
Do you want to just play songs, or do you want to learn to find chords to melodies? If you want ready-made chords, there are song books and online resources for that. But if you want to learn to find chords yourself, you have to know a little bit of music theory, like (1) how to identify the key of the song, and (2) what chords are commonly used in each key. When you know these two things, you can start trying out chords until you find ones that sound good.
To get you started with the example, the second melody you posted is in the key of G major, where the most common chords to try are:
And in this particular case, you also want to try:
I think you can tell by ear when you feel that the chord should change. Then try any of the other chords. You'll get better at it.
There are a lot of different ways to harmonize a melody. It's especially dependent on musical style.
With the examples you gave the musical style seems to be traditional or folk tunes, perhaps pop tunes.
One approach to try is use the primary chords of the key. Those are the triads built of the 1st, 4th, and 5th degrees of the scale. So in
Bb major the primary chords are
F. Another common set of chords to try using includes the relative minor
of the 1st chord instead of the 4th chord. In
Bb that would be
From these two simple set of chord the idea is select a chord that contains the melody note. The first chord in a bar often has special importance. Some melody notes will have two chord options. Ex.
F in the key
Bb could be harmonized with either
To some degree you can make choices based on if a phrases is and ending. In
Bb chord can make an ending whereas
F tends to create a feeling to continue. This is the level where melody harmonization is not a 'paint by numbers' process. Studying the use of chords in songs and experience eventually will guide your sense of harmonization.
My answer is just quick intro to the subject. You should seek out some books on the topic - harmony textbooks for starts - for a deeper understanding.
BTW, We Wish You a Merry Christmas is actually not a simple example to start with. Even though the song is super familiar it actually uses some chords that are not in the key. That's chromatic harmony. It isn't too hard to learn about chromatic harmony, but it definitely is not the place to start learning harmony.
There's no way to figure out the original (composer intended) chords based on melody. You might be lucky or get close by trying to infer the chords from the notes and phrases but these methods are not a sure way to "reenngineer" the original harmony. Especially as you put it "assignments to the bars" which is harmonic rythm is a bit of a guess game. For your examples the best and practical way is to simply find another source that actually has the chords written out. I'm sure you'll find chords to "We Wish You a Merry Christmas" in like five seconds.
As you ask about a „song“ and also your examples are showing what kind of songs you mean. So this songtunes are mainly harmony based:
we can derive from the final note the root tone and the key,
As the final chord normally is the tonic, we can also derive the dominant chord and
find out where the tonic in the beginning will appear (in most cases it will be in the first measure ... with exceptions.
As this songs that you might have in mind are chord based you will find some clausulas and formulas in the melody that contain the notes of a certain chord of which you can conclude what chord this must be.
There are usually chord patterns and typical progressions (as cadences and sequences, secondary fifths and subdominant cadence) underlying to the melodical ideas of the composer. (Especially in folk music and popsongs, also in blues and jazz).
But eventhough there always more or less different and more or less “originally” harmonizations possible.