Thank you for this script. It is very useful except a bug that should be corrected.
I have formatted/scaled waveform by using this script. I was successful to download it to Agilent Signal generator PSG E8267D.
Unfortunately, the PSG output signal (with ARB waveform modulation) was not as suppose to be. I expected to see burst spread spectrum signal but I've got flat signal with a few dB variations.
After a few hours investigation of the problem I found that the cause is the use of Two's complement formatting in the Matlab code.
wave = uint16(mod(modval + wave, modval));
I've got the correct PSG's output signal without 2's complement formatting.
If you go to the Agilent PSG programming guide (E8251-90255) you can see the following sentence "...Data must be in signed, 2’s complement (binary) format..." (very interesting combination, isn't it?)
There is no use of 2's complement formatting in the attached examples script in C/C++.