An algorithm is proposed for performing harmonic analysis of tonal music. The algorithm begins with a representation of a piece as pitches and durations; it generates a representation in which the piece is divided into segments labeled with roots. This is a project of psychological interest, because much evidence exists that harmonic analysis is performed by trained and untrained listeners during listening; however, the perspective of the current project is computational rather than psychological, simply examining what has to be done computationally to produce "correct" analyses for pieces. One of the major innovations of the project is that pitches and chords are both represented on a spatial representation known as the "line of fifths"; this is similar to the circle of fifths except that distinctions are made between different spellings of the same pitch class. The algorithm uses preference rules to evaluate different possible interpretations, selecting the interpretation that most satisfies the preference rules. The algorithm has been computationally implemented; examples of the program's output are given and discussed.