29 July 2005

The Digital Modeling of a Simple RC Low Pass Filter

Ralph J. Coppola (r_j_coppola<at>hotmail.com)

Introducing the Basic RC Low Pass Filter

Filters play an important role in many kinds of analog electronic circuits.

Figure 1 illustrates an example of the exaggerated output signal from a noisy analog sensor.


Figure 1. Exaggerated raw signal.

It is generally desirable to smooth or filter the clutter of the short-term noise so that the signal's long-term trends may be more readily visualized. This is especially true if we are using the signal to control a process, such as temperature, where we may want the process to respond to the trend and not the short term spikes.

When dealing with analog signals, this filtering can be easily accomplished by using a simple resistor/capacitor (RC) low pass filter, such as the one shown in Figure 2.


Figure 2. RC low pass filter.

It is not my intention to go too deeply into the theory of RC Filters. It is sufficient to say that the operation of an RC low pass filter is based on the time that it takes for the capacitor to charge or discharge. Those who may not be familiar with the subject are encouraged to refer to a text on basic electronics or refer to the Web links for “ RC Filters ” and/or RC Time Constants ” that I have provided at the end of this article.

Figure 3 shows a RC Low Pass Filter's DC response to a positive step input.

Note: 1 Time Constant (seconds) = R (ohms) x C (farads)


Figure 3. RC low pass filter's response to a step input signal.

As a rule of thumb, the output of the circuit is considered to have reached a steady state level after 5 RC time constants.

Table 1 shows the percentage of the steady state value at each time constant (TC) interval.

Time

% of Max

0 TC

0

1 TC

63.2%

2 TC

86.5%

3 TC

95.0%

4 TC

98.2%

5 TC

99.3 %

Table1. Percentage of the steady state.

The effect of the simple RC low pass filter is governed by the value of the RC time constant. In other words, the longer the time constant, the greater the filtering.

Filter Simulation

If the signals of interest are in a digital format, such as the output from an analog/digital converter or a computer text file, some method other than an RC filter must be used.

The following equation provides a simple method to use software to closely simulate the characteristics of an RC low pass filter. The smoothing is accomplished by taking a fraction of the new input and adding it to a percentage of the previous filtered result.

S = (f * R) + (( 1 – f ) * P)

Where S is the smoothed or filtered value, f is the filter factor, R is the raw data input, and P is the previous value of S. 0 = maximum filtering (no output: S = 0 ), and 1 = minimum filtering ( S = R ).

Note that this simulation equation uses only simple arithmetic. This allows it to be implemented in computer languages that do not support higher math functions.

The Code

The following code solves for the equation given above. It is written in a generic form and should be easily translated into your favorite language.

 # ***********************************
 # *  Example of Filter Program  *
 #                                                    *
 # ***********************************

 # S = (f*R) + ((1-f) * P)

 S = 0              # Smoothed or filtered value
 f = 0.2            # Filter factor
 D = 0             # Raw data input
 P = 0             # Previous Smoothed value

 Start    input D                                 # Get the raw (D)ata sample
            S = (f*D) + ((1-f) * P)           # Compute the new (S)moothed value
             output S                              # Output  (S)moothed value to the application
             P = S                                   # Save the new (S)moothed value as (P)revious
             goto Start                            # Loop for the next sample
 End

Filter Response

Table 2 illustrates that if a filter factor of 0.2 is used, the simulation mimics an actual RC low pass filter very closely.

RC Low Pass Filter

Simulation (f=0.2)

Time

% of Max

Samples

% of Max

0 RC

0

0

0

1 RC

63.2%

4

59.0%

2 RC

86.5%

8

83.2%

3 RC

95.0%

12

93.1%

4 RC

98.2%

16

97.2%

5 RC

99.3 %

20

98.8%

Table 2. Comparison of an RC low pass filter to the simulation.

It should be noted that unlike the RC filter, which is related to time, the simulation's output is related, instead, to the number of samples.

Figure 4 shows the simulation's response to a square pulse, while Figure 5 illustrates its response to a cyclic or random input.


Figure 4. Simulation's response to a square pulse.


Figure 5. Simulation's response to a random function.

Tables 2 and 3 show a Microsoft S Excel setup that is similar to the program that was used to produce the example shown in Fig. 9.

 

A

B

C

E

1

= RAND ()

0

0

0

2

= RAND ()

=0.2*A1

=0.8*E1

=B2 + C2

3

= RAND ()

=0.2*A2

=0.8*E2

=B3 + C3

4

= RAND ()

=0.2*A3

=0.8*E3

=B4 + C4

 

 

 

 

 

n

= RAND ()

=0.2*A(n-1)

=0.8*E(n-1)

=Bn + Cn

Table 3. Microsoft Excel programming for Fig. 5.

 

Column

Description

A

Random Input

B

Use 20% of Input

C

Use 80% of Old Value

E

New Value = B + C

- - -

Net Old Value = New Value

Table 4. Values used in Fig. 5. 

 

Conclusion

Besides its simplicity, the main advantages of this model is that it can be used in real time, while the data is being collected or at some later time from stored data.

Similar techniques can be found in many industrial process control applications, and it is hoped that you may find it of some value in your applications.

References 

   
Copyright 2005 by Society for Amateur Scientists