How to use circshift in Simulink?

3 次查看(过去 30 天)
I have a vector signal in Simulink that I want to transform so that each value is replaced by the average of that value and the subsequent value. My first idea was using a MATLAB function block with the following code:
function y = fcn(u)
y = (u+circshift(u,1))/2;
However, this somehow changes the vector to a matrix and leads to an error message:
I don't fully understand this behavior, but it is probably caused by reasons described in the answers to a previous question:
Are there other ways to perform this function using standard Simulink blocks?
  3 个评论
Walter Roberson
Walter Roberson 2025-4-3
The first thing I would wonder is whether u is a row vector or column vector; the distinction would make a difference to the circshift() call.
Bruce
Bruce 2025-4-3
编辑:Bruce 2025-4-3
"In other words you want to do a average using the current and past value so either you use a FIR filter , or simply use a delay / memory block to store the previous sample (so you are re-creating a 2 taps FIR filter) "
Actually, I want want the function to rearrange the vector signal within each iteration.
"The first thing I would wonder is whether u is a row vector or column vector; the distinction would make a difference to the circshift() call."
Thanks, that helped. u is a row vector, and the MATLAB function block seems to convert it to a column vector. I tested the function previously in MATLAB, and there it had no effect on the shape of the input vector. However, I found that the solution to my problem in Simulink is simply to invert y in the function:
function y = fcn(u)
y = ((u+circshift(u,1))/2)';

请先登录,再进行评论。

采纳的回答

Fangjun Jiang
Fangjun Jiang 2025-4-3
It is not really a matrix. I suggest debugging the MATLAB Function first. Provide a Constant vector input, e.g. (1:5), observe the output. Is that what you are expecting?
The error was caused by something else. There is no input data thus no dimension info. Look at the last error message and specify the dimension of the input signal of that Gain block.
  1 个评论
Bruce
Bruce 2025-4-3
Thanks, that helped. Using a simplified model, I found that the MATLAB function block converts the vector form from a row to a column vector. This happens regardless of the function it contains, for example, even if I simply set u = y. The solution is described above.

请先登录,再进行评论。

更多回答(0 个)

标签

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by