# Why is there a mismatch between normalized cross-correlation and perception for basic waveforms?

To my ears, sawtooth and square waves are more similar to each other than either one is to a triangle wave. However, when I compare their magnitude spectra against each other using normalized cross-correlation, the square and triangle waves are the most similar to each other, followed by the sawtooth and triangle waves. Does anyone know what the source of this apparent mismatch is?

• Sawtooth:
• Even and odd harmonics, falling off at 1/n
• Square:
• Odd harmonics only, falling off at 1/n
• Triangle:
• Odd harmonics only, falling off at 1/n2

So while triangle and square have the same set of frequencies present, the spectral envelope of sawtooth and square is more similar, with much stronger high frequency harmonics. Triangle waves are simpler/closer to a sine wave.

(Also, if you're generating them digitally, it's uncommon for them to be bandlimited, which produces worse aliasing artifacts with the square and sawtooth than with the triangle. This would be most audible with frequency sweeps or high fundamental frequencies.)

• Right, so I was expecting the normalized cross-correlation of the spectral envelopes to show that sawtooth and square waves are the most similar to each other, but I got the opposite result. Any ideas as to why? I checked and double-checked for math errors and couldn't find any, but maybe my eyes/brain missed something... – visual-kinetic Oct 2 '12 at 16:24
• I don't have any data to back that, but I'm not sure that you'd get consistent results when asking people to say whether square is more similar to triangle or to sawtooth. Timbre is multidimensional. Some people would group square and saw because they sound richer than triangle (higher spectral centroid / f0). Some people would group square and triangle because they sound "hollower" than sawtooth (no even harmonics). – pichenettes Oct 2 '12 at 18:30
• @visual-kinetic: Ohhh, you're cross-correlating the spectrum of a square wave with the spectrum of a triangle wave? Well all of the frequencies are identical in that case. If you're multiplying a sawtooth with a square wave, half the harmonics are being multiplied by 0, so the ratio to the total amplitude will be lower? – endolith Oct 2 '12 at 18:39
• @visual-kinetic: have a look at this answer. dsp.stackexchange.com/questions/3425/… . In particular the link to the document describing common features used for audio classification. Extracting these features on each audio clip and comparing the feature vectors will be much more useful than comparing the raw audio waveform! – pichenettes Oct 2 '12 at 19:26
• @visual-kinetic: Not sure if this is what you're doing, but you might want to look at the two-way mismatch (TWM) algorithm, too. – endolith Oct 2 '12 at 21:41

The human ear does not "sense" the time-domain representation of the signal, but, in a rough approximation, the magnitude of its frequency-domain representation (spectrum).

In particular, several variants of the same stationary signal with different phase shifts will sound exactly the same. From that, it is clear to see that normalized cross-correlation is a poor metric for comparing audio signals; since a sine-wave and the same sine-wave with a 90° phase shift will have a low cross-correlation, while they are indistinguishable. A better place to start to compare waveforms would be to compute the cross-correlation of the magnitude spectra.

• Sorry, by "harmonics" I was actually referring to the waveforms' magnitude spectra. I'll edit the question so it's clearer. Thanks! – visual-kinetic Oct 2 '12 at 15:17
• I think you might mean a 90-degree phase shift. Two sinusoids with a 180-degree phase shift between them will have a significant (negative) cross-correlation. For a 90-degree phase shift, you would get zero cross-correlation if the time window is an integer number of periods. – Jason R Oct 2 '12 at 15:33
• Yes, I meant 90°, edited. – pichenettes Oct 2 '12 at 17:42
• -1 He was not talking about time-domain cross-correlation. – Jim Clay Oct 2 '12 at 19:51