# Pulse wave from sum of sinusoids

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: But as dcyc decreases from 50%, the negative peak amplitude also decreases. If dcyc is 33.3%, the negative peak amplitude is -0.540: If dcyc is 10%, the negative peak amplitude decreases even further to -0.167, rendering the resulting waveform almost unipolar: 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?

• 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. – Tendero Jan 27 '17 at 19:08
• @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" ? – Jovito Jan 27 '17 at 19:18
• 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. – Tendero Jan 27 '17 at 19:26
• @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? – Jovito Jan 27 '17 at 19:38
• 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. – Tendero Jan 27 '17 at 19:55

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

$$a_0=0.78d_{cyc}+(1-d_{cyc})(-0.78)$$

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);
plt.grid(True) • 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. – Jovito Jan 27 '17 at 20:24
• I've edited my post with the explanation why your problem happens and how you would fix it. – Maximilian Matthé Jan 27 '17 at 20:43
• 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? – Jovito Jan 27 '17 at 21:32
• 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. – Maximilian Matthé Jan 27 '17 at 21:35
• 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? – Jovito Jan 27 '17 at 21:46