How can I introduce a continuos signal to a MATLAB Function block so I can get a continuos output.
My MATLAB Function block will be this:
function y = fcn(u)
y = 2*exp(-u);
So I can get a negative exponential, this because I need a control voltage source with a negative exponential signal. I need to introduce a controlled voltage source a exponential signal, is there other way?
Thanks
First of all, you don't need a MATLAB function to do that: take your input signal, multiply it by -1 with a Gain block, then use a Math Function block set to exp, and finally another Gain block to multiply it by 2.
Second, your input signal can be whatever you want. For example, you can use a Sine Wave block, or choose whatever block you want from the Sources library. If you leave the Sample Time parameter to 0, you will have a "continuous" signal (in the Simulink sense of the word), see Specify Sample Time in the documentation for more details. You can also use your own data from the MATLAB workspace using a From Workspace block.
Related
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?
Simulink model 1
Pulse generator block parameters
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:
Simulink model 2
But with no luck either. I get the following error message:
Simulink error messages
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?
I need to use resample() function to take a variable argument of Q downsampling factor in Simulink. Basically a Simulink fcn block containing this code:
function y = resample(data,Q)
y=resample(data,1000,Q);
On desktop simulation I can get variable Q to work as argument by specifying it as input to a MATLAB interpreted function, but since I need to generate a C code,my only option is to use the fcn block, obviously it won't compile due to above limitation.
error: the downsample factor Q must be constant
I understand this is a documented limitation of the resample function:
resample: The upsampling and downsampling factors must be specified as
constants. Expressions or variables are allowed if their values do not
change.
Any workaround or different approach to address this? Perhaps other block which is capable of doing the same job? ofc it has to be compatible with Simulink coder.
Thanks!
resample function would need to design filters and decide output sizes based on the sample factors. After code generation this cannot be changed, which is why this function needs the sampling factors to be constant.
But if the different downsampling factor values you need to support are limited you could use conditional branching with calls to resample in each branch with constant values. For example,
% Declare out as a var-size with max decided by the minimum downsampling factor
% Assuming data is [1000, 1]
coder.varsize('out', [500 1]);
out = zeros(500,1);
if Q == 2
out = resample(data,1000,2);
elseif Q == 4
out = resample(data,1000,4);
elseif ...
...
end
You also need to deal with variable sized data "out" in the rest of your MATLAB code and Simulink model if this is an output variable from MATLAB Function block.
Time dependent signal is input to a Simulink model which give time dependent output, both are continuous functions, we can separately plot input/output signal values as a function of time but we want to plot input value with output value for a particular time. Does Simscape have block for that? please help. Thank you in advance.
The xygraph block allows you to input a signal for both, the x and the y axis.
I am trying to acquire the frequency information about a displacement signal(e.g., Vx) while the simulation is running.
My idea was to make use of the fft command supported by Embedded Matlab Function block.
The first thing which i performed was to store the values of displacement signal 'Vx' in a buffer of length 'L'.
The second thing is to compute the fft of those values stored in the buffer and calculate the index value corresponding to the maximum amplitude.
The third thing is to acquire the frequency from index value, sampling frequency and length of the buffer.
Embedded Matlab code is the following:-
Function[freq_Vx,buffero_Vx] = fcn(Vx,bufferi_Vx)
% This block supports the Embedded MATLAB subset.
% See the help menu for details.
buffo_Vx = [Vx;buffi_Vx(1:end-1)]; % buffer which stores the values of signal 'Vx'
Fs = 2000;
nfft = 2^nextpow2(length(buffo_Vx));
[max_Vx,index_Vx] = max(abs(fft(buffo_Vx,nfft)));
freq_Vx = index_Vx*Fs/length(buffo_Vx);
end
Is this the right way of acquiring the frequency content of a signal while the simulation is running?
I believe your approach to processing the data "real time" in the model is reasonable, however I believe the index_Vx*Fs/length(buffo_Vx) is not going to give the desired results and buffo_Vx = [Vx;buffi_Vx(1:end-1)]; will likely need to be buffo_Vx = [Vx;bufferi_Vx(1:end-1)];` Checkout this link for your frequency conversion.
For diagnostic purposes, check out the Simulink Extras -> Additional Sinks blocks on the Simulink library browser.
The Spectral densities should be helpful.
i want to show the signal modulate in simulink but i don't know how to make it
The Spectrum Scope calculates the FFT of the input signal.
In your model you are trying to take the fft of the fft.
(Although you aren't quite doing that as the input to the FFT block needs to be buffered so that the FFT has a vector of data over which to calculate the FFT.)
Remove your FFT and Abs blocks and feed the signal directly into the Spectrum Scope.
Use the block's dialog to set the buffer and FFT length that you want to use.
Phil.