1

Consider a block scheme where there is a feedback path and the feedback signal is filtered by some kind of filter.

How can I implement this in Matlab (not Simulink)? My doubt is particularly about the filter: what interval of the signal should I choose to perform the filter?

For example if the filter has n coefficients, is it enough to filter the interval of the signal from i_n to i where i is the current iteration step?

I would like to replicate the behaviour of Simulink for the feedback loop but I found there is no way to export a simulink model to a Matlab script.

roschach
  • 8,390
  • 14
  • 74
  • 124
  • What type of filter do you use? is it the `filter` core function of MATLAB, or something fancier? –  Jul 24 '14 at 16:16
  • Is it possible you provide any example code, or pseudo-code? Your question seems like making a feedback that depends on results in previous iterations. In this case you simply hold a variable, and use and then update it in every iteration. – Yvon Jul 24 '14 at 16:18

2 Answers2

2

To your first question, the interval is depending on your sampling time. However, to implement the actual simulink behavior, that also depends on your solver options. Of course, it would be easy to implement an fixed-step solver than variable step one.

Now let's consider a simplest feedback amplifier as

In--->----Add--->----Gain--->---Out
           |           |
           ^           V
           |--filter---|

If filter has n coefficients, you will need memory to retain the previous input (assume an FIR filter). This is where persistent variable comes in handy. So your call may look like:

%% Sample Pseudocode. Do not run it until you fully understand.

function xo = feedbackTest(xi)
n = numel(xi);
xo = zeros(n,1);
for idx = 1:n // feed through and feedback
    xo(idx) = fsystem(xi(idx)) + xi(idx); 
end
%% ---------- filter ---------
function out = fsystem(in)
% y = a1[n] + a2[n-1] + a3[n-2]; // you need to insert your coefficients 
persistent x1 x2 x3; // retain previous inputs
if isempty(x2) x2 = 0; end
if isempty(x1) x1 = 0; end
x3 = x2;
x2 = x1;
x1 = in;
out = a1*x1 - a2*x2 + a3*x3;

And of course, the above example is just a demo for showing it's possible to design feedback loop using script/function. For a very complex system, it's still possible to implement but will be very difficult to put without using Simulink. You would very likely spend most of the time designing your filter modules instead of doing actual control mechanism.

[Edit]

For IIR filter such as Butterworth, that will involve feedforward path in your filters/system. However, the implementation concept is similar as shown in fsystem(). It will be better to write a function that generate appropriate coefficients in according to your cutoffs and ripples (assume you know the equations), and then apply your filter in the system. That you don't have to hard-coding coefficients every time when you need a new filter in your entire model.

Hopefully, that answers your question.

Y. Chang
  • 595
  • 1
  • 5
  • 18
  • Thanks a lot: your answer has been very helpful and clear. Just one question the variable you called n and defined as numel(xi) it is not the number of filter coefficient, is it? Plus if it is a IIR filter I just add other persistent variables corresponding to the previous "old" values right? – roschach Jul 25 '14 at 10:08
  • `n` is the number of your input signal. What main function does is that taking signal and feed through your system. You need to specify the order and coefficient in your system functions. And you are right, if designing IIR filter, you may add more persistent variables (or use array,cell in your var) for storing more "old" values. – Y. Chang Jul 25 '14 at 13:17
  • Thanks a lot again for the clear explanation. It seems to work. – roschach Jul 25 '14 at 13:46
0

If you have the Control System Toolbox, you can represent your filter and other components of your system as transfer function or state-space objects, and use functions like feedback to connect them. Once you have represented your entire system with feedback loops, you can use functions like lsim to simulate the time response of the system for a particular input.

Have a look at Creating and Manipulating Linear Models for a very basic example.

am304
  • 13,758
  • 2
  • 22
  • 40
  • Ya, control system toolbox is very handy IF you have it. But just to be clarified, I think it doesn't mean you cannot design your control system without it although it's gonna be very very hard. – Y. Chang Jul 25 '14 at 14:36