From my assumption, you are trying to generate a square wave which looks like in the below image, when length_of_wave is 100, num_cycle is 10 and dutycycle is 0.75
In this case your code throws an error because the value of duty_cycle * length_of_wave/num_cycle is not an integer and you are trying to pass this non-integer value to "zeros" function. Hence the error.
You can make use of the following piece of code
function waveform = squareWave(length_of_wave, num_cycle, duty_cycle)
oneCycleLength = length_of_wave/num_cycle;
t=1:oneCycleLength;
oneCycle(t)=0;
oneCycle(oneCycleLength*duty_cycle+1:end) = 1;
waveform = repmat(oneCycle, [1,num_cycle]);
plot(waveform)
end
In the above code, you have to handle the case when length_of_wave = num_cycle
Best Answer