basic fir filter -凯发k8网页登录

main content

basic 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 a fir filter in filter designer

this section assumes that you are familiar with the matlab user interface and the filter designer. these instructions guide you through designing and creating a basic fir filter using 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 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, check that these filter options are set.

    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 filter.

  5. if you modified options listed in step 4, click design filter. the filter designer creates a filter for the specified design and displays this message 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 filter

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

  1. open the basic fir filter design you created in design a fir filter in filter designer.

  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 showing filter response and filter arithmetic menu

  3. select fixed-point from the filter arithmetic 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 showing filter response and filter precision options

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

    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
  4. click apply.

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

configure and generate vhdl code

after you quantize your filter, you are ready to configure coder options and generate vhdl code for the filter. this section guides you through starting the filter design hdl coder ui, setting options, and generating the vhdl code and test bench for the basic fir filter you designed and quantized in design a fir filter in filter designer and quantize the 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. find the filter design hdl coder online help.

    1. in the matlab window, click the help button in the toolbar or click help > product help.

    2. in the contents pane of the help browser, select the filter design hdl coder entry.

    3. minimize the help browser.

  3. in the generate hdl tool, click the help button. a small context-sensitive help window opens. the window displays information about the tool.

  4. close the help window.

  5. place your cursor over the folder label or text box in the target pane of the generate hdl tool, and right-click. a what's this? button appears.

    portion of generate hdl tool with a what's this pop up button

  6. click what's this? the context-sensitive help window displays information describing the folder option. configure the contents and style of the generated hdl code, using the context-sensitive help to get more information as you work. a help topic is available for each option.

  7. in the name text box of the target pane, replace the default name with basicfir. this option names the vhdl entity and the file that contains the vhdl code for the filter.

    target section of generate hdl tool

  8. select the global settings tab of the ui. then select the general tab of the additional settings section of the ui. type tutorial - basic fir filter in the comment in header text box. the coder adds the comment to the end of the header comment block in each generated file.

    global settings tab of 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 like this figure.

    ports tab of the generate hdl tool, with the add input register option unchecked

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

    test bench tab of the generate hdl tool

  13. click generate to start the code generation process.

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

    ### starting vhdl code generation process for filter: basicfir
    ### generating: c:\hdlfilter_tutorials\hdlsrc\basicfir.vhd
    ### starting generation of basicfir vhdl entity
    ### starting generation of basicfir vhdl architecture
    ### hdl latency is 2 samples
    ### successful completion of vhdl code generation process for filter: basicfir
    ### starting generation of vhdl test bench
    ### generating input stimulus
    ### done generating input stimulus; length 3429 samples.
    ### generating test bench: c:\hdlfilter_tutorials\hdlsrc\basicfir_tb.vhd
    ### please wait ...
    ### done generating vhdl test bench
    

    as the messages indicate, the coder creates the folder hdlsrc under your current working folder and places the files basicfir.vhd and basicfir_tb.vhd 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 vhdl code has these characteristics:

    • vhdl entity named basicfir.

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

    • the table shows the names of the ports.

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

    • clock input, clock enable input, and reset ports are of type std_logic and data input and output ports are of type std_logic_vector.

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

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

    • registers are generated with the statement elsif clk'event and clk='1' then rather than with the rising_edge function.

    • the postfix '_process' is appended to process names.

    the generated test bench:

    • is a portable vhdl 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.

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

  14. when you have finished generating code, click close to close the generate hdl tool.

explore the generated vhdl code

get familiar with the generated vhdl code by opening and browsing through the file basicfir.vhd in an ascii or hdl simulator editor.

  1. open the generated vhdl filter file basicfir.vhd.

  2. search for basicfir. this line identifies the vhdl module, using the value you specified for the name option in the target pane. see step 5 in configure and generate vhdl code.

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

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

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

  6. search for entity. this line names the vhdl entity, using the value you specified for the name option in the target pane. see step 5 in configure and generate vhdl code.

  7. search for port. this port declaration defines the clock, clock enable, reset, and data input and output ports. the ports for clock, clock enable, and reset signals are named with default character vectors. the ports for data input and output are named as you specified on the input port and output port options on the ports tab of the generate hdl tool. see step 12 in configure and generate vhdl code.

  8. search for constants. this section 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 section of code defines the signals for the filter.

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

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

  12. search for elsif. this code checks for rising edges when the filter operates on registers. the default elsif clk'event statement is used instead of the optional rising_edge function.

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

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

verify the generated vhdl code

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

to verify the filter code, complete these steps:

  1. start your mentor graphics modelsim simulator.

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

    mentor graphics modelsim transcript window

  3. if desired, create a design library to store the compiled vhdl entities, packages, architectures, and configurations. in the mentor graphics modelsim simulator, you can create a design library with the vlib command.

    mentor graphics modelsim transcript window

  4. compile the generated filter and test bench vhdl files. in the mentor graphics modelsim simulator, you compile vhdl code with the vcom command. these commands compile the filter and filter test bench vhdl code.

    vcom basicfir.vhd
    vcom basicfir_tb.vhd
    

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

    mentor graphics modelsim transcript 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, you load the test bench for simulation with the vsim command. for example:

    vsim work.basicfir_tb

    this figure shows the results of loading work.basicfir_tb with the vsim command.

    mentor graphics modelsim transcript window

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

    mentor graphics modelsim transcript window, showing the

    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 starting a simulation with the run -all command.

    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 vhdl code.

    this wave window shows the simulation results as hdl waveforms.

    mentor graphics modelsim wave window

网站地图