Square wave with randomly varying frequency
16 views (last 30 days)
Show older comments
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;
0 Comments
Answers (2)
Dyuman Joshi
on 17 Jan 2024
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;
VBBV
on 30 Mar 2024
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
VBBV
on 1 Apr 2024
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
% 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;
Dyuman Joshi
on 2 Apr 2024
Edited: Dyuman Joshi
on 3 Apr 2024
@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.
See Also
Categories
Find more on Waveform Generation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!