Square wave with randomly varying frequency

This is the code of a square wave with 200 khz frequency.. The frequency should be varied randomly... The variation in the frequency range is between 266.6 khz to 133.3 khz. This is my condition.. while trying this code the square wave is not coming properly it seems like a traiangle wave.. i need a proper square.
min_freq = 133333;
max_freq = 266666;
duty_cycle = 50;
t = 0: 0.0000025:1;
Amplitude = 1;
% Generate a random frequency within the specified range
frequency = min_freq + rand(1, length(t)) .* (max_freq - min_freq);
% Generate a square wave with the random frequency and duty cycle
square_wave = Amplitude*square(2 * pi * frequency .* t, duty_cycle);
% Plot the square wave
plot(t, square_wave);
axis([0 0.005 -2 2 ]);
grid on;

Answers (2)

The increment in time vector too small to clearly resolve the output wave-form.
You can either zoom into parts of wave form to resolve it more clealry i.e. by decreasing the x-limits, or you can increase the increment.
min_freq = 133333;
max_freq = 266666;
duty_cycle = 50;
t = 0: 0.005:1;
Amplitude = 1;
% Generate a random frequency within the specified range
frequency = min_freq + rand(1, length(t)) .* (max_freq - min_freq);
% Generate a square wave with the random frequency and duty cycle
square_wave = Amplitude*square(2 * pi * frequency .* t, duty_cycle);
% Plot the square wave
plot(t, square_wave);
axis([0 0.5 -2 2 ]);
grid on;
To make it appear square, you need to delete the 2*pi part in the square function, and use randi instead of rand for scalar frequency generation
min_freq = 133333;
max_freq = 266666;
duty_cycle = 50;
t = 0: 0.0025:10; % increase the step size
Amplitude = 1;
% Generate a random frequency within the specified range
frequency = min_freq + randi([0 1])*(max_freq-min_freq); % use randi function
% Generate a square wave with the random frequency and duty cycle
square_wave = Amplitude*square(frequency .* t, duty_cycle); % delete the 2*pi
% Plot the square wave
plot(t, square_wave);
axis([0 1 -2 2 ]);
grid on;

4 Comments

"use randi instead of rand for scalar frequency generation"
@VBBV, Any particular reason why?
The question does mention that the frequency must be varied randomly. However, it is same in the solution you have provided.
This will make the entire waveform either max_freq or min_freq. For each execution of the script, frequency is constant, not varied.
No specific reason that randi needs to be used, except for frequency as a random whole number instead of arbitrary decimal number.
min_freq = 133333;
max_freq = 266666;
duty_cycle = 50;
t = 0: 0.005:10; % increase the step size
Amplitude = 1;
% Generate a random frequency within the specified range
frequency = min_freq + rand*(max_freq-min_freq) % use randi function
frequency = 2.2482e+05
% Generate a square wave with the random frequency and duty cycle
square_wave = Amplitude*square(frequency .* t, duty_cycle); % delete the 2*pi
% Plot the square wave
plot(t, square_wave);
axis([0 1 -2 2 ]);
grid on;
@VBBV, Alright, but that still does not satisfy OP's requirement of variable frequency, whereas it is constant in your solution, as has been pointed out earlier.

Sign in to comment.

Asked:

on 17 Jan 2024

Commented:

on 19 Apr 2024

Community Treasure Hunt

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

Start Hunting!