I've finally sorted out the code to generate PWM waveforms that you'd use to control an electronic device such as an H-bridge driver. I must warn you this is engineering coding, error checking is non existent, and things may be a little rough around the edges, but it'll get you started on a version for your specific problem. It should also be noted that some problems can't be solved. If the magnitudes are set too high you could end up in a situation where there is more power in the waves spectrum than can be in the actual waveform. In that case the solver will do its best to minimise the objective function but it won't actually find a solution.
The code will generate a waveform that you can scale to the desired frequency. By entering two vectors, one containing the harmonics of the output waveform that you want to control, and the second containing the magnitude of the harmonics, a set of switching times will be returned. It's also important to remember that all even harmonics are automatically zeroed due to the quarter-wave symmetry of the waveform.
As a demonstration I've generated the example waveform below that sets the first harmonic magnitude to 0.5 and all odd harmonics up to the 31st to zero.
The code for this demonstration can be found here.
|FFT of a HEPWM Waveform|
Although the magnitude of the first harmonic is set to 0.5 the FFT shows two peaks of 0.25. This is due to the nature of the FFT showing positive and negative frequency. These combine to give the 0.5 magnitude. All the harmonics up to the 31st are however zeroed.
There are still a couple aspects of this method I'd like to investigate, but all the heavy lifting is done and we can get into some practical aspects of the process.