Square wave with randomly varying frequency

17 次查看(过去 30 天)
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;

回答(2 个)

Dyuman Joshi
Dyuman Joshi 2024-1-17
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
VBBV 2024-3-30
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 个评论
VBBV
VBBV 2024-4-1
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;
Dyuman Joshi
Dyuman Joshi 2024-4-2
编辑:Dyuman Joshi 2024-4-3
@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.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by