# Understanding PID Control, Part 3: Expanding Beyond a Simple Derivative

From the series: Understanding PID Control

Let’s continue to expand our ideal PID controller and see if we can make it even more robust when trying to control real systems that don’t behave like ideal linear models. In the last video, we looked at how an actuator can saturate and we tacked on some anti-windup logic to deal with that. In this video, we’re going to focus our attention on the derivative and look at how the device that is sensing the true state of the system and producing a measured state that the controller can use can add noise into our feedback loop.  I’m Brian, and welcome to a MATLAB Tech Talk.

Noise is a random disturbance on a signal. And when you are dealing with sensors, both mechanical and electronic sensors, it is unavoidable. The generic term "noise" refers to the summation of different noises created from different sources that stem from things like the environment it’s operating in (which is both naturally occurring noise and man-made noise), the specific implementation of the electronics, and from manufacturing defects. They can exist at very specific frequencies or be spread out across the spectrum. For example, if the noise has equal intensities at different frequencies, then it’s called white noise, which you have probably have seen and heard as static in a nonexistent radio or TV station.

But beyond white noise, there are tons of things that can cause noise in your system and they have cool names like thermal noise, shot noise, flicker noise, burst noise, and coupled noise, to just name a few. The descriptions of each of these are beyond the scope of this video, but the thing to take away from this is that they all produce these tiny shifts in voltage over a wide range of frequencies, which in turn produces shifts in the measurement itself. It’s easy to see how large amplitude, or really loud noise could impact a system, but what isn’t as obvious is how small amplitude, quiet noise but really high frequency can also cause problems.

What the sensor is measuring might be a nice smooth quantity, like a slowly rising temperature, but due to sensor noise the measurement might be jagged with tiny fluctuations that deviate from the true temperature, even if they are so small you can’t initially tell just by looking at the data with your eye.

An easy way to demonstrate this is to take a picture with your cell phone. But instead of a busy image where you might not notice some really subtle noise, take a black image by laying it down on a flat surface, so no light can get in. I took a dark image with my cell phone and to show you what this noise looks like I’ll use MATLAB to view the image. At first glance, the image looks like a perfectly black picture, but there are slight variations in those dark pixels. We can confirm that by making the image brighter by multiplying each pixel value by 50. And you can see that it’s obvious that there is some low-amplitude random noise throughout the image.

This little bit of noise doesn’t impact the look of an image much, especially if it’s a bright vibrant image, so it stands to reason that low-amplitude noise won’t impact our control system much either – it’ll be down in the noise as they say. And that’s true for some control laws; as long as the noise is low amplitude then it won’t impact the system much. But that’s not true for our ideal PID controller because this has a pure derivative.  Derivatives amplify high-frequency signals and can take those tiny, barely noticeable wiggles and amplify them to values that can impact our system. Let’s see if we can figure out why that is.

Let’s look at a low-frequency noise signal. Just a pure sine wave. The slope of the signal is the derivative, and if we look at the point where the signal has the steepest slope and draw a yellow line we can get a visual indication of what value the derivative will return. The steeper the line, the higher the derivative.

Now let’s look at how the slope changes for a high-frequency signal. The amplitude of the noise hasn’t changed, but the increase in frequency generated a steeper slope. We can reduce the amplitude of the noise signal and get back to a slope, or derivative that is similar to the low-frequency signal. And this is the general idea we’ll use later; lower the amplitude of high-frequency noise, so that the derivative isn’t too large. If we increase the frequency even more, the slope gets steeper and we’ll have to lower the amplitude to account for that. So you can see that if you leave really really high-frequency noise in your system, the derivative path will see that noise, even if it’s really small, and amplify it and cause you issues.

We can look at the derivative and see that the amplification of high frequencies also makes sense mathematically. Any signal can be defined as a summation of an infinite number of sine waves. This is what you are doing when you take a Fourier transform of a signal. For the purpose of this example, we only need to look at a single frequency, where A is the amplitude, omega a is the frequency in rad/s, and phi is the phase.

If we take the derivative of this sine wave we get a new sine wave at the exact same frequency, but shifted in phase 90 degrees, and with a new amplitude A times omega a. From here, it’s easy to see that if omega a is greater than 1 rad/s the amplitude got larger. And if omega a is less than 1 rad/s the amplitude got smaller.

If we plot this magnitude change it will look like this sloped yellow line where higher frequencies create higher-amplitude signals and lower frequencies create lower-amplitude signals. So we want a filter that can block frequencies above a certain point from entering our derivative and causing problems. This point is called the cutoff frequency and as designers we have the choice of where to place it. For example, do we place it as a high frequency and block just the orange section or place it lower and block the green section?

Let’s add our filter to the derivative path and start to talk about what this looks like. Ideally, you want a filter that can remove all of the noise and perfectly pass through all of the signal. But this isn’t something that can be achieved in practice. Luckily, for a lot of applications, the noise across the spectrum is relatively low amplitude, or low power and the signal you’re interested in keeping has comparatively large power and low frequency. In this case, the low-frequency noise doesn’t really impact the derivative much if the amplitude is small and, therefore, we often can remove all of the bothersome noise to our derivative simply by blocking or attenuating just the high-frequency information. And the simplest way to do that is with a first order low pass filter.

This is a filter, as the name suggests, that will allow frequencies below the cutoff point to pass through mostly unchanged and will attenuate, or lower the amplitude of frequencies above the cutoff point. It doesn’t remove the noise entirely; it just makes it smaller so that even after we amplify it through a derivative, it won’t impact our system much. The key aspect of this filter is determining where to put the cutoff frequency so that you remove as much high-frequency noise as possible without impinging on the frequencies that are actually in the signal you care about. We’ll talk more about that when we talk tuning in a future video.

Now before we close out this video, I want to briefly show you the structure of this low pass filter and derivative combination, mostly so I can then show you a really cool way to implement the derivative portion of the PID controller using not a derivative, but using an integral instead.

This section requires some knowledge of Laplace domain transfer functions, but if you’re not familiar with them that’s OK, I think you’ll still get something out of hearing the concepts. I’ll provide a simple cheat sheet to help interpret the various transfer functions we’ll be looking at. s is the Laplace domain representation of a derivative, the inverse, 1/s is the representation of an integral, and N / (s + N) is a low pass filter, first order, where the number N is the cutoff frequency in rad/s. So if you see the transfer function 10 / (s + 10), this is a low pass filter with cutoff frequency at 10 rad/s.

Also, N / (s + N)  isn’t the only popular form of the 1st order low pass filter. We can divide the top and bottom by N to get 1 / ((1/N)*s + 1). And since the inverse of frequency is time, this form of the equation allows you to specify the time constant of the filter rather than the cutoff frequency. Here, I’m using Tau to represent the time constant but you may also see t, or tf depending on the industry you’re in.

All right, so using these definitions, we have a low pass filter and a derivative and the transfer function of these combined systems is the product of the two, or (s * n) / (s + n). From here we could choose a cutoff frequency and implement a digital version of this in our PID code. But I want to show you an alternative approach to implementing this logic. Instead of a low pass filter in series with a derivative, we can create feedback loop with N in the forward path and an integral in the feedback path.

This might not look like the same logic but we can reduce this block diagram to a single transfer function n over 1 + n*(1/s), and then move the s around to (s * n) / (s + n). Which is exactly equal to what we had before. If you’re not familiar with block diagram reduction, or how we went from the feedback system to a transfer function n / (1 + N * 1/s), I’ll quickly go through an algebraic derivation that you can walk through on your own if you pause this video. So this is an interesting result: you have the choice to implement a low pass filter and derivative, or a feedback loop with an integral in the feedback path. They are going to produce the exact same effect.

So why implement one versus the other? Well, I think it comes down to this. Writing out the filter and derivative explicitly makes your code easy-to-read and understand and so I think it’s preferred. However, it takes more math operations and more computer memory to accomplish this. The integral in the feedback path is a more efficient computation. So which way you implement it depends on what you are going for, easy to read code or efficient algorithms.

At this point, let’s go over to MATLAB and launch Simulink. We just need a blank model to start, and I’ll add the PID controller block to it. When you double click this block to open up the parameters, you’ll notice that you have control over both the derivative gain and the filter coefficient, N. And if you look at the formula that Simulink is using it isn’t a pure derivative; it’s the formula for the filtered derivative that we just described.

So hopefully this cleared up any confusion you might have about why the derivative path is not just a pure derivative and it gives you a better insight into what the filter coefficient is and how it protects your system from high-frequency noise.

In the next video, we’ll get into some of the ways to tune your controller to get the performance you want. So if you don’t want to miss the next tech talk video, don’t forget to subscribe to this channel. Also, if you want to check out my channel, control system lectures, I cover more control theory topics there as well. Thanks for watching, I’ll see you next time.