Sine wave with variable frequency

I would like to create a sine wave with frequency changing every 2*T. Can someone help please.

 採用された回答

Manikanta Aditya
Manikanta Aditya 2024 年 12 月 27 日

0 投票

You can create a sine wave with a variable frequency in MATLAB by defining a time vector and then adjusting the frequency at intervals of 2*T. Here's an example of how you can achieve this:
% Define parameters
T = 1; % Base period
fs = 1000; % Sampling frequency
t = 0:1/fs:10*T; % Time vector for 10 periods
% Initialize the sine wave
y = zeros(size(t));
% Define the frequency change interval
interval = 2 * T;
% Loop through the time vector and change frequency every 2*T
for i = 1:length(t)
if mod(t(i), interval) < T
f = 1; % Frequency for the first T seconds
else
f = 2; % Frequency for the next T seconds
end
y(i) = sin(2 * pi * f * t(i));
end
% Plot the sine wave
figure;
plot(t, y);
xlabel('Time (s)');
ylabel('Amplitude');
title('Sine Wave with Variable Frequency');
grid on;
You can adjust the frequencies and the interval as needed. This code will generate a sine wave where the frequency changes every 2*T seconds.
I hope this will help you understand better, Thanks!

3 件のコメント

Alhassane
Alhassane 2024 年 12 月 27 日
Thanks for your answer. I needed to change at every 2*T of time the frequency from 1 to 800k. Can you help with that?
Manikanta Aditya
Manikanta Aditya 2024 年 12 月 27 日
Check this:
% Define parameters
T = 1; % Base period
fs = 1000000; % Sampling frequency (1 MHz) to accommodate high frequency
t = 0:1/fs:10*T; % Time vector for 10 periods
% Initialize the sine wave
y = zeros(size(t));
% Define the frequency change interval
interval = 2 * T;
% Loop through the time vector and change frequency every 2*T
for i = 1:length(t)
if mod(t(i), interval) < T
f = 1; % Frequency for the first T seconds
else
f = 800000; % Frequency for the next T seconds
end
y(i) = sin(2 * pi * f * t(i));
end
% Plot the sine wave
figure;
plot(t, y);
xlabel('Time (s)');
ylabel('Amplitude');
title('Sine Wave with Variable Frequency');
grid on;
Alhassane
Alhassane 2024 年 12 月 27 日
Thank you Manikanta Aditya.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2023a

質問済み:

2024 年 12 月 27 日

コメント済み:

2024 年 12 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by