用8位微處理器實(shí)現(xiàn)數(shù)字低通濾波器設(shè)計(jì)
濾波常發(fā)生在模擬世界。不幸的是,在數(shù)字領(lǐng)域,工程師通常主要使用DSP(數(shù)字信號處理器),而不是8位單片機(jī)實(shí)現(xiàn)濾波。這個(gè)情形的發(fā)生,是因?yàn)闉V波器設(shè)計(jì)的算法比大多數(shù)工程師樂于處理的算法更復(fù)雜。而且,數(shù)字濾波需要計(jì)算整形數(shù),而不是浮點(diǎn)數(shù)。這引發(fā)了兩個(gè)問題。第一,有限位的舍入誤差降低了濾波器響應(yīng),甚至使它不穩(wěn)定。第二,必須用整數(shù)算法處理小數(shù)值。
有幾種方法解決這些問題。例如,可以使用16、32和64位數(shù)的操作,或可以縮放到更好的精度。這些和其他方法通常需要更多的存儲器,造成編程經(jīng)常不適于小型微處理器。文獻(xiàn)研究所示用C語言編寫的數(shù)字濾波固件,與用匯編語言編寫需要更多的存儲器。這個(gè)情形對存儲器資源有限的小型微處理器來講,常常是不可接受的。
列表1(程序見英文原文)列出了一個(gè)用8位微處理器設(shè)計(jì)單極低通數(shù)字濾波器固件的簡單方法。Freescale公司的低端MC68HC908QT2使用匯編編程器,但可以將本設(shè)計(jì)方案用于任一型號微處理器,只要其使用的也是標(biāo)準(zhǔn)匯編指令。
將基于廣義Z
變換算法的復(fù)雜設(shè)計(jì)方案放在一邊,本方案使用了一種基于遞歸方程的解決辦法。計(jì)算每個(gè)輸出信號采樣,作為輸入信號和前一個(gè)輸出信號帶相關(guān)系數(shù)的總和。遞歸方程定義一個(gè)單極低通濾波器為:Y[n]=X[n]×a0+Y[n–1]×b1,在這里X[n]和Y[n]為采樣[n]的輸入輸出值,Y[n–1]為前一采樣[n–1]的輸出值,a0和b1為減少δ控制的權(quán)重系數(shù)。系數(shù)為0δ1的值,a0=1–δ且b1=δ。理論上,δ為輸入信號從高電平降到低電平時(shí),鄰近的輸出采樣之間的衰減量??梢灾苯又付?delta;值或找到濾波器所需的時(shí)間常數(shù)d,其為采樣數(shù)上升到低通濾波器穩(wěn)態(tài)63.2%時(shí)的輸出。D和δ之間存在確定的關(guān)系:δ=e–1/d,在這里e為自然對數(shù)基底。前面的公式服從Y[n]=Y[n–1]+(1–δ)×(X[n]–Y[n–1])。
為取代小數(shù)相乘,1–δ對匯編程序而言,用除以倒數(shù)為整數(shù)的方法更方便,F(xiàn)=1/(1–δ): Y[n]=Y[n–1]+(X[n]–Y[n–1])/F。因此,可以按照下面的步驟確定數(shù)字濾波器參數(shù):
1、選擇參數(shù)F。對匯編程序而言,便于用右移實(shí)現(xiàn)除法運(yùn)算。右移就是F值應(yīng)該為2S,在此S為偏移數(shù)。讓F為8,相當(dāng)于右移三位。
2、計(jì)算衰減:δ=1–1/F=1–1/8=0.875。
3、計(jì)算時(shí)間常數(shù)d=–1/lnδ=–1/ln0.875=7.49采樣。
公式Y(jié)[n]=Y[n–1]+(X[n]–Y[n–1])/F決定濾波器的微處理器算法設(shè)計(jì)。算法需要三個(gè)寄存器:輸入X[n]、輸出Y[n]和一個(gè)遞增寄存器保存(X[n]–Y[n–1])/F的值。三個(gè)寄存器的大小取決于輸入。應(yīng)用中,內(nèi)置的8位ADC輸出范圍從00到$FF的信號,必須經(jīng)歷低通濾波器。所以輸入和輸出寄存器均為1個(gè)字節(jié)。為增加除法精度,增加一半除數(shù)到被除數(shù)。這個(gè)處理將遞增寄存器增加到2個(gè)字節(jié)。
用數(shù)字方法實(shí)現(xiàn)濾波功能提供了一致性的好處,因?yàn)槠骷`差、溫度漂移和老化不會影響濾波器算法。用微處理器實(shí)現(xiàn)數(shù)字濾波器,增加了可調(diào)整濾波器參數(shù)的靈活性優(yōu)勢,因?yàn)檫@個(gè)靈活性僅取決于固件。
英文原文:
8-bit microcontroller implements digital lowpass filter
A simple assembler routine fits a digital lowpass filter into a microcontroller.
Abel Raynus, Armatron International, Malden, MA; Edited by Charles H Small and Fran Granville -- EDN, 1/24/2008
Filtering occurs frequently in the analog world. Unfortunately, in the digital world, engineers apply it mainly to the DSPs (digital-signal processors) and not to the small 8-bit microcontrollers that designers commonly use. This situation occurs because the math for the filter design is more complicated than most engineers are willing to deal with. Moreover, digital filtering requires calculations on integers instead of on floating-point numbers. This scenario causes two problems. First, the rounding-off error from the limited number of bits can degrade the filter response or even make it unstable. Second, you must handle the fractional values with integer math.
Several ways exist to solve these issues. For example, you can use operations with 16-, 32-, and 64-bit numbers, or you can scale for better accuracy. These and other methods usually require more memory, and, as a result, the program often does not fit into a small microcontroller. A literature search shows that published digital-filter firmware is written in C. Programs in C need more memory than those written in assembler. This situation often makes them unacceptable for small microcontrollers with limited memory resources.
Listing 1 shows a simple engineering method to design single-pole, lowpass-digital-filter firmware for 8-bit microcontrollers. The low-end Freescale MC68HC908QT2 is the target of the assembler program, but you can apply this Design Idea to any type of microcontroller because it uses only standard assembler instructions.
Leaving aside the sophisticated design methods based on Z transformation with its extensive math, this idea uses another approach based on a recursive equation. You calculate each output-signal sample as the sum of the input signal and the previous output signal with corresponding coefficients. A recursive equation defines a single-pole lowpass filter as: Y[n]=X[n]×a0+Y[n–1]×b1, where X[n] and Y[n] are input and output values of sample [n], Y[n–1] is an output value of the previous sample [n–1], and a0 and b1 are weight coefficients that decrement δ controls. The coefficients have the value of 0δ1, a0=1–δ, and b1=δ. Physically, δ is the amount of decay between adjacent output samples when the input signal drops from a high level to a low level. You can directly specify the value of δ or find it from the desired time constant of the filter, d, which is the number of samples it takes the output to rise to 63.2% of the steady-state level for a lowpass filter. A fixed relationship exists between d and δ: δ=e–1/d, where e is the base of natural logarithms. The preceding equations yield Y[n]=Y[n–1]+(1–δ)×(X[n]–Y[n–1]).
Instead of multiplying a decimal-point number, 1–δ, it is more convenient for assembler programming to divide by the reciprocal integer, F=1/(1–δ): Y[n]=Y[n–1]+(
X[n]–Y[n–1])/F. Thus, you can determine the digital filter’s parameters using the following steps:
Choose the parameter F. For assembler, it is convenient to perform division as right shifts. For right shifts, the value of F should be 2S, where S is the number of shifts. Let F equal 8, which you reach after three right shifts.
Calculate the decrement: δ=1–1/F=1–1/8=0.875.
Calculate the time constant as d=–1/lnδ=–1/ln0.875=7.49 samples.
The equation Y[n]=Y[n–1]+(X[n]–Y[n–1])/F determines the design of the microcontroller’s algorithm for the filter. The algorithm needs three registers: input for X[n], output for Y[n], and an increment register to keep the (X[n]–Y[n–1])/F term. The size of these registers depends on the inputs. In this application, the signals from the built-in 8-bit ADC range from 00 to $FF and must go through the lowpass filter. So, the input and the output registers are 1 byte in size. To increase the accuracy of division, add half the divisor to the dividend. This action increases the increment register to 2 bytes.
Numerically performing the filtering function provides the benefit of consistency because component tolerances, temperature drift, and aging do not affect the filter’s algorithm. The implementation of the digital filter in the microcontroller gives the additional benefit of flexibility to adjust the filter’s parameters, because this flexibility depends only on the firmware.
評論