fixed-凯发k8网页登录
design filters for use with fixed-point input. the example analyzes the effect of coefficient quantization on filter design. you must have the fixed-point designer™ software to run this example.
introduction
fixed-point filters are commonly used in digital signal processors where data storage and power consumption are key limiting factors. with the constraints you specify, dsp system toolbox™ software allows you to design efficient fixed-point filters. the filter for this example is a lowpass equiripple fir filter. design the filter first for floating-point input to obtain a baseline. you can use this baseline for comparison with the fixed-point filter.
fir filter design
the lowpass fir filter has the following specifications:
sample rate: 2000 hz
center frequency: 450 hz
transition width: 100 hz
equiripple design
maximum 1 db of ripple in the passband
minimum 80 db of attenuation in the stopband
samplingfrequency = 2000; centerfrequency = 450; transitionwidth = 100; passbandripple = 1; stopbandattenuation = 80; designspec = fdesign.lowpass('fp,fst,ap,ast',... centerfrequency-transitionwidth/2, ... centerfrequency transitionwidth/2, ... passbandripple,stopbandattenuation, ... samplingfrequency); lpf = design(designspec,'equiripple',... 'systemobject',true)
lpf = dsp.firfilter with properties: structure: 'direct form' numeratorsource: 'property' numerator: [-0.0013 -0.0055 -0.0112 -0.0125 -0.0048 0.0062 0.0081 -0.0022 -0.0104 -0.0027 0.0115 0.0093 -0.0098 -0.0171 0.0039 0.0242 0.0073 -0.0286 -0.0247 0.0268 0.0498 -0.0134 -0.0887 -0.0298 0.1895 0.3983 0.3983 0.1895 -0.0298 ... ] initialconditions: 0 show all properties
view the baseline frequency response. the dotted red lines show the design specifications used to create the filter.
fvtool(lpf)
full-precision fixed-point operation
the fixed-point properties of the filter are contained in the fixed-point properties
section in the display of the object. by default, the filter uses full-precision arithmetic to deal with fixed-point inputs. with full-precision arithmetic, the filter uses as many bits for the product, accumulator, and output as needed to prevent any overflow or rounding. if you do not want to use full-precision arithmetic, you can set the fullprecisionoverride
property to false
and then set the product, accumulator, and output data types independently.
rng default inputwordlength = 16; fixedpointinput = fi(randn(100,1),true,inputwordlength); floatingpointinput = double(fixedpointinput); floatingpointoutput = lpf(floatingpointinput); release(lpf) fullprecisionoutput = lpf(fixedpointinput); norm(floatingpointoutput-double(fullprecisionoutput),'inf')
ans = 6.8994e-05
the result of full-precision fixed-point filtering comes very close to floating point, but the results are not exact. the reason for this is coefficient quantization. in the fixed-point filter, the coefficientsdatatype
property has the same word length (16) for the coefficients and the input. the frequency response of the filter in full-precision mode shows this more clearly. the measure
function shows that the minimum stopband attenuation of this filter with quantized coefficients is 76.6913 db, less than the 80 db specified for the floating-point filter.
lpf.coefficientsdatatype
ans = 'same word length as input'
fvtool(lpf)
measure(lpf)
ans = sample rate : 2 khz passband edge : 400 hz 3-db point : 416.2891 hz 6-db point : 428.1081 hz stopband edge : 500 hz passband ripple : 0.96325 db stopband atten. : 76.6913 db transition width : 100 hz
the filter was last used with fixed-point input and is still in a locked state. for that reason, fvtool
displays the fixed-point frequency response. the dash-dot response is that of the reference floating-point filter, and the solid plot is the response of the filter that was used with fixed-point input. the desired frequency response cannot be matched because the coefficient word length has been restricted to 16 bits. this accounts for the difference between the floating-point and fixed-point designs. increasing the number of bits allowed for the coefficient word length makes the quantization error smaller and enables you to match the design requirement for 80 db of stopband attenuation. use a coefficient word length of 24 bits to achieve an attenuation of 80.1275 db.
lpf24bitcoeff = design(designspec,'equiripple',... 'systemobject',true); lpf24bitcoeff.coefficientsdatatype = 'custom'; coeffnumerictype = numerictype(fi(lpf24bitcoeff.numerator,true,24)); lpf24bitcoeff.customcoefficientsdatatype = numerictype(true,... coeffnumerictype.wordlength,coeffnumerictype.fractionlength); fullprecisionoutput32bitcoeff = lpf24bitcoeff(fixedpointinput); norm(floatingpointoutput-double(fullprecisionoutput32bitcoeff),'inf')
ans = 4.1077e-07
fvtool(lpf24bitcoeff)
measure(lpf24bitcoeff)
ans = sample rate : 2 khz passband edge : 400 hz 3-db point : 416.2901 hz 6-db point : 428.1091 hz stopband edge : 500 hz passband ripple : 0.96329 db stopband atten. : 80.1275 db transition width : 100 hz
design parameters and coefficient quantization
in many fixed-point design applications, the coefficient word length is not flexible. for example, supposed you are restricted to work with 14 bits. in such cases, the requested minimum stopband attenuation of 80 db cannot be reached. a filter with 14-bit coefficient quantization can achieve a minimum attenuation of only 67.2987 db.
lpf14bitcoeff = design(designspec,'equiripple',... 'systemobject',true); coeffnumerictype = numerictype(fi(lpf14bitcoeff.numerator,true,14)); lpf14bitcoeff.coefficientsdatatype = 'custom'; lpf14bitcoeff.customcoefficientsdatatype = numerictype(true, ... coeffnumerictype.wordlength,coeffnumerictype.fractionlength); measure(lpf14bitcoeff,'arithmetic','fixed')
ans = sample rate : 2 khz passband edge : 400 hz 3-db point : 416.2939 hz 6-db point : 428.1081 hz stopband edge : 500 hz passband ripple : 0.96405 db stopband atten. : 67.2987 db transition width : 100 hz
for fir filters in general, each bit of coefficient word length provides approximately 5 db of stopband attenuation. accordingly, if your filter's coefficients are always quantized to 14 bits, you can expect the minimum stopband attenuation to be only around 70 db. in such cases, it is more practical to design the filter with stopband attenuation less than 70 db. relaxing this requirement results in a design of lower order.
designspec.astop = 60; lpf60dbstopband = design(designspec,'equiripple',... 'systemobject',true); lpf60dbstopband.coefficientsdatatype = 'custom'; coeffnumerictype = numerictype(fi(lpf60dbstopband.numerator,true,14)); lpf60dbstopband.customcoefficientsdatatype = numerictype(true, ... coeffnumerictype.wordlength,coeffnumerictype.fractionlength); measure(lpf60dbstopband,'arithmetic','fixed')
ans = sample rate : 2 khz passband edge : 400 hz 3-db point : 419.3391 hz 6-db point : 432.9718 hz stopband edge : 500 hz passband ripple : 0.92801 db stopband atten. : 59.1829 db transition width : 100 hz
order(lpf14bitcoeff)
ans = 51
order(lpf60dbstopband)
ans = 42
the filter order decreases from 51 to 42, implying that fewer taps are required to implement the new fir filter. if you still want a high minimum stopband attenuation without compromising on the number of bits for coefficients, you must relax the other filter design constraint: the transition width. increasing the transition width might enable you to get higher attenuation with the same coefficient word length. however, it is almost impossible to achieve more than 5 db per bit of coefficient word length, even after relaxing the transition width.
designspec.astop = 80; transitionwidth = 200; designspec.fpass = centerfrequency-transitionwidth/2; designspec.fstop = centerfrequency transitionwidth/2; lpf300transitionwidth = design(designspec,'equiripple',... 'systemobject',true); lpf300transitionwidth.coefficientsdatatype = 'custom'; coeffnumerictype = numerictype(fi(lpf300transitionwidth.numerator,... true, 14)); lpf300transitionwidth.customcoefficientsdatatype = numerictype(true,... coeffnumerictype.wordlength,coeffnumerictype.fractionlength); measure(lpf300transitionwidth,'arithmetic','fixed')
ans = sample rate : 2 khz passband edge : 350 hz 3-db point : 385.4095 hz 6-db point : 408.6465 hz stopband edge : 550 hz passband ripple : 0.74045 db stopband atten. : 74.439 db transition width : 200 hz
as you can see, increasing the transition width to 200 hz allows 74.439 db of stopband attenuation with 14-bit coefficients, compared to the 67.2987 db attained when the transition width was set to 100 hz. an added benefit of increasing the transition width is that the filter order also decreases, in this case from 51 to 27.
order(lpf300transitionwidth)
ans = 27