double** table_new_pulse(ULONG length, ULONG nharms, double dcyc)

    ULONG harm = 1;
    double step = TWOPI / length;
    double dcyclerad = M_PI * dcyc;
    for (ULONG i = 0; i < nharms; i++)
        double amp = sin(harm * dcyclerad) / harm;
        for (int j = 0; j < length; j++)
            table[j] += amp * cos(step * harm * j);
        harm += 1;

    // normalize

    return table;

The preceding C function tries to approximate a pulse wave of duty-cycle dcyc by summing cosines of the first nharms each with an amplitude of sin(sin(harm * dcyc) / harm) and storing the values in a lookup table. If length is 1024 and nharms is 8, when dcyc is 50% the resulting waveform has equal positive and negative peak amplitudes:

Pulse wave with 50% duty-cycle

But as dcyc decreases from 50%, the negative peak amplitude also decreases. If dcyc is 33.3%, the negative peak amplitude is -0.540:

Pulse wave with 33.3% duty-cycle

If dcyc is 10%, the negative peak amplitude decreases even further to -0.167, rendering the resulting waveform almost unipolar:

Pulse wave with 10% duty cycle

Can anyone explain why this happens in the simplest way possible? How can one generate a bandlimited pulse wave with varying duty-cycle and with equal positive and negative peak amplitudes?

  • $\begingroup$ Looking at your pictures, it seems as if your code was generating waves with 0 offset no matter what and that the peak value was always 1. So this leads to negative peaks decreasing in absolute value. Are you modifying the mean value when you modify the duty cycle? If not, I believe that is the cause of your problem. $\endgroup$
    – Tendero
    Commented Jan 27, 2017 at 19:08
  • $\begingroup$ @Tandero Could you explain why generating waves with 0 offset and peak value of 1 leads to negative peaks? Also, what do you mean by "mean value" ? $\endgroup$
    – bzrr
    Commented Jan 27, 2017 at 19:18
  • 1
    $\begingroup$ By mean value I refer to the 0-frequency Fourier component. What I was thinking is that if this mean is 0 and the maximum is fixed, then varying the duty cycle will vary the minimum the wave reaches. $\endgroup$
    – Tendero
    Commented Jan 27, 2017 at 19:26
  • $\begingroup$ @Tendero You mean the average value of the signal? If so, how would I go about varying this mean value to keep the negative and positive peak amplitudes equal? $\endgroup$
    – bzrr
    Commented Jan 27, 2017 at 19:38
  • 2
    $\begingroup$ You should find the average value of the signal you would get if negative and positive peaks were equal and add it to the signal you currently have. $\endgroup$
    – Tendero
    Commented Jan 27, 2017 at 19:55

1 Answer 1


Essentially, you are evaluating the partial sum of a Fourier Series of the rectangular periodic pulse. The pulse is symmetric around $t=0$, so the sine-parts can be omitted, leaving only the cosine-parts, as you have shown. However, you miss an essential thing: the frequency zero (i.e. the DC part), which is the overall mean of your waveform.

I dont know exactly, how you came up with the different values for amp, but it turns out the waveform with dcyc=0.5 has an amplitude of approximated 0.78. So, the mean of a waveform with a given $d_{cyc}$ is given by


You need to add this mean value to your Fourier Series to generate the equal-amplitude waveform. See below:

length = 1024
nharms = 8
dcyc = 0.2
step = 2*np.pi/length
harm = 1

A = 0.78
mean = (dcyc) * A + (1-dcyc)*(-A)

j = np.arange(4*length)

table = 0
for i in range(nharms):
    amp = np.sin(harm*dcyc*np.pi)/harm
    table = table + amp*np.cos(step*harm*j)
    harm = harm + 1
table = table + mean

plt.plot(j, table);

enter image description here

  • $\begingroup$ You're right, I made an error when I copied the code over. double amp = sin(harm * dcyc) / harm; should be double amp = sin(harm * dcyclerad) / harm; I already edited the post. $\endgroup$
    – bzrr
    Commented Jan 27, 2017 at 20:24
  • $\begingroup$ I've edited my post with the explanation why your problem happens and how you would fix it. $\endgroup$ Commented Jan 27, 2017 at 20:43
  • $\begingroup$ A dcyc of 0.1 still yields a negative peak amplitude of -0.872. Is 0.2 the minimum duty cycle that can result in a pulse wave of equal peak amplitudes? Is it possible to maintain this property with duty cycles lower than 0.2? $\endgroup$
    – bzrr
    Commented Jan 27, 2017 at 21:32
  • $\begingroup$ I guess with dcyc=0.1 you do not have enough harmonics to accurately represent this short duty cycle. If I use e.g. 80 harmonics, I get equal amplitude for positive and negative amplitudes. $\endgroup$ Commented Jan 27, 2017 at 21:35
  • $\begingroup$ Is there a way to calculate the maximum deviation from dcyc=0.5 for n harmonics that would result in a pulse wave with equal peak amplitudes? $\endgroup$
    – bzrr
    Commented Jan 27, 2017 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.