Since we don't see buffer allocation in the provided code, I would assuming that the buffer has sufficient size to hold all samples (otherwise buffer overflow could cause all sorts of undefined behaviors).
With the code provided, you should realize that in the loops, the expressions
if( z < fmodf(squareIndex, theta_increment)/theta_increment < y) {
are evaluated from left to right as:
if( (z < fmodf(squareIndex, theta_increment)/theta_increment) < y) {
Let's look at the second loop to illustrate the effect:
so long as squareIndex is less than
0.8*theta_increment`, the subexpression
(z < fmodf(squareIndex, theta_increment)/theta_increment)
evaluates to false
which, after numerical promotion, is less than y=0.9
, so the overall expression is true
(so k=0
). Once squareIndex
becomes more than 0.8*theta_increment
(z < fmodf(squareIndex, theta_increment)/theta_increment)
becomes true
which, again after numeric promotion, is more than y=0.9
, so the overall expression becomes false
(so k=1
).
The loops then generate the following curves:
where from top to bottom you have the first, second and third loop, followed by the combined waveform.
To fix this you can change the conditions to:
float t = fmodf(squareIndex, theta_increment)/theta_increment;
if (z < t && t < y) {
k = 1.0;
} else {
k = 0.0;
}
Which should then generate the following waveform: