How to generate pulse signal in Simulink with pulse width input?
112 views (last 30 days)
Show older comments
I would like to drive a transfer function block with a pulse signal in Simulink. This can for example be done with a Pulse Generator block. The only thing I am missing is an external input where I can specify the pulse width or duty cycle of the signal. Signal parameters can only be entered through the Pulse Generator's Block parameters. Is there a way to go around this?
Alternatively, I tried to use a Matlab function block inside Simulink with the Matlab function "square", which takes a few constants as well as the current simulation time as inputs. I used the following function:
% Output data value for specific time instant
function DataValue = pulsegen(PulseWidth,Frequency,TimeInput)
PulseDutyCycle = PulseWidth*Frequency*100;
% Generate signal
t = 0:0.0001:60;
y = square(2*pi*Frequency*t,PulseDutyCycle);
y = max(y,0); % Set nonzero elements to zero
DataValue = y(find(t==TimeInput)); % find data value at specific time instance
end
And the following model:
But with no luck either. I get the following error message:
Which does not makes sense to me. When I test this function stand-alone in Matlab, the function output is just a 1x1 constant value, not an array. Does anyone know a solution to this?
1 Comment
vidyesh
on 1 Nov 2023
Declare a variable in the Base Workspace and then pass that variable to Pulse Width in Pulse Generator's Block Parameters.
Answers (1)
Yash
on 10 Jan 2024
Edited: Yash
on 10 Jan 2024
Hi Pelle,
I understand that you are looking to feed variable inputs to the "Pulse Generator Block" in Simulink. While you could use a "MATLAB Function" block for this purpose, it's advisable to employ the dedicated "Pulse Generator Block" for such tasks.
To incorporate variable parameters within the block, they must be properly declared. Here are two methods you could consider:
1. Using the Base workspace: In this method, you need to first declare all the variables in base workspace and then use them in the block parameters. See the below picture for the reference:
However, using this approach, you would interfere with the base workspace which might cause issues if you are running multiple MATLAB programs.
2. Using the model workspace: This approach is generally preferable as it avoids conflicts with the base workspace. For this, you need to open the "Model Explorer" through the Modeling tab in Simulink. After that, select Model Workspace and add variables through the toolbar at the top.
You can also define a data dictionary for this, but using model workspace would be the better in your case. For further guidance, refer to the MATLAB documentation at the following link: https://www.mathworks.com/help/simulink/ug/create-edit-and-manage-workspace-variables.html
You can have a look at the below picture for reference:
Hope this helps!
0 Comments
See Also
Categories
Find more on Sources in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!