How to use Convolution in Simulink?
8 views (last 30 days)
Show older comments
Hi, I make the block using Convolution funtion, but the results was not I wanted.
The program is below.
function y = fcn(u1, u2)
y=conv(u1,u2);
The u1 and u2 both are input which has [1001×1] matrix in total 10s simulatin time.
I want to solve it as below.
which n=1001.
However, in Simulink, Only product calculation for each sample time is executed.
Just like
So the results is same as just use produc function.
How to make the program with the first formula in Simulink?
4 Comments
Paul
on 2 Jul 2022
So the goal is to have a Simulink model that computes the convolution integral of u1(t) and u2(t) as a function of time?
The correct form of the convolution integral is
Is there an expression for u2(t)?
Answers (2)
Andy Bartlett
on 1 Jul 2022
You can quickly model this BEHAVIOR using the
two Tapped Delay blocks
one Dot Product block
I emphasized BEHAVIOR because you will be doing 1001 multiplies at every time step and 1000 additions at every time step.
But at the first time step, 1000 of the multiplications will involve multiplication by zero, so output at the first time step will behave like the
w(1) = u(1) * v(1)
you are seeking, but it will be implemented as
w(1) = u(1) * v(1) + 0 * 0 + 0 * 0 + ... + 0 * 0
the zeros come initial conditions of the tapped delay blocks.
At the second time step, 999 multiplications will be zeros, and so on.
Note with the Tapped Delay, you'll want one configured as Oldest first and the other as Newest first.
I think you'll also want to check "Include current input in output vector"
3 Comments
Andy Bartlett
on 1 Jul 2022
Edited: Andy Bartlett
on 1 Jul 2022
Signal's Value Now vs Time History
Signals in Simulink have a value at any given time step. Let's call that the "now" value. If you connect a Display block to a signal in Simulink and use the "Step Forward" debugging, you'll see the signal's "now" value at just the current time step. At the end of a Simulation, the Display block will show the value from the last time step.
The To Workspace block is all about creating a time history of those "now" values and writing the history to the MATLAB workspace. So it's output to the MATLAB workspace is stacking up
Signal foo at first time step
Signal foo at second time step
...
Signal foo at last time step
Clearly, the "now" values and the time history are tied together. But it can be confusing to think about both simultaneously.
When designing a Simulink model, it is typically simplest to think about the "now" values. For example, if input signal Foo's "now" value is X and if input signal Bar's "now" value is Y, then what blocks do I combine and configure to get the final output to have the desired "now" value of Z.
I recommend you follow that pattern and focus on what the Display blocks show for the signals at a given time step.
In my experience, To Workspace is great for batch testing after I've used "now" thinking to figure out the design details.
Start with Simplified Case
I suggest you setup a simplified version of the problem that will be easy to debug and analyze.
For example, instead of n = 1001, first play around with n = 3.
And let's use some values that make it easy to "eyeball" correctness.
For example, let's suppose the "now" output of two Tapped Delays were
u = 2:4
v = 10.^(0:2)
u =
2 3 4
v =
1 10 100
Then the "now" value of conv function would be the following
y1 = conv(u,v)
y1 =
2 23 234 340 400
Is that what you are seeking for the "now" output?
Or perhaps you wanted either u or y flipped.
y2 = conv(u,fliplr(v))
y2 =
200 320 432 43 4
y3 = conv(fliplr(u),v)
y3 =
4 43 432 320 200
Once you're fully clear on the desired "now" values, craft a model to make it easy to test and explore.
Easy exploration model
You can use two
Repeating Sequence Stair blocks
to feed in the time series values.
Set the sample times to 1 and set the simulation stop time to 2 or higher.
For example, in the second Repeating Sequence Stair block set the
Vector of Output Values
to
v = 10.^(0:2)
v =
1 10 100
To understand what this block does connect a Display block to it's output and on the model's toolstrip click "Step Forward" button three times. On the first time the value will be 1. On the next two time steps, the Display block will show 10 and 100, respectively.
In the other Repeating Sequence Stair block, put
u = 2:4
u =
2 3 4
Now feed those outputs to Tapped Delay blocks and put Display blocks on their outputs. Use "Step Forward" button to verify those outputs are what you expect.
Finally, use Display block connected to the output of the Conv block and use "Step Forward" to confirm the desired "now" values are obtain.
If it is not what you want, you can easily experiment with alternate approaches and use Display blocks and the "Step Forward" button to see what is being done right and wrong.
As an experiment example, let's consider your last statement
"I want w(3)=u1(1)*v(3)+u(2)v(2)+u(3)*v(1)"
which sounds like you want
y2 = conv( u, fliplr(v))
To get that "fliplr" action, you can change Tapped Delay block settings
Order output vector starting with: Oldest or Newest
You can easily experiment with the four combinations to see which is right for you. For example, try Oldest for the Tapped Delay of u and Newest for the Tapped Delay of v as one of the four combinations.
If none of those are right, then that's where the beauty of the simple model for exploration comes in. You could easly try different things like replacing Tapped Delay with the Buffer block or replacing Conv with Dot Product or various other approaches. Using Display blocks and the "Step Forward" Simlink debugger button will allow you to better understand what each piece is doing and if the overall approach is working. That should allow you to fairly quickly discover a great approach for your needs.
Paul
on 2 Jul 2022
I think this model does what you're looking for.
Here is the model that uses the convolution sum to approximate the convolution integral of u1(t) = 1 and u2(t) = exp(-t). I used those u1(t) and u2(t) because I know what the answer should be and so could check it.
The sampe Time is Ts = 0.01.
The fcn code is:
function y = fcn(u1,u2)
persistent n
if isempty(n)
n = -1;
end
n = n + 1;
y = sum(u1(end-n:end).*fliplr(u2(end-n:end)));
end
And the output is:
which is y(t) = 1 - exp(-t).
There may be simpler and/or better ways to do this.
Also, the model will have problems if the number of delays in the Tapped Delay is too small relative to Tfinal/Ts.
If I may ask, what is the application that needs to compute the convolution integral in simulation?
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!