Timeseries Waveform Generation Question

60 views (last 30 days)
Adeel Saleem
Adeel Saleem on 4 Nov 2024 at 15:15
Commented: Adeel Saleem on 8 Nov 2024 at 9:11
I am attempting to create a timing model using MATLAB and SImulink and would like some help from the knowledgeable people on here.
I am trying to to create a signal waveform with many signals all with the same start and end times but with varying steps(0 or 1) and delays. My signals and delays are all defined in a MATLAB script. I am creating a timeseries with the range of time (0 to 1800ns). I have known steps for the signal from 0>1 and 1>0 using the following code:
start_time = 0;
end_time = 1800;
num_points = 1000;
delay1 = 40e-9
% create time vector
time_vector = linspace(start_time, End_time,num_points)
time_vector_ns = time_vector *1e-9
%Timeseries Signal
signal1 = timeseries(0,time_vector_ns) % empty time series data with time 0 to 1800 in steps
signal1_mask = timeseries([0 1 0 1 0], [delay1 160e-9 425e-9 1160e-9 1400e-9]);
for yy =1:length(signal1_mask.Time) %identify how many values are in the transitions for the signal
if signal1.Time(yy,:) = signal1_mask.TIme(yy) % if the time step for the five times in the mask match the value in the long 0 to 1800ns timestep then move on to append data
signal1.Data(yy) = signal1_mask(yy) %Take data from the matching timestep and add it to the empty timeseries
end
end
Is this going along the right path to programmatically define the data in this signal to define the on/off times like this:
0->delay from variable = '0';
delay from variable ->hold time variable = '1'
hold end -> 1800ns ->'0'
I will have to do this for many signals that are dependant on each other. So that's why I am trying to use variables to define the delays since it would be easy to change one and have the rest of the model update (offset) by the correct amount. The other option is to draw out each signal in Simulink signal editor but that's seems laborious and also not my required use case to have it configurable on variable delays.
Any help would be much appreciated. I have hopefully given enough context.

Accepted Answer

Shubham
Shubham on 8 Nov 2024 at 8:32
Hi Adeel,
To generate a series of time-dependent waveforms in MATLAB with transitions between 0 and 1 at specified times, you can use the timeseries object. The requirements for creating the waveform are as follows:
  • The waveform starts and ends at 0.
  • Transitions occur at specified times.
  • The approach is scalable for multiple signals with different delays and hold times.
Here's the MATLAB script to that demonstrates how to create such a waveform:
% Define parameters
start_time = 0; % in ns
end_time = 1800; % in ns
num_points = 1000;
delay1 = 40; % in ns
% Create time vector
time_vector = linspace(start_time, end_time, num_points);
% Define the mask for signal transitions
signal1_mask_times = [delay1, 160, 425, 1160, 1400]; % in ns
signal1_mask_values = [0, 1, 0, 1, 0];
% Initialize the signal data with zeros
signal1_data = zeros(1, num_points); % Ensure this is a 1D array
% Assign values based on mask
for idx = 1:length(signal1_mask_times)
% Find the closest time index in the time_vector
[~, closest_idx] = min(abs(time_vector - signal1_mask_times(idx)));
% If it's not the last transition, update up to the next transition time
if idx < length(signal1_mask_times)
% Find the next transition index
[~, next_idx] = min(abs(time_vector - signal1_mask_times(idx + 1)));
% Update the signal data between the current and next transition
signal1_data(closest_idx:next_idx-1) = signal1_mask_values(idx);
else
% For the last transition, update until the end
signal1_data(closest_idx:end) = signal1_mask_values(idx);
end
end
% Check the shape of the data
disp(size(signal1_data)); % Should be [1, num_points]
1 1000
% Create the timeseries object
signal1 = timeseries(signal1_data(:), time_vector * 1e-9); % Flatten to ensure 1D
% Plot the signal to verify
figure;
plot(signal1.Time, signal1.Data);
xlabel('Time (s)');
ylabel('Amplitude');
title('Generated Signal Waveform');
grid on;
For more details on timeseries, refer to the following documentation link:
Hope this helps.
  1 Comment
Adeel Saleem
Adeel Saleem on 8 Nov 2024 at 9:11
Hi. Thanks for your response. It does the job perfectly. I did solve the issue partly but not as cleanly as this by using a timetable object where I would define the mask and assign values as you have done. I like your method of updating the transitions.
Thanks a lot!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!