Generating a Sin wave that changes frequency pseudo randomly after each period?

6 visualizzazioni (ultimi 30 giorni)
Im trying to program a sig gen that uses mathlad and was wondering if there is a way to generate a sin wave that changes frequency psuedo randomly after each period for a given lenght time? Is there a shortcut or does matlab have a library to accomplish this task?

Risposte (1)

Jacob Mathew
Jacob Mathew il 14 Ago 2024
Hey Minaam,
I understand that you want to generate a Sine wave that changes frequency pserdo randomly after each period. The following script showcases an example of achieving the same:
% Parameters
fs = 1000; % Sampling frequency (Hz)
t = 0:1/fs:10; % Time vector (10 seconds)
f_min = 1; % Minimum frequency (Hz)
f_max = 10; % Maximum frequency (Hz)
segment_duration = 1; % Duration of each frequency segment (seconds)
% Generate pseudo-random frequencies
num_segments = floor(length(t) / (segment_duration * fs));
frequencies = f_min + (f_max - f_min) * rand(1, num_segments);
% Create the sine wave with changing frequency
y = zeros(size(t));
for i = 1:num_segments
start_idx = (i-1) * segment_duration * fs + 1;
end_idx = i * segment_duration * fs;
if end_idx > length(t)
end_idx = length(t);
end
f = frequencies(i);
y(start_idx:end_idx) = sin(2 * pi * f * t(start_idx:end_idx));
end
% Plot the results
figure;
plot(t, y);
xlabel('Time (s)');
ylabel('Amplitude');
title('Sine Wave with Pseudo-Random Frequency Changes');
grid on;
% Plot the frequency changes
figure;
stairs((0:num_segments-1) * segment_duration, frequencies, 'r');
xlabel('Time (s)');
ylabel('Frequency (Hz)');
title('Pseudo-Random Frequency Changes');
grid on;
You can modify the parameters and observe how the sin wave changes. Depending on you use case requirements, you can use “interp1” or “pulstran” to generate time and frequency dependent sine waves and plot them. You can view their documentation using the following links:
A sample code of generating them using “interp1”:
% Parameters
fs = 1000; % Sampling frequency (Hz)
t = 0:1/fs:10; % Time vector (10 seconds)
f_min = 1; % Minimum frequency (Hz)
f_max = 10; % Maximum frequency (Hz)
segment_duration = 1; % Duration of each frequency segment (seconds)
% Generate pseudo-random frequencies
num_segments = floor(length(t) / (segment_duration * fs));
frequencies = f_min + (f_max - f_min) * rand(1, num_segments);
% Create a time vector for the frequency changes
t_freq = (0:num_segments-1) * segment_duration;
% Interpolate the frequencies to match the time vector `t`
interp_frequencies = interp1(t_freq, frequencies, t, 'previous');
% Generate the sine wave with changing frequency
y = sin(2 * pi * cumtrapz(t, interp_frequencies));
% Plot the results
figure;
plot(t, y);
xlabel('Time (s)');
ylabel('Amplitude');
title('Sine Wave with Pseudo-Random Frequency Changes');
grid on;
% Plot the frequency changes
figure;
stairs(t_freq, frequencies, 'r');
xlabel('Time (s)');
ylabel('Frequency (Hz)');
title('Pseudo-Random Frequency Changes');
grid on;

Categorie

Scopri di più su Measurements and Feature Extraction in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by