An uninterruptable power supply -UPS- comprising: a first input constructed to receive input power from a first power source; a second input constructed to receive input power from a second power source; an output constructed to provide output alternating current -AC-power derived from at least one of the first power source and the second power source; an inverter coupled to the first input, the second input, and the output and constructed to generate the output AC power; and a controller coupled to the inverter and configured to: identify one or more harmonics in the output AC power; generate an inverter reference signal; and modulate the inverter reference signal to reduce harmonic distortion contributed by the one or more harmonics, wherein, in order to modulate the reference signal, the controller is further configured to: measure a first total harmonic distortion-THD- of a voltage associated with the output AC power; select a harmonic of the one or more harmonics; and find a local THD minimum with respect to the selected harmonic at least in part by increasing or reducing power of the selected harmonic after measuring the first THD by subtracting a sinusoidal signal from the inverter reference signal or adding a sinusoidal signal to the inverter reference signal and measuring a second THD of the voltage waveform of the output AC power to determine whether the second THD has decreased relative to the first THD. a first input constructed to receive input power from a first power source; a second input constructed to receive input power from a second power source; an output constructed to provide output alternating current -AC-power derived from at least one of the first power source and the second power source; an inverter coupled to the first input, the second input, and the output and constructed to generate the output AC power; and a controller coupled to the inverter and configured to: identify one or more harmonics in the output AC power; generate an inverter reference signal; and modulate the inverter reference signal to reduce harmonic distortion contributed by the one or more harmonics, identify one or more harmonics in the output AC power; generate an inverter reference signal; and modulate the inverter reference signal to reduce harmonic distortion contributed by the one or more harmonics, wherein, in order to modulate the reference signal, the controller is further configured to: measure a first total harmonic distortion-THD- of a voltage associated with the output AC power; select a harmonic of the one or more harmonics; and find a local THD minimum with respect to the selected harmonic at least in part by increasing or reducing power of the selected harmonic after measuring the first THD by subtracting a sinusoidal signal from the inverter reference signal or adding a sinusoidal signal to the inverter reference signal and measuring a second THD of the voltage waveform of the output AC power to determine whether the second THD has decreased relative to the first THD. measure a first total harmonic distortion-THD- of a voltage associated with the output AC power; select a harmonic of the one or more harmonics; and find a local THD minimum with respect to the selected harmonic at least in part by increasing or reducing power of the selected harmonic after measuring the first THD by subtracting a sinusoidal signal from the inverter reference signal or adding a sinusoidal signal to the inverter reference signal and measuring a second THD of the voltage waveform of the output AC power to determine whether the second THD has decreased relative to the first THD. <EOS>