The problem of designing a zero mean two-level switched periodic waveform with a prescribed harmonic content is considered. A solution is obtained by posing it as a linear programming problem, with the DC bus utilization (ratio of the total power in the prescribed harmonic content to the square of the DC bus voltage) as the cost function and the prescribed harmonic content as linear constraints. The method can be used for the purpose of selective harmonic elimination in two-level inverters and also for generating switching patterns for harmonic compensators. The proposed method is computationally efficient and leads to a waveform with the a very low total harmonic distortion in the case of selective harmonic elimination. Simulations and experimental results for selective harmonic elimination are provided for corroboration. © 2017 IEEE.