Engee documentation
Notebook

Creating a rectangular wave from the sum of sinusoids

This example shows how to make a rectangular signal from the sum of sine waves by adding them one after the other, with parameters that can be obtained using Fourier expansion (its odd part).

Summing sinusoids

Create a vector varying from 0 to 10 in 0.1 increments, and take the sine from each of the values in that vector. Plot the fundamental harmonic component of our signal.

In [ ]:
t = 0:.1:10;
y = sin.(t);
plot( t, y )
Out[0]:

Let's add a third harmonic to the main harmonic and plot the graph.

In [ ]:
y = sin.(t) + sin.( 3t )./3;
plot(t,y)
Out[0]:

Now let all odd harmonics up to the ninth harmonic appear on the graph.

In [ ]:
y = @. sin(t) + sin(3t)/3 + sin(5t)/5 + sin(7t)/7 + sin(9t)/9;
plot(t,y)
Out[0]:

We used the operator @. not to add the vectorisation indicator . to each function separately, but instead to make the whole expression vectorised.

At the end of the demonstration, we will plot several graphs in which the sum of odd harmonics will gradually accumulate and we will reach the 19th component.

In [ ]:
t = 0:0.02:3.14;
y = zeros(10,length(t));
x = zeros(size(t));
for k = 1:2:19
   x = x .+ sin.(k*t)./k;
   y[Int32((k+1)/2),:] = x;
end
plot( y[1:2:9,:]', leg=false,
    title="Накопление синусоид и приближение к прямоугольной функции")
Out[0]:

The behaviour of the sum of the harmonic series in the vicinity of the breaking point is sometimes called the Gibbs effect. According to the observed effect, adding a large number of sinusoids will result in the excess of the desired signal level at the break point tending to 9%, although the integral of the error will tend to zero.

Conclusion

We have studied a small example that allows us to come to an understanding of how the Fourier transform works and where some types of interference come from when transmitting pulsed signals.