How do I create a single square wave of variable duty cycle and length.

22 views (last 30 days)
I tried a piece of code for developing a single square wave which consists of user input "duty cycle and length" but there are few inputs when the code doesn't function and returns an error.
function waveform = squareWave(duty_cycle,length_of_wave)
duty_cycle = duty_cycle * length_of_wave; %introduces duty cycle for positive edge
inv_duty = length_of_wave - duty_cycle; %introduces duty cycle for negative edge
oneCycle = [ones(1,duty_cycle),zeros(1,inv_duty)]; % one square wave form
end
This piece works fine and perfectly.
but when i wish to repeat this or apply a recursion to this at certain values it throws in errors.
function waveform = squareWave(length_of_wave,num_cycle, duty_cycle)
duty_cycle = duty_cycle * length_of_wave/num_cycle; %introduces duty cycle for positive edge
inv_duty = length_of_wave/num_cycle - duty_cycle; %introduces duty cycle for negative edge
oneCycle = [ones(1,duty_cycle),zeros(1,inv_duty)]; % one square wave form
waveform = repmat(oneCycle, [1,num_cycle]); %repeating "oneCycle" "num_cycle" times
end
  1 Comment
Krut Mehta
Krut Mehta on 12 Jun 2020
Thank you for your input towards my question. When I tried running your snippet the expected output was completely reversed
function waveform = squareWave(length_of_wave,num_cycle, duty_cycle)
duty_cycle = duty_cycle * length_of_wave/num_cycle; %introduces duty cycle for positive edge
inv_duty = length_of_wave/num_cycle - duty_cycle; %introduces duty cycle for negative edge
oneCycle = [ones(1,duty_cycle),zeros(1,inv_duty)]; % one square wave form
waveform = repmat(oneCycle, [1,num_cycle]); %repeating "oneCycle" "num_cycle" times
end
%the expected output for the data squareWave(10,2,0.2) is
% 1 1 0 0 0 1 1 0 0 0
but when I ran your snippet it inculcated the decimal value and took it as an integer but it completely reversed the output
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
% the actual output given by this code for the same data squareWave(10,2,0.2) is
% 0 0 1 1 1 0 0 1 1 1
Thanks again for your Inputs

Sign in to comment.

Accepted Answer

Chidvi Modala
Chidvi Modala on 12 Jun 2020
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

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!