1. 程式人生 > >高通&低通濾波演算法

高通&低通濾波演算法

高通 Algorithmic implementation

The filter recurrence relation provides a way to determine the output samples in terms of the input samples and the preceding output. The following pseudocodealgorithm will simulate the effect of a high-pass filter on a series of digital samples:

 // Return RC high-pass filter output samples, given input samples,  // time interval dt
, and time constant RC 
 function highpass(real[0..n] x, real dt, real RC)    
var real[0..n] y    var real α := RC / (RC + dt)    
y[0] := x[0]    
for i from 1 to n      
y[i] := α * y[i-1] + α * (x[i] - x[i-1])    
return y 

The loop which calculates each of the n outputs can be refactored into the equivalent:

   for i from 1 to n      y[i] := α * (y[i-1] + x[i] - x[i-1]) 

However, the earlier form shows how the parameter α changes the impact of the prior output y[i-1] and current change in input (x[i] - x[i-1]). In particular,

  • A large α implies that the output will decay very slowly but will also be strongly influenced by even small changes in input. By the relationship between parameter α and 
    time constant
     RC above, a large α corresponds to a large RC and therefore a low corner frequency of the filter. Hence, this case corresponds to a high-pass filter with a very narrow stop band. Because it is excited by small changes and tends to hold its prior output values for a long time, it can pass relatively low frequencies. However, a constant input (i.e., an input with (x[i] - x[i-1])=0) will always decay to zero, as would be expected with a high-pass filter with a large RC.
  • A small α implies that the output will decay quickly and will require large changes in the input (i.e., (x[i] - x[i-1]) is large) to cause the output to change much. By the relationship between parameter α and time constant RC above, a small α corresponds to a small RC and therefore a high corner frequency of the filter. Hence, this case corresponds to a high-pass filter with a very wide stop band. Because it requires large (i.e., fast) changes and tends to quickly forget its prior output values, it can only pass relatively high frequencies, as would be expected with a high-pass filter with a small RC.

低通Algorithmic implementation

The filter recurrence relation provides a way to determine the output samples in terms of the input samples and the preceding output. The following pseudocodealgorithm will simulate the effect of a low-pass filter on a series of digital samples:

 // Return RC low-pass filter output samples, given input samples,  // time interval dt, and time constant RC  
function lowpass(real[0..n] x, real dt, real RC)    
var real[0..n] y    var real α := dt / (RC + dt)    
y[0] := x[0]    
for i from 1 to n        
y[i] := α * x[i] + (1-α) * y[i-1]    
return y 

The loop that calculates each of the n outputs can be refactored into the equivalent:

   for i from 1 to n        y[i] := y[i-1] + α * (x[i] - y[i-1]) 

That is, the change from one filter output to the next is proportional to the difference between the previous output and the next input. This exponential smoothing property matches the exponential decay seen in the continuous-time system. As expected, as the time constant \scriptstyle RC increases, the discrete-time smoothing parameter \scriptstyle \alpha decreases, and the output samples \scriptstyle (y_1,\, y_2,\, \ldots,\, y_n) respond more slowly to a change in the input samples \scriptstyle (x_1,\, x_2,\, \ldots,\, x_n); the system will have more . This filter is an infinite-impulse-response (IIR) single-pole lowpass filter.

http://en.wikipedia.org/wiki/High-pass_filter

http://en.wikipedia.org/wiki/Low-pass_filter