From control systems perspective, I have two subsystems that are connected in a cascade structure. An outer loop represents a joystick that provides position that runs at 1kHz and an inner loop represents controller that runs at 30Hz. Although the desired input is generated from the outer loop, the actual controller runs within the inner loop. As a result of the significant difference in loop rates, the inner loop may suddenly see a big change in the desired input. I don't want that to happen. Within the outer loop, I would like to design a low pass filter to attenuate abrupt changes above 30Hz. What is the best way to design such a filter and ultimately implement it digitally using Matlab or C++?

According to Low-pass filter Wikipedia, it seems the filter is designed as follows: $$ y[n] = \alpha x[n] + (1-\alpha)y[n-1] $$ where $$\alpha = \frac{1}{1+ f_\mathrm{s}RC} \qquad \text{ and } \qquad RC=\frac{1}{2\pi f_\mathrm{c}}$$ and $f_\mathrm{s}$ is the sample rate. It seems to me, $f_\mathrm{c}=$ 30 Hz in my case; but I didn't understand this part

This equation can be discretized. For simplicity, assume that samples of the input and output are taken at evenly spaced points in time separated by $\frac{1}{f_\mathrm{s}}$ time.*

Each loop has its own step size (i.e. outer loop with 0.001 and the inner loop with 0.033). Which step size should I choose?

  • $\begingroup$ What does it mean to have a [outer/inner/whatever] loop running at some specified frequency? What is the loop? $\endgroup$ Commented Oct 8, 2023 at 4:53
  • $\begingroup$ @robertbristow-johnson sorry for not being lucid enough. I will update it now. $\endgroup$
    – CroCo
    Commented Oct 8, 2023 at 4:54
  • $\begingroup$ So you're saying that these two discrete-time control systems with feedback loops are operating at different sample rates? How are these two control systems interacting? $\endgroup$ Commented Oct 8, 2023 at 17:26
  • 1
    $\begingroup$ My edits were to change the symbols and nomenclature to be consistent with the DSP subdiscipline within the EE discipline. I hope that's okay with you. If not, just revert it. in this change $$ f_\mathrm{s} \triangleq \frac{1}{\Delta_T} $$ which is the sample rate. $\endgroup$ Commented Oct 8, 2023 at 17:35

1 Answer 1


There are dozens of methods to design lowpass filters: Filter design is a complicated tradeoff between steepness, pass-band ripple, stop-band attenuation, phase distortion, causality, time domain ringing, transient preservation, memory and CPU consumption, real time requirements, latency, etc.

The choice of a specific filter depends a lot on your specific requirements. Since you are trying to do a control loop, I'm guessing you want a minimum phase filter with as low a latency as possible (but I'm not sure).

In this case, Butterworth lowpass filters are probably a good starting point. These are so-called IIR filters which are recursive and should ALWAYS be implemented as cascaded second order sections.

Below is a code example on how to do this in Matlab.

%% 4th order Butter worth filter
fs = 1000; % sample rate
fc = 30;   % cut off frequency
[z,p,k] = butter(4,2*fc/fs);  % design filter
sos = zp2sos(z,p,k);  % convert to second order sections
% test run
nx = 2^16; 
x = randn(nx,1);   % white noise
y = sosfilt(sos,x);  % apply filter

% plot the result
nfft = 8192; % frequency resolution for spectrum
psd = pwelch([x y],hanning(nfft)); % power spectral density
xlabel('Frequency in Hz');
ylabel('Level in dB');
ylims = [-100 3];
% add a marker line and a tick for the cutoff frequeny
hold on
plot(fc*[1 1],ylims,'-d');
title('4th order BW lowpass @30 Hz');
xtick = get(gca,'xtick');
xtick = sort([fc xtick]);
legend('Input Spectrum','Output Spectrum','Cutoff Frequency','Location','East');

enter image description here

  • $\begingroup$ Nice answer. thanks. $\endgroup$
    – CroCo
    Commented Oct 10, 2023 at 17:01
  • $\begingroup$ is this recursive process? $\endgroup$
    – CroCo
    Commented Oct 10, 2023 at 22:22
  • $\begingroup$ Yes. It's an IIR filter and hence its recursive. $\endgroup$
    – Hilmar
    Commented Oct 11, 2023 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.