How to generate pulse signal in Simulink with pulse width input?
98 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/920129/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/920134/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/920139/image.png)
But with no luck either. I get the following error message:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/920144/image.png)
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 Commento
vidyesh
il 1 Nov 2023
Declare a variable in the Base Workspace and then pass that variable to Pulse Width in Pulse Generator's Block Parameters.
Risposte (1)
Yash
il 10 Gen 2024
Modificato: Yash
il 10 Gen 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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1587421/image.png)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1587426/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1587441/image.png)
Hope this helps!
0 Commenti
Vedere anche
Categorie
Scopri di più su Sources 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!