main content

hdl video filter -凯发k8网页登录

this example illustrates how to generate hdl code for an itu-r bt.601 luma filter with 8-bit input data and 10-bit output data. this filter is a low-pass filter with a -3 db point of 3.2 mhz with a 13.5 mhz sampling frequency and a specified range for both passband ripple and stopband attenuation shown in the itu specification. the filter coefficients were designed using the dsp system toolbox™. this example focuses on quantization effects and generating hdl code for the system object filter.

set up the coefficients

assign the previously designed filter coefficients to variable b. this is a halfband filter and, therefore, every other coefficient is zero with the exception of the coefficient at the filter midpoint, which is exactly one-half.

check that double-precision filter design meets the itu-r bt.601 template for passband ripple and stopband attenuation using freqz and plot the passband. the red lines show the allowed variation in the specification.

b = [0.00303332064210658,  0,...
    -0.00807786494715095,  0,...
     0.0157597395206364,   0,...
    -0.028508691397868,    0,...
     0.0504985344927114,   0,...
    -0.0977926818362618,   0,...
     0.315448742029959,...
     0.5,...
     0.315448742029959,    0,...
    -0.0977926818362618,   0,...
     0.0504985344927114,   0,...
    -0.028508691397868,    0,...
     0.0157597395206364,   0,...
    -0.00807786494715095,  0,...
     0.00303332064210658];
f = 0:100:2.75e6;
h = freqz(b,1,f,13.5e6);
plot(f,20*log10(abs(h)));
title('hdl video filter double-precision passband');
axis([0 2.75e6 -.8 .8]);
passbandrange = {[2.75e6;  1e6;   0;  1e6; 2.75e6],...
                 [  -0.5; -0.5;   0;  0.5;    0.5]};
line(passbandrange{:}, 'color', 'red');

figure contains an axes object. the axes object with title hdl video filter double-precision passband contains 2 objects of type line.

plot the stopband

the red line shows a "not to exceed" limit on the stopband.

f = 4e6:100:6.75e6;
h = freqz(b,1,f,13.5e6);
plot(f,20*log10(abs(h)));
title('hdl video filter double-precision stopband');
axis([4e6 6.75e6 -70 -35]);
stopbandrange = {[4e6;   6.25e6;  6.75e6],...
                 [-40;      -55;     -55]};
line(stopbandrange{:}, 'color', 'red');

figure contains an axes object. the axes object with title hdl video filter double-precision stopband contains 2 objects of type line.

create the quantized filter

create a fir filter system object filter with previously defined coefficients. experiment with the coefficient word length to get the desired response for 8-bit input data and 10-bit output data.

videofilter = dsp.firfilter;
videofilter.numerator = b;
videofilter.structure = 'direct form symmetric';
%try 10-bit coefficients
videofilter.coefficientsdatatype = 'custom';
videofilter.customcoefficientsdatatype = numerictype(1,10);

plot the quantized filter response

now examine the passband and stopband response of the quantized filter relative to the specification. plot and check the quantized passband first.

the quantized design meets the passband specifications except at dc, where it misses the specification by about 0.035 db.

f = 0:100:2.75e6;
h = freqz(videofilter,f,13.5e6,'arithmetic','fixed');
plot(f,20*log10(abs(h)));
title('hdl video filter quantized passband');
axis([0 2.75e6 -.8 .8]);
line(passbandrange{:}, 'color', 'red');

figure contains an axes object. the axes object with title hdl video filter quantized passband contains 2 objects of type line.

plot the quantized stopband

the red lines again show a "not to exceed" limit on the stopband.

the stopband limit is violated, which indicates a problem with the quantization settings.

f = 4e6:100:6.75e6;
h = freqz(videofilter,f,13.5e6,'arithmetic','fixed');
plot(f,20*log10(abs(h)));
title('hdl video filter quantized stopband');
axis([4e6 6.75e6 -70 -35]);
line(stopbandrange{:}, 'color', 'red');

figure contains an axes object. the axes object with title hdl video filter quantized stopband contains 2 objects of type line.

change the coefficient quantizer settings

adding more bits to the coefficient word length enables the filter to meet the specification. increment the word length by one and replot the stopband.

this just misses the specification at the end of the stopband. this small deviation from the specification might be acceptable if you know that some other part of your system applies a lowpass filter to this signal.

videofilter.customcoefficientsdatatype = numerictype(1,11);
f = 4e6:100:6.75e6;
h = freqz(videofilter,f,13.5e6,'arithmetic','fixed');
plot(f,20*log10(abs(h)));
title('hdl video filter quantized stopband');
axis([4e6 6.75e6 -70 -35]);
line(stopbandrange{:}, 'color', 'red');

figure contains an axes object. the axes object with title hdl video filter quantized stopband contains 2 objects of type line.

set the final coefficient quantizer word length

add one more bit to the coefficient quantizer word length and replot the stopband. this should meet the specification.

videofilter.customcoefficientsdatatype = numerictype(1,12);
f = 4e6:100:6.75e6;
h = freqz(videofilter,f,13.5e6,'arithmetic','fixed');
plot(f,20*log10(abs(h)));
title('hdl video filter final quantized stopband');
axis([4e6 6.75e6 -70 -35]);
line(stopbandrange{:}, 'color', 'red');

figure contains an axes object. the axes object with title hdl video filter final quantized stopband contains 2 objects of type line.

perform a final check on the passband response

recheck the passband to be sure the changes have improved the problems in the response near dc. the response now passes the specification.

f = 0:100:2.75e6;
h = freqz(videofilter,f,13.5e6,'arithmetic','fixed');
plot(f,20*log10(abs(h)));
title('hdl video filter final quantized passband');
axis([0 2.75e6 -.8 .8]);
line(passbandrange{:}, 'color', 'red');

figure contains an axes object. the axes object with title hdl video filter final quantized passband contains 2 objects of type line.

generate hdl code and test bench from the quantized filter

starting from the quantized filter, generate vhdl or verilog code.

create a temporary work directory. after generating the hdl (selecting vhdl in this case), open the generated vhdl file in the editor.

generate a vhdl test bench to make sure that it matches the response in matlab exactly. select the default input stimulus, which for fir is impulse, step, ramp, chirp, and noise inputs.

to generate verilog code and verilog test bench instead, change value for 'targetlanguage' property from 'vhdl' to 'verilog'.

the warnings indicate that by selecting the symmetric structure for the filter and generating hdl, may result in smaller area or a higher clock rate.

assume an input of 8-bit word length with 7-bit fractional bits.

workingdir = tempname;
generatehdl(videofilter, 'name', 'hdlvideofilt', ...
                'inputdatatype' ,numerictype(1,8,7), ...
                'targetlanguage', 'vhdl', ...
                'targetdirectory', workingdir, ...
                'generatehdltestbench', 'on');
### starting vhdl code generation process for filter: hdlvideofilt
### generating: /tmp/bdoc23a_2213998_491196/tpf775bc5c_8389_41bc_938f_f0d9cd6296c1/hdlvideofilt.vhd
### starting generation of hdlvideofilt vhdl entity
### starting generation of hdlvideofilt vhdl architecture
### successful completion of vhdl code generation process for filter: hdlvideofilt
### hdl latency is 2 samples
### starting generation of vhdl test bench.
### generating input stimulus
### done generating input stimulus; length 3261 samples.
### generating test bench: /tmp/bdoc23a_2213998_491196/tpf775bc5c_8389_41bc_938f_f0d9cd6296c1/hdlvideofilt_tb.vhd
### creating stimulus vectors ...
### done generating vhdl test bench.
edit(fullfile(workingdir, 'hdlvideofilt.vhd'));

plot the test bench stimulus

plot the default test bench stimulus used by the above command, using the generatetbstimulus function.

tbstim = generatetbstimulus(videofilter,'inputdatatype', numerictype(1,8,7));
plot(tbstim);
title('hdl video filter test bench stimulus');

figure contains an axes object. the axes object with title hdl video filter test bench stimulus contains an object of type line.

conclusion

you designed a double precision filter to meet the itu-r bt.601 luma filter specification and then created a fir filter system object that also met the specification. you generated vhdl code and a vhdl test bench that functionally verified the filter.

you can use a vhdl simulator, such as modelsim®, to verify these results. you can also experiment with verilog. you can use many optimizations to get smaller and faster hdl results by removing the constraint that the generated hdl be exactly true to matlab. when you use these optimizations, the hdl test bench can check the filter response to be within a specified error margin of the matlab response.

网站地图