optimized fir filter -凯发k8网页登录

main content

optimized fir filter

note

the filter design hdl coder™ product will be discontinued in a future release. instead, you can model hardware behavior, and generate hdl code by using system objects or simulink® blocks from dsp hdl toolbox™. these objects and blocks include hardware-friendly control signals and architecture options. to generate hdl code from dsp hdl toolbox objects and blocks, you must also have the hdl coder™ product.

for examples of modeling an fir filter for hardware, see fully parallel systolic fir filter implementation (dsp hdl toolbox) and (dsp hdl toolbox). these examples use the (dsp hdl toolbox) block. equivalent functionality is also available in the (dsp hdl toolbox) system object™.

create a folder for your tutorial files

set up a writable working folder outside your matlab® installation folder to store files that will be generated as you complete your tutorial work. the tutorial instructions assume that you create the folder hdlfilter_tutorials on drive c.

design the fir filter in filter designer

this tutorial guides you through the steps for designing an optimized quantized discrete-time fir filter, generating verilog code for the filter, and verifying the verilog code with a generated test bench.

this section assumes that you are familiar with the matlab user interface and the filter designer.

  1. start the matlab software.

  2. set your current folder to the folder you created in create a folder for your tutorial files.

  3. start the filter designer by entering the filterdesigner command in the matlab command window. the filter design & analysis tool appears.

    filter designer tool

  4. in the filter design & analysis tool, set these filter options.

    optionvalue
    response typelowpass
    design methodfir equiripple
    filter orderminimum order
    optionsdensity factor: 20
    frequency specifications

    units: hz

    fs: 48000

    fpass: 9600

    fstop: 12000

    magnitude specifications

    units: db

    apass: 1

    astop: 80

    these settings are for the default filter design that the filter designer creates for you. if you do not have to change the filter, and design filter is grayed out, you are done and can skip to quantize the fir filter.

  5. click design filter. the filter designer creates a filter for the specified design. the following message appears in the filter designer status bar when the task is complete.

    designing filter... done
    

    for more information on designing filters with the filter designer, see the dsp system toolbox™ documentation.

quantize the fir filter

you must quantize filters for hdl code generation. to quantize your filter,

  1. open the fir filter design you created in design the fir filter in filter designer if it is not already open.

  2. click the set quantization parameters button in the left-side toolbar. the filter designer displays a filter arithmetic menu in the bottom half of the window.

    filter designer tool

  3. select fixed-point from the list. then select specify all from the filter precision list. the filter designer displays the first of three tabbed panels of quantization parameters across the bottom half of the window.

    filter designer tool

    use the quantization options to test the effects of various settings on the performance and accuracy of the quantized filter.

  4. set the quantization parameters as follows:

    tabparametersetting
    coefficientsnumerator word length16
     best-precision fraction lengthsselected
     use unsigned representationcleared
     scale the numerator coefficients to fully utilize the entire dynamic rangecleared
    input/outputinput word length16
     input fraction length15
     output word length16
    filter internalsrounding modefloor
     overflow modesaturate
     accum. word length40
  5. click apply.

for more information on quantizing filters with the filter designer, see the dsp system toolbox documentation.

configure and generate optimized verilog code

after you quantize your filter, you are ready to configure coder options and generate verilog code for the filter. this section guides you through starting the ui, setting options, and generating the verilog code and a test bench for the fir filter you designed and quantized in design the fir filter in filter designer and quantize the fir filter.

  1. start the filter design hdl coder ui by selecting targets > generate hdl in the filter designer tool. the filter designer displays the generate hdl tool.

    generate hdl tool

  2. select verilog for the language option, as shown in this figure.

    target section of the generate hdl tool

  3. in the name text box of the target pane, replace the default name with optfir. this option names the verilog module and the file that contains the verilog code for the filter.

  4. in the filter architecture pane, select the optimize for hdl option. this option is for generating hdl code that is optimized for performance or space requirements. when this option is enabled, the coder makes tradeoffs concerning data types and might ignore your quantization settings to achieve optimizations. when you use the option, keep in mind that you do so at the cost of potential numeric differences between filter results produced by the original filter object and the simulated results for the optimized hdl code.

  5. select csd for the coefficient multipliers option. this option optimizes coefficient multiplier operations by instructing the coder to replace them with additions of partial products produced by a canonical signed digit (csd) technique. this technique minimizes the number of addition operations required for constant multiplication by representing binary numbers with a minimum count of nonzero digits.

  6. select the add pipeline registers option. for fir filters, this option optimizes final summation. the coder creates a final adder that performs pairwise addition on successive products and includes a stage of pipeline registers after each level of the tree. when used for fir filters, this option can produce numeric differences between results produced by the original filter object and the simulated results for the optimized hdl code.

  7. the generate hdl tool now appears.

    generate hdl tool

  8. select the global settings tab of the ui. then select the general tab of the additional settings section.

    in the comment in header text box, type tutorial - optimized fir filter. the coder adds the comment to the end of the header comment block in each generated file.

    global settings tab of the generate hdl tool

  9. select the ports tab of the additional settings section of the ui.

    ports tab of the generate hdl tool

  10. change the names of the input and output ports. in the input port text box, replace filter_in with data_in. in the output port text box, replace filter_out with data_out.

    ports tab of the generate hdl tool

  11. clear the check box for the add input register option. the ports pane now looks as shown.

    ports tab of the generate hdl tool

  12. click the test bench tab in the generate hdl tool. in the file name text box, replace the default name with optfir_tb. this option names the generated test bench file.

    test bench tab of the generate hdl tool

  13. in the test bench pane, click the configuration tab. observe that the error margin (bits) option is enabled. this option is enabled because previously selected optimization options (such as add pipeline registers) can potentially produce numeric results that differ from the results produced by the original filter object. you can use this option to adjust the number of least significant bits the test bench ignores during comparisons before generating a warning.

    test bench tab of the generate hdl tool

  14. in the generate hdl tool, click generate to start the code generation process. when code generation completes, click close to close the tool.

    the coder displays these messages in the matlab command window as it generates the filter and test bench verilog files:

    ### starting verilog code generation process for filter: optfir
    ### generating: c:\hdlfilter_tutorials\hdlsrc\optfir.v
    ### starting generation of optfir verilog module
    ### starting generation of optfir verilog module body
    ### hdl latency is 8 samples
    ### successful completion of verilog code generation process for filter: optfir
    ### starting generation of verilog test bench
    ### generating input stimulus
    ### done generating input stimulus; length 3429 samples.
    ### generating test bench: c:\hdlfilter_tutorials\hdlsrc\optfir_tb.v
    ### please wait ...
    ### done generating verilog test bench 

    as the messages indicate, the coder creates the folder hdlsrc under your current working folder and places the files optfir.v and optfir_tb.v in that folder.

    observe that the messages include hyperlinks to the generated code and test bench files. by clicking these hyperlinks, you can open the code files directly into the matlab editor.

    the generated verilog code has these characteristics:

    • verilog module named optfir.

    • registers that use asynchronous resets when the reset signal is active high (1).

    • generated code that optimizes its use of data types and eliminates redundant operations.

    • coefficient multipliers optimized with the csd technique.

    • final summations optimized using a pipelined technique.

    • the table shows the names of the ports.

      verilog portname
      inputdata_in
      outputdata_out
      clock inputclk
      clock enable inputclk_enable
      reset inputreset
    • an extra register for handling filter output.

    • coefficients named coeffn, where n is the coefficient number, starting with 1.

    • type-safe representation is used when zeros are concatenated: '0' & '0'...

    • the postfix '_process' is appended to sequential (begin) block names.

    the generated test bench:

    • is a portable verilog file.

    • forces clock, clock enable, and reset input signals.

    • forces the clock enable input signal to active high.

    • drives the clock input signal high (1) for 5 nanoseconds and low (0) for 5 nanoseconds.

    • forces the reset signal for two cycles plus a hold time of 2 nanoseconds.

    • applies a hold time of 2 nanoseconds to data input signals.

    • applies an error margin of 4 bits.

    • for a fir filter, applies impulse, step, ramp, chirp, and white noise stimulus types.

explore the optimized generated verilog code

get familiar with the optimized generated verilog code by opening and browsing through the file optfir.v in an ascii or hdl simulator editor:

  1. open the generated verilog filter file optcfir.v.

  2. search for optfir. this line identifies the verilog module, using the value you specified for the name option in the target pane. see step 3 in configure and generate optimized verilog code.

  3. search for tutorial. this section of code is where the coder places the text you entered for the comment in header option. see step 9 in configure and generate optimized verilog code.

  4. search for hdl code. this section lists the coder options you modified in configure and generate optimized verilog code.

  5. search for filter settings. this section of the vhdl code describes the filter design and quantization settings as you specified in design the fir filter in filter designer and quantize the fir filter.

  6. search for module. this line names the verilog module, using the value you specified for the name option in the target pane. this line also declares the list of ports, as defined by options on the ports pane of the generate hdl tool. the ports for data input and output are named with the values you specified for the input port and output port options on the ports tab of the generate hdl tool. see steps 3 and 11 in configure and generate optimized verilog code.

  7. search for input. this line and the four lines that follow, declare the direction mode of each port.

  8. search for constants. this code defines the coefficients. they are named using the default naming scheme, coeffn, where n is the coefficient number, starting with 1.

  9. search for signals. this code defines the signals of the filter.

  10. search for sumvector1. this area of code declares the signals for implementing an instance of a pipelined final adder. signal declarations for four additional pipelined final adders are also included. these signals are used to implement the pipelined fir adder style optimization specified with the add pipeline registers option. see step 7 in configure and generate optimized verilog code.

  11. search for process. the block name delay_pipeline_process includes the default block postfix '_process'.

  12. search for reset. this code asserts the reset signal. the default, active high (1), was specified. also note that the process applies the default asynchronous reset style when generating code for registers.

  13. search for posedge. this verilog code checks for rising edges when the filter operates on registers.

  14. search for sumdelay_pipeline_process1. this block implements the pipeline register stage of the pipeline fir adder style you specified in step 7 of configure and generate optimized verilog code.

  15. search for output_register. this code writes the filter output to an output register. the code for this register is generated by default. in step 12 in configure and generate optimized verilog code , you cleared the add input register option, but left the add output register selected. also note that the process name output_register_process includes the default process postfix '_process'.

  16. search for data_out. this code drives the output data of the filter.

verify the generated verilog code

this section explains how to verify the optimized generated verilog code for the fir filter with the generated verilog test bench. this tutorial uses the mentor graphics® modelsim® simulator as the tool for compiling and simulating the verilog code. you can use other hdl simulation tool packages.

to verify the filter code, complete these steps.

  1. start your simulator. when you start the mentor graphics modelsim simulator, a screen display similar to the following appears.

    mentor graphics modelsim window

  2. set the current folder to the folder that contains your generated verilog files. for example:

    cd hdlsrc
  3. if desired, create a design library to store the compiled verilog modules. in the mentor graphics modelsim simulator, you can create a design library with the vlib command.

    vlib work
  4. compile the generated filter and test bench verilog files. in the mentor graphics modelsim simulator, you compile verilog code with the vlog command. these commands compile the filter and filter test bench verilog code.

    vlog optfir.v
    vlog optfir_tb.v

    this figure shows the command sequence and informational messages displayed during compilation.

    mentor graphics modelsim window

  5. load the test bench for simulation. the procedure for loading the test bench varies depending on the simulator you are using. in the mentor graphics modelsim simulator, load the test bench for simulation with the vsim command. for example:

    vsim optfir_tb

    this figure shows the results of loading optfir_tb with the vsim command.

    mentor graphics modelsim window

  6. open a display window for monitoring the simulation as the test bench runs. in the mentor graphics modelsim simulator, can use this command to open a wave window and view the results of the simulation as hdl waveforms.

    add wave *

    this wave window opens.

    mentor graphics modelsim wave window

  7. to start running the simulation, issue the start simulation command for your simulator. for example, in the mentor graphics modelsim simulator, you can start a simulation with the run command.

    this figure shows the run -all command being used to start a simulation.

    mentor graphics modelsim window

    as your test bench simulation runs, watch for error messages. if error messages appear, interpret them as they pertain to your filter design and the hdl code generation options you selected. determine whether the results are expected based on the customizations you specified when generating the filter verilog code.

    this wave window shows the simulation results as hdl waveforms.

    mentor graphics modelsim wave window

网站地图