필터 지우기
필터 지우기

Square wave with randomly varying frequency

조회 수: 18 (최근 30일)
Nandhini
Nandhini 2024년 1월 17일
댓글: Dyuman Joshi 2024년 4월 19일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Wind Power에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by