How to test if stereo speakers are in phase

    On my main computer I was using a set of cheap amplified speakers.  The speakers soon burnt out but the amplifier was still good so I adapted a couple of old oval 8"x5" speakers which I placed in cardboard boxes and they work quite well with the old amplifier.

    Recently when using headphones I noticed a slight but annoying hum on the left side.  I noticed the hum was there in the speakers too but not so noticeable.  So, today, I opened the amplifier and got ready to check what might be the cause of the hum.
    I noticed the cable for the right hand speaker (which did not have a hum) was tied in a knot with the input cable.  I tried tying the cable for the left speaker in the same knot and it got rid of the hum. Maybe some negative feedback or shielding. Who knows. That was easy enough though.
    But, after assembling everything again, I was not sure if the speakers were correctly wired in phase or out of phase.  Hmmmm. . . how to find out without opening the whole thing again?

    I had an idea.  Using CoolEdit I generated a ten second audio file which had a constant tone in the left channel and the same tone in the right channel except that it inverted twice.  The right hand channel was divided into four sections.  The first and third were exactly like the left channel but the second and fourth had the same signal inverted.

    I thought I would be able to tell by ear when the signal sounded louder (in phase) or weaker (out of phase).  In fact, I could barely tell the difference but a recorder with a VU meter could very clearly discern and, as was to be expected, the speakers were out of phase and I had to invert one of them to make things right.  That might have been the end but now comes the interesting part.
    I thought it migh be useful to keep that audio file for future tests so I compressed it using MPEG-L3 and saved it as WAV. But, to my surprise, the compressed file was defective.  The two parts where the signals are in phase were saved correctly but in the two parts where they were opposed ther was nothing. The signal was flat zero in both channels.  I tried increasing the bit rate and other thing and, although it did reproduce those parts, they were badly distorted.
    Then I remembered that Besweet has many more options than CoolEdit so I switched to Besweet.
    Looking in LAME´s command line modifiers I see

-m s/j/f/d/m   stereo mode
Joint-stereo is the default mode for stereo files with VBR when -V is more than 4 or fixed bitrates of 160kbs or less. At higher fixed bitrates or higher VBR settings, the default is stereo.

In this mode, the encoder makes no use of potentially existing correlations between the two input channels. It can, however, negotiate the bit demand between both channel, i.e. give one channel more bits if the other contains silence or needs less bits because of a lower complexity.

joint stereo
In this mode, the encoder will make use of a correlation between both channels. The signal will be matrixed into a sum ("mid"), computed by L+R, and difference ("side") signal, computed by L-R, and more bits are allocated to the mid channel.
This will effectively increase the bandwidth if the signal does not have too much stereo separation, thus giving a significant gain in encoding quality.

Using mid/side stereo inappropriately can result in audible compression artifacts. To much switching between mid/side and regular stereo can also sound bad. To determine when to switch to mid/side stereo, LAME uses a much more sophisticated algorithm than that described in the ISO documentation, and thus is safe to use in joint stereo mode.

forced joint stereo
This mode will force MS joint stereo on all frames. It's slightly faster than joint stereo, but it should be used only if you are sure that every frame of the input file has very little stereo separation.

dual channels
In this mode, the 2 channels will be totally indenpendently encoded. Each channel will have exactly half of the bitrate. This mode is designed for applications like dual languages encoding (ex: English in one channel and French in the other). Using this encoding mode for regular stereo files will result in a lower quality encoding.

The input will be encoded as a mono signal. If it was a stereo signal, it will be downsampled to mono. The downmix is calculated as the sum of the left and right channel, attenuated by 6 dB.

    Probably CoolEdit and the MP3 codec it is using are using Joint Stereo mode by default but this results in problems when there is so much difference between channels.
    I tried using BeSweet and forcing plain Stereo ( -m -s) and the file was compressed correctly. You can download it here.  Remember, parts 1 & 3 are in phase and parts 2 & 4 are out of phase. If you hear parts 1 &3 louder then your speakers are in phase and correctly wired.  If you hear parts 2 & 4 louder then your speakers are out of phase and one of them should be inverted.
    Now I am reminded that, in the past, I have had problems with some songs which had problems when compressed with CoolEdit and MP3. Some parts would not sound right. When I compressed them with the old DivX audio codec they sounded right but not many people have that codec. Maybe I will try compressing them with Besweet and MP3 forcing the Stereo mode and preventing the Joint Stereo mode from being used. Maybe that will make things sound right.