Programmed harmonic reduction in inverters and controlled rectifiers