Lets talk about your bathroom fan. Make and model please.
I have installed around 30 bathroom fans and some have motors that need caps to start. They shoot a lot of juice right when you turn the fan on to fire up the motor those caps can be double the voltage for a spilt second to start the fan's motor. If you have an aging cap inside this can be the cause of the sound. If the ground wire is loose on the fan this can be why. How old is the fan 3 or more years of heavy use can be enough to kill the cap.
That's a good line of thought .............. however the speaker/headphone noise is still there when the UPS is on bats.
JohnG