Square wave with randomly varying frequency
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
Dyuman Joshi
am 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;
1 Kommentar
VBBV
am 30 Mär. 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 Kommentare
VBBV
am 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
am 2 Apr. 2024
Bearbeitet: Dyuman Joshi
am 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.
Siehe auch
Kategorien
Mehr zu Waveform Generation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!