Copyright 2008
Momentum Data Systems
17330 Brookhurst Street, Suite 230
Fountain Valley, CA 92708
World rights reserved. No part of this publication may be stored in a retrieval system, transmitted,
or reproduced in any way, including but not limited to photocopy, photograph, magnetic or other
record, without the prior agreement and written permission of Momentum Data Systems.
Information in this manual is subject to change without notice and does not represent a commit-
ment on the part of Momentum Data systems. The software described in this reference guide is
furnished under a license agreement and may be used or copied only in accordance with the terms
of the agreement.
Momentum Data Systems, provides this program and licenses its use. You assume responsibility for the selection of the
program to achieve your intended results, and for the installation, use and results obtained from the program.
LICENSE
YOU MAY NOT USE, COPY, MODIFY, OR TRANSFER THIS PROGRAM, OR ANY COPY, MODIFICATION, OR
MERGED PORTION, IN WHOLE OR IN PART, EXCEPT AS EXPRESSLY PROVIDED FOR IN THIS LICENSE. IF YOU
TRANSFER POSSESSION OF ANY COPY, MODIFICATION, OR MERGED PORTION OF THIS PROGRAM TO ANY
OTHER PARTY, YOUR LICENSE IS AUTOMATICALLY TERMINATED.
TERM
The license is effective until terminated. You may terminate it at any other time by destroying the program together with
all copies, modifications and merged portions in any form. It will also terminate upon conditions set forth elsewhere in
this Agreement or if you fail to comply with any term or condition of this Agreement. You agree upon such termination to
destroy the program together with all copies, modifications and merged portions in any form with the exception of User
Programs.
LIMITED WARRANTY
With respect to the software and physical documentation enclosed herein, Momentum Data Systems, Inc. (MDS) warrants
the same to be free of defects in materials and workmanship for period of 30 days from the date of purchase. In the event
of notification within the warranty period of defects in material or workmanship, MDS will replace the defective diskettes
or documentation. The remedy for breach of this warranty shall be limited to replacement and shall not encompass any
other damages, including but not limited to loss of profit, special, incidental, consequential, or other similar claims.
Momentum Data Systems, Inc. and its distributors specifically disclaims all other warranties, expressed or implied,
including but not limited to, implied warranties of merchantability and fitness for a particular purpose with respect to
defects in the software and documentation and the program license granted herein, in particular, and without limiting
operation of the program license with respect to any particular application, use, or purpose. In no event shall MDS and its
distributors be liable for any loss of profit or any other commercial damage including but not limited to special, incidental,
consequential or other damages.
GENERAL
You may not sublicense, assign or transfer the license or program except as expressly provided in this Agreement.
Momentum Data Systems, Inc. does not warrant that operation of the program will be uninterrupted or error-free.
MDS agrees to provide free technical support to all users that are registered owners. This support will be free of charge for
a period of at least 90 days following the date of your purchase.
This Agreement will be governed by the laws of the State of California, excluding any conflict of laws provisions.
You acknowledge that you have read this agreement, understand it, and agree to be bound by its terms and conditions. You
further agree that it is the complete and exclusive agreement between us which supersedes any proposal or prior agree-
ment, oral or written, and any other communications between us relating to the subject matter of this Agreement.
IF YOU HAVE ANY QUESTIONS CONCERNING THIS AGREEMENT, PLEASE CONTACT MOMENTUM DATA
SYSTEMS.
Filter Design for dsPIC™ DSC
Table of contents
Chapter 1 - Introduction
1.1 Design Capabilities 1-2
1.2 General System Features. 1-4
1.3 Installation 1-5
1.3.1 Application Software Installation 1-5
1.4 Input Specifications 1-6
1.5 Design Methods 1-7
1.6 System Operation 1-8
Chapter 2 - Examples
2.1 IIR Lowpass 2-2
2.2 IIR Bandpass 2-5
2.3 FIR Lowpass 2-7
2.4 Equiripple FIR Lowpass 2-10
2.5 Using a dsPICfd Filter in dsPICworks 2-12
Chapter 3 - System Operation
3.1 Toolbar 3-3
3.1.1 Status Bar 3-5
3.2 File Menu 3-6
3.2.1 Load Specification File 3-7
3.2.2 Print 3-8
3.2.3 Printer Setup 3-8
3.2.4 System Settings 3-9
3.2.5 User Information 3-10
3.2.6 Exit 3-10
3.2.7 About dsPIC‘ FD Filter Design 3-11
3.3 View Menu 3-12
3.3.1 Toolbar 13-2
3.3.2 Status Bar 3-12
3.3.3 Text Filter File View 3-13
3.4 Design Menu 3-14
3.4.1 IIR Design 3-15
3.4.2 FIR Design with Windows 3-17
3.4.3 Equiripple FIR Design 3-20
3.5 Filter Menu 3-22
3.5.1 Lowpass 3-23
3.5.2 Highpass 3-24
3.5.3 Bandpass 3-25
3.5.4 Bandstop 3-26
3.6 Output Menu 3-27
3.6.1 Plot Control 3-27
3.6.2 Snap to Grid 3-30
3.6.3 Quantization 3-30
3.6.4 Create Specification File 3-31
3.6.5 Create Filter Coefficient File 3-32
3.6.6 Create Plot Data Files 3-33
3.7 Code Generation Menu 3-34
3.7.1 Create C Code 3-35
3.7.2 C Code File Generation 3-35
3.7.3 Microchip dsPIC30F/33F Code Generation 3-35
3.8 Window Menu 3-40
3.8.1 Select Plots 3-41
3.8.2 Display Control 3-41
Chapter 4 - Filter Coefficient Files
4.1 IIR FILTER COEFFICIENT FILES 4-2
4.1.1 General Discussion on Cascaded Biquad Sections 4-3
4.1.2 Fractional Fixed Point Cascaded Biquad Sections 4-5
4.2 FIR FILTER COEFFICIENT FILE 4-9
Chapter 5 - Addendum
5.1 References 5-2
5.2 IIR Design Output Example 5-3
CHAPTE R 1 Introduction to Filter
Design for Microchip
Technology dsPIC30F/
dsPIC33F
This manual is a reference guide to the Filter Design System dsPICFd specifically
developed for use with Microchip Technology’s dsPIC30F/dsPIC33F™ family. It is
intended to explain the usage of the system and what the various screen displays and
computer printouts mean. It is not intended to be a tutorial on digital signal processing
since several excellent texts on the subject exist and it is assumed that the user has had a
certain amount of academic or professional exposure to the subject. Several references
are given in Chapter 4, should more in-depth information be required.
This software tool dsPICFd will allow the user to design coefficients for digital filters
and provides a seamless manner of getting a filter routine and its associated coefficients
into an MPLAB project.
More advanced versions of QEDesign are also available. Please call Momentum Data
Systems for further information. These systems support significantly more powerful filter
design techniques.
• The filter design program supports C callable assembly language reoutines that
implement the designed filter.
• The user creates the desired filter within dsPICfd. When a satisfactory filter has
been created, the code generator can be invoked to create the files associated with
the filter.
• An option to create a C main program to execute the filter using the simulator is
allowed.
• There is support for 32-bit coefficients in the dsPICFd system. This capability is not
available for dsPICFdlite.
2. Use of 64 bit floating point for all design calculations ensures that maximum
accuracy is maintained for the design calculations. Certain critical calculations are
carried out in 128 bit precision.
3. Specification file which allows the retention and retrieval of filter specifications.
5. Cursor tracking on all frequency domain plots. X and Y coordinates read out
automatically as the cursor is tracked on the function by holding the mouse button
down and dragging across the window as desired.
The system is automatically installed by executing the following command at the DOS
prompt or the run dialog.
For dsPICFD:
<drive>:\dsPICFD.msi
For dsPICFDLite:
<drive>:\dsPICFDLite.msi
Note: this executes SETUP.EXE on the cd which decompresses the files and installs the
software group.
This assumes the magnitude range of the passband is from [1-δ1,1], δ1>0 and δ1<1
Thus the magnitude of the stopband is from [0, δ2] and δ2<1
Note that: -20log10(1.0-δ1) and -20log10(δ2) are positive numbers. These values are often
referred to as passband ripple and stopband ripple, but a more accurate description would
be maximum passband attenuation and minimum stopband attenuation.
These forms are used throughout all IIR and FIR designs for Lowpass, Highpass,
Bandpass and Bandstop Filters.
1.0
1-δ1
δ2
Frequency
All IIR and FIR filters have their stopband and passband filters specified in this manner.
There are no unique specifications for each filter type. Such transformations if required
for design calculators are all handled automatically by the design program.
Note that the definitions of the passband and stopband values are positive numbers.
Furthermore -20log10(1.0-δ1) < -20log10(δ2) .
For the IIR design, a normalized lowpass analog transfer function is generated based on
the given filter specifications. This normalized lowpass analog transfer function is then
transformed to an unnormalized analog transfer function (lowpass,highpass,bandpass or
bandstop) via the analog frequency transformation formulae. Values for the frequency
transformation are suitably chosen to compensate for the prewarping caused by the
subsequent bilinear transformation.
The unnormalized transfer functions for Lowpass, Highpass, Bandpass and Bandstop
Filters are then transformed to the digital domain via the bilinear transformation. Details
of the transformations are printed in the output file SFIL.OUT. Output for each design
will be accumulated in this file until it is printed. The filter characteristics including the
impulse response are simulated via cascaded second order sections with the poles and
zeros grouped using the L. B. Jackson algorithm1 to minimize stability problems.
For the FIR design with windows, the number of taps in the filter are determined using
the window calculation, again based on input specifications. The coefficients for the taps
are then determined by using the Fourier series design techniques for computing the
impulse response and the window coefficients.
The Equiripple FIR design uses the Remez2 exchange algorithm to determine an optimal
solution. The solution is optimal in the minimax sense - that is the maximum error over
the entire frequency band is minimized. This produces equiripple results in both the
passband and stopband.
The dsPICFD system was designed to be intuitively easy to use without sacrificing any
of the powerful design capabilities required for a sophisticated engineering application.
The top menu bar provides all the necessary options to design filters and will save
previously inputted data thus allowing specifications to be changed with the minimum of
effort. To alter a field, simply move the cursor into the relevant field using the mouse or
Enter keys and re-enter the desired data.
The menus and dialogs are largely self-explanatory, thus allowing the system to be used
with the minimum of difficulty. The user merely selects the desired option/s by following
the menu and dialog prompts. All dialog boxes have a cancel box which will cause the
system to revert to the previous dialog box.
It is not necessary to input data in floating point format as the system will automatically
convert integers to the correct format. For example, 5000, 5000.0, and 5E03 are all equally
acceptable forms of input.
To start the system: Double-click on the dsPICFD icon to launch the system or enter the
following command:
<drive:><\path\>dsPICFD.exe
The authors of dsPICFD believe that the most efficient way to become familiar with the
implementation and potential of the system is to step through the examples provided. The
following pages contain examples of the various design methods and are organized in the
following manner:
The first example is an IIR design with an inverse Tschebyscheff analog prototype.
The frequencies are specified in Hertz. The sampling frequency is set at 5000 Hz, the
passband frequency is set at 800Hz and the stopband frequency is set to 1600 Hz. Note
that for a lowpass filter the stopband frequency must be greater than the passband
frequency and both frequencies must be less than half the sampling frequency.
Both the passband and the stopband ripples are specified in dB (see Section 1.4 ). The
ripple values are all specified as positive values. The passband value must be < the
stopband value. Typical passband values are 0.1 to 3 dB. Typical stopband values are
40dB or greater.
Inverse Tschebyscheff is selected for the analog prototype. The filter order will default
to 5 unless overridden in this dialog box.
Some interesting things to note about this filter design - the zeros on the unit circle
correspond to the frquencies in the log frequency plot where the response has a null .xxxx
This filter has relatively narrow transition bands. The Elliptic filter design method will
be significantly more efficient than other analog prototypes. In general, unless one has
a specific reason for picking a different analog prototype, the Ellliptic filer prototype s
hould be used for IIR filter design.
Note that there are ripples in both the passband and the stopband. This is characteristic
of all IIR filters that are generated using the elliptical design
Several window functions are available for FIR design. However, the Kaiser window
function is usually selected for this type of design.
The frequencies are specified in Hertz. The sampling frequency is set at 5000 Hz, the
passband frequency is set at 800Hz and the stopband frequency is set to 1600 Hz. Note
that for a lowpass filter the stopband frequency must be greater than the passband
frequency and both frequencies must be less than half the sampling frequency.
Both the passband and the stopband ripples are specified in dB (see Section 1.4 ). The
ripple values are all specified as positive values. The passband value must be < the
stopband value. Typical passband values are 0.1 to 3 dB. Typical stopband values are
40dB or greater.
The Kaiser window design is selected with a default order of 19. It appears from the filter
orders that a rectangular window would be better. However, a rectangular window will
not provide a design where the first sidelobe of the stopband will be less than 45 dB.
An FIR filter has ripples in both the passband and stopband. The ripples in the stopband
are easily seen in the magnitude plot. The ripples in the passband can be seen by zooming
in on the passband portion of the log magnitude or magnitude plot.
Also note that the phase plot is essentially a straight line and the group delay is a constant.
Both of these phenomena are characteristic of an FIR design with symmetric or anti-
symmetric coeeficients. If an FIR filter does not have symmetric or anti-symmetric
coefficients, then the group delay is not a constant.
The same lowpass filter specifications as in Examples 1 and 3 are used for this FIR lowpass
filter
The Equiripple design can make use of both ripple values in the design of the filter whereas
the FIR design with windows cannot. The FIR design with windows uses the only one
ripple value and will over design the other ripple value. The FIR design uses the smallest
ripple value. This is almost always the stopband ripple. Hence, the FIR design with
windows cannot be as efficient as the equiripple deisgn.
Note that the filter order is 11 - much less than the Kaiser window design.
The magnitude plot for this design is considerably different than the magnitude plot for
the Kaiser window design. The equiripple design makes use of the two ripple values and
thus requires fewer taps or coefficients to achieve the desired design specifications.
An FIR window design has only one ripple value for its design calculations. The smallest
ripple value will be used for the design calculation - usually this is the stopband ripple
value - hence the filter is over-designed in the passband. What that means is that the
passband ripple is smaller than requested by the user.
Just create an FLT file. Then select said file when applying a filter in dsPICworks.
System operation is controlled via the standard graphical user interface of a main menu
bar with pull-down menus and dialog boxes. The main menu bar consists of the following
entries:
Selection Description
File Allows loading of previously stored filter and analysis input
specifications
View View the design data and filter files
Design Selection of Filter Design Options
Filter Selection of Filter Types
Output Selects plotting options and output files
Codegen Generates dsPIC assembly code
Window Standard Windows Operations to select output and graphical
options
The FILE, VIEW, DESIGN, FILTER, OUTPUT, CODEGEN and WINDOW menu items
are covered in this chapter.
The toolbar allows the user to select the most commonly used features by button.
The buttons and equivalent Menu options are detailed below in the order in which they
appear on the toolbar.
Load Files
About/Printer Options
FILE/About,
FILE/Printer
WINDOW/Cascade,
WINDOW/Tile
DESIGN/IIR,
DESIGN/FIR,
DESIGN/Equiripple
FILTER/Lowpass,
FILTER/Highpass,
FILTER/Bandpass,
FILTER/Bandstop
Quantization Option
OUTPUT/Quantization
Start Design
FILTER/Start Design
Output Options
The Status Bar which appears at the bottom of the screen displays the following
information:
Status
Q16 - Resolution
Select the file menu by placing the cursor over the word FILE, click and hold the selection
by moving the cursor to the appropriate item in the drop-down menu, prior to releasing
the mouse button. Note that the enabled options are listed in black with disabled options
appearing in gray. The FILE menu appears as follows:
Certainly the most powerful feature of this menu is the ability to automatically load
previously stored specifications via FILE/Load Specifications.
If the FILE/Load Specifications option was selected, the following dialog box will be
displayed.
All filter specification files suffixed by .SPC will be displayed. A filter specification file
can be created by selecting OUTPUT/Create Specification File after a filter design is
complete. The dialog box corresponding to the current design method will be started
automatically after a specification file is selected. If necessary, the design method will be
changed to a method that can design the filter.
On completion of a filter design, filter plots can be printed. The print menu does not apply
to text windows. Text files must be printed outside of QED Lite.
Select the desired printer setup. Note these parameters are not saved after the system exits.
The printer setup box will vary according to the type of printer.
The system settings dialog box controls global aspects of dsPIC FD operations.
The text in the User Information will be displayed on the graphical displays and printouts.
3.2.6 Exit
Displays the current version number and technical support information. Technical support
is available for customers subscribing to maintenance contracts - please contact our sales
department for further information.
3.3.1 Toolbar
All files can be read in text format. Simply select the file type by scrolling through the
available file types. Text files cannot be printed within dsPIC FD.
The following menu is the primary means of starting the design of digital filters.
The various design methods and filter types will be discussed in some detail although, it
should be noted, that some filter types are specific to a particular design method.
An output file (SFIL.OUT) is automatically created showing detailed calculations for the
designs.
Selects the system design method to Infinite Impulse Response (IIR) design. IIR designs
all have feedback loops in the difference equations that implement the filters - that is the
output value in a linear function of previous output values as well as current and previous
input values. IIR designs are classical filter designs using analog design methods which
transform the resulting S domain filter to a Z domain filter.
Filter Type
IIR designs for Lowpass, Highpass, Bandpass and Bandstop filters all use analog
prototypes. After selecting “Next” on the IIR Design Menu the following dialog box will
be displayed.
The desired order is an optional field. If nothing is specified, then the order associated
with the selected analog filter type is used. The desired order field can be any positive
integer (must be a multiple of 2 for bandstop or bandpass filters) and is not restricted to
merely incrementing the filter order.
Sets the system design method to Finite Impulse Response (FIR) Windows design. FIR
designs produce a filter in which difference equations do not have feedback loops - that
is, the output value is a function only of current and previous input values. Previous output
values are not part of these filter calculations. The Windows type of FIR design is done
by forming a truncated Fourier Series to represent the desired frequency response and
'windowing' or multiplying the resulting coefficients element by element with a window
function to reduce the Gibbs effect of truncating the Fourier series. The dsPIC FD system
sets the filter coefficient symmetries such that all FIR filters have linear phase.
The system allows the specification of both the baseband ripple and the stopband ripple
for filters. However, for window designs, the system selects the minimum ripple (smallest
in linear sense) and uses that value for both the stopband and passband ripples. This
restriction is an inherent mathematical restriction of the FIR window design.
Select Window Function and Filter Order
After the system accepts specifications for either Lowpass, Highpass, Bandpass, or
Bandstop, a dialog box similar to the following dialog box is displayed with the estimated
order for each of the supported window functions.
The dsPIC FD system defaults to the Kaiser Window.
The desired filter length is optional --if left blank the system will set the number of taps
equal to the estimate. The estimate can be overridden by any value from 2 to 513.
In selecting a window function for a filter, the number of taps cannot be used as a sole
decision factor. Most of the Windows have a fixed falloff to the first sidelobe of the
stopband - this value is the stopband attenuation in the following table:
For the Windows with fixed stopband attenuation longer filter lengths reduces the
transition bandwidth. The rectangular window is essentially no windowing and illustrates
the Gibbs effect on truncating a Fourier series. Windows marked as ‘Selectable’ allow
variable falloff to the main sidelobe and variable transition bands.
Selecting this menu item will set the system design method to Equiripple FIR design.
This method is also known as the Parks-McClellan algorithm.
The following design box will be displayed during the design calculations for Lowpass,
Highpass, Bandpass, and Bandstop filter designs. The estimated number of taps will be
accepted as a default if a desired number of taps is not entered.
The number of taps for Equiripple designs can be any number from 3 to 513.
Equiripple FIR Filter Design Limitations
System specifications in Equiripple design may not be met for a variety of reasons. The
algorithm appears to terminate normally in these cases, but the results do not give the
desired results. Typically the gain will exceed 1.0 even though this is the expected
maximum gain.
If the detailed magnitude and log magnitude report was requested, the values can be
examined to see how far from the desired value the computations are.
The following dialog box appears if Lowpass was selected from the DESIGN menu or
FILTER/Lowpass was selected. All fields are mandatory and a valid entry must be in
each of them. Frequencies are in Hertz. The stopband and passband frequencies must be
less than the sampling frequency/2.
The passband frequency of a lowpass filter must be less than the stopband frequency. The
attenuation or ripple of the stopband and passband is expressed in dB. While both the
passband ripple and the stopband ripple are required, the filter order and results are
determined using the stopband ripple only for FIR design with Windows. Both fields are
required in case this specification is used in either an IIR design or an FIR Equiripple
(Parks-McClellan) design. A typical range of values for passband ripple or attenuation is
0.01dB to 3dB. A typical range of values for stopband ripple is 20dB to 100dB. The
passband ripple must be less than the stopband ripple.
The following dialog box appears if Highpass was selected from the DESIGN menu or
FILTER/Highpass was selected. The passband frequency must be greater than the
stopband frequency. Both the stopband frequency and the passband frequency must be
less than the sampling frequency/2. The passband ripple or attenuation must be less than
the stopband ripple. Typical values for passband ripples range from .01dB to 3dB while
typical values for the stopband ripple range from 20dB to 100dB.
Furthermore, the passband frequency of a highpass filter must be greater than the stopband
frequency. The attenuation or ripple of the stopband and passband is expressed in dB.
While both the passband ripple and the stopband ripple are required, the filter order and
results are determined using the stopband ripple only for FIR design with Windows. Both
fields are required in case this specification is used in either an IIR design or an FIR
Equiripple (Parks-McClellan) design. A typical range of values for passband ripple or
attenuation is 0.01dB to 3dB. A typical range of values for the stopband ripple is 20dB
to 100dB.
The following dialog box appears if a bandpass filter was selected. There are two
frequencies for the passband and two frequencies for the stopband. All frequencies must
be less than the sampling frequency/2. The first passband frequency must be less than the
second passband frequency and the first passband frequency must be greater than the first
stopband frequency. Similarly, the second passband frequency must be less than the
second stopband frequency.
While both the passband ripple and the stopband ripple are required, the filter order is
determined for FIR design with Windows using the stopband ripple only. Both fields are
required in case this specification is used in either an IIR design or an FIR Equiripple
(Parks-McClellan) design. For an Equiripple design, the transition regions are set inter-
nally such that the transition regions are symmetric. This is required to design an
acceptable equiripple bandpass filter.
The following dialog box appears if a Bandstop filter was selected on the DESIGN menu
or FILTER/Bandstop was selected. This dialog box is virtually identical to the Bandpass
dialog box. However, different design rules apply
• The first passband frequency must be less than the second passband frequency and
the first stopband frequency must be less than the second stopband frequency.
• The first passband frequency must be less than the first stopband frequency and the
second stopband frequency must be less than the second passband frequency.
While both the passband ripple and the stopband ripple are required, the filter order and
results are determined using the stopband ripple only for FIR design with Windows. Both
fields are required in case this specification is used in either an IIR design or an FIR
Equiripple (Parks-McClellan) design.
The OUTPUT menu contains a variety of options related to data output - graphical,
hardcopy, or data files.
Selecting OUTPUT/ Plot Control causes the following dialog box to be displayed:
The Plot control dialog box allows the plotting limits to be changed for a specific filter .
All of the Check Box options are global system settings. This means that the option once
set will remain in effect until explicitly reset via this dialog box.
The settings are not activated until the next replot. This means, for example, that to see
the change from linear frequency scale to log frequency scale, the ‘Accept’ button must
be selected to force a replot.
• Frequency Range
The system defaults to a range of [0,fs/2] where fs is the sampling frequency. The
purpose of the frequency selection is to allow one to narrow in on a transition area
from passband to stopband. However, it is not restricted to this. One can demon-
strate the periodicity of the magnitude and phase response by specifying a range that
is greater than [0,fs].
• Magnitude Range
The standard range of values for the magnitude plot is from zero to one. However,
this can be changed to any value between 0 and 10. Typically this can be used to
look at the passband ripple characteristics or the stopband ripple.
• Log Magnitude Range
The standard range of values for the Log Magnitude plot is -100 dB to 0 dB and
these can be changed to any values between -200dB and +10dB.
• Number of Time Steps
For the IIR design, the system by default will compute the impulse response and step
response for 300 time steps. This can be overridden by entering a different number
Option Range
Frequency 0 to fs/2
Magnitude 0 to 1
Log Magnitude -100 to 0
Number of time steps 300
If the design has been completed and just the plotting ranges are to be changed, then
changes in these values will be reflected in the plots after selecting the done button.
If the option is deselected then the values in the plotting ranges will be used for all
subsequent plots even if the problem is changed. This allows fixed plotting ranges
independent of problem definition.
Zooms are achieved by clicking with the right mouse button and then dragging the mouse
to form a rectangle. Releasing the right button will cause the plot to be zoomed to that
rectangle.
If the Snap To Grid option is on, the largest rectangle formed by the gridlines which
completely encloses the rectangle drawn by dragging the mouse, will be the new plot
values.
3.6.3 Quantization
Select either 16 or 32 bits. Setting the number of bits to 32 will force usage of the
extended precision IIR Filter. This value must be 16 for FIR filters. Note that the
extended precision IIR filter is not available in the dsPICFDlite system. If the num-
ber of bits is 32, only the canonic second order section case can be selected for real-
ization type.
Realization Type for Cascaded Sections
w i1 ( n ) b a i1 w i2 ( n – 1 )
k
- = -----i1k- x ( n ) + ------
--------------- k
y ( n ) + ------------------------
k
- (EQ 1)
2 2 2 2
w i2 ( n ) b a i2
- = -----i2- x ( n ) + ------
--------------- y(n)
k k k
2 2 2
The input to the section is x(n) and the output of the section is y(n)
The output of section i is the input of section i + 1
i.e. the y(n) of section i is the x(n) of section i+1
• Canonic Second Order Sections
Cascaded canonic sections requires scaling down the input signal to the first section.
This is due to the feedback in the first summing node. This form of the difference
equations results in a simpler implementation. However, scaling down the input
reduces the Signal to Noise Ratio. Unless the number of clock cycles is an important
issue, it is recommended that transpose canonic sections be used.
The difference equations for section i are:
wi ( n ) ( n ) ------
a a i2
- = x----------
------------ + i1k w i ( n – 1 ) + ------ w (n – 2)
k k k i
2 2 2 2
y(n) b i0 b i1 b i2
----------
k
= ------ w ( n ) + ------k w i ( n – 1 ) + ------k w i ( n – 2 )
k i
(EQ 2)
2 2 2 2
The dsPICFD system allows a filter specification to be saved in a file. If this option is
selected, a subsequent dialog box will request the file name. The file will be created with
an SPC suffix.
The dsPIC FD system allows filter coefficients to be saved in a file. If this option is
selected, a subsequent dialog box will request the file name. The file will be created with
an FLT suffix.
This file is only used for applying a filter created in dsPICworks. It is not necessary to
understand the details of this file in using the dsPIC FD design program. Please refer to
Chapter 4 for detailed information on the Filter Coefficients.
The data which generates the current plots can be written to a set of files. This allows
processing in other visualization or computational applications. All files will have the
same prefix with differing suffixes.
There are two Code Generation options - generating C Code or Microchip’s dsPIC
assembly language code.
C Code can be created for any filter. A fully documented C program which can be used
as either a subroutine or main program is generated. This C program uses two include
files: qed_filt.c and qed_cgen.h. The filter routines are in qed_filt.c and the function
prototypes and structure definitions are defined in qed_cgen.h.
The generated C code is most appropriately used in a floating point DSP or a personal
computer.
The dsPIC FD system supports optimized assembly language code generation specifically
for Microchip’s dsPIC30F/33F processor. Select thedsPIC30F/33F option as shown
below.
This assembly file contains the assembly definition of the filter structure, the filter
coefficients and a code fragment showing how to call the generalized filter routine.
Assuming that the name entered in the file save dialog is <FilterName>, the generated
assembly file is <FilterName>.s".
The "Use General Subroutine" radio button specifies that the filter source routine to be
used is one of the following:
• FIR_Filter.s (Used in the case of FIR Filters)
• IIRT_Filter.s (Used in the case of Transposed IIR Filters)
• IIRC_Filter.s (used in the case of Canonic IIR Filters)
• IIRC_FilterExtendedPrecision.s (used in the case of Extended Precision IIR Filters)
These general routines reside in the dsPICFD setup folder depending on the filter type
chosen (FIR, IIR Transpose, IIR Canonic or IIR Canonic Extended Precision). The
respective general subroutine along with the generated <FilterName>.s assembly file
must be added to the MPLAB Integrated Development Environment (IDE) project. The
general subroutines will use the filter structure and filter coefficients present in the
generated FilterName.s file as inputs.
The C support files option allows creation of a C header file: <FilterName>.h. This file
is automatically created if the C wrapper option is selected for simulation of the filter.
.hword TestCGNumSections-1
.hword TestCGCoefs
.hword 0xFF00
.hword TestCGStates1
.hword TestCGStates2
.hword 0x0000
; ..............................................................................
; Sample assembly language calling program
; The following declarations can be cut and pasted as needed into a program
; .extern _IIRTransposedInit
; .extern _IIRTransposed
; .extern _TestCGFilter
; .section .bss
; The input and output buffers can be made any desired size
; the value 40 is just an example - however, one must ensure
; that the output buffer is at least as long as the number of samples
; to be filtered (parameter 1)
;input: .space 40
;output: .space 40
; .text
;; This code can be copied and pasted as needed into a program
Set up pointers to access input samples, filter taps, delay line and
; output samples.
; mov #_TestCGFilter, W0; Initalize W0 to filter structure
; call _IIRTransposedInit; call this function once
;
; The next 4 instructions are required prior to each subroutine call
to _IIRTransposed
; mov #_TestCGFilter, W3; Initalize W3 to filter structure
; mov #input, W2; Initalize W2 to input buffer
; mov #output, W1; Initalize W1 to output buffer
; mov #20, W0 ; Initialize W0 with number of required output samples
; call _IIRTransposed; call as many times as needed
File Name Filter type Filter Routine Name Input Routine Name # Bits/Coefficientt
FIR_Filter.s FIR _FIR _FIRInit 16
IIRT_Filter.s Transposed IIR _IIRTransposed _IIR TransposedInit 16
IIRC_Filter.s Canonic IIR _IIRCanonic _IIRCanonicInit 16
IIRC_FilterExtende Canonic IIR _IIRCanonicExtend _IIRCanonicExtend 32
dPrecision.s edPrecision.s edPrecision
For the 16-bit filters, the transposed IIR routine is the preferred choice for IIR filters
(called form 1 in the quantization menu). This routine uses more instructions than the
Canonic Routine (Form 2). However, it behaves significantly better for filters where the
poles are close to the unit circle.
These routines have the same calling sequence, filter names and structure names as the
DSP library. (See MPLAB C30 HELP File).
There are 3 likely scenarios for using the generated code from dsPICFD. These are:
1. Test the filter with an impulse response in a standalone project
2. Add the filter to an existing project with the filter routines called by C Code
3. Add the filter to an existing project with the filter routines called by asm code
For the first two cases, select the optional C Code file generation.
In the event of Case 1), create a standalone project wiht the generated files and one of the
filter files described in Table 3.5.
In the event of Case 2), just add the generated C Header file. The MPLAB plugin may
have added the main to the project. In this case, delete the main program.
For case 3, do not generate any C files. The generated asembly file can be used by
following the instructions printed in the comments at the bottom of the file.
Note that there is no library routine for the Extended Precision version of the CanonicIIR
routine. The supplied routine must be used for this capability. Also note that this capability
is not available for dsPICFdlite.
Clicking the Select Plots option causes the following menu item to be displayed:
Plots which have been deselected using the Cancel window button can be reselected using
this dialog box. All selected plots are indicated by a check mark.
Allows the selection of color, font and line style for plots and prints (Note: hardcopy
printouts are black and white only). The following dialog boxes are provided:
Filter coefficient files can be generated from the design system. The filter coefficient
files contain the values of the quantized coefficients of the digital filters intended for
actual implementation of a digital filter. Each file is an ASCII file. As such, these files
can be processed by any text editor or application program.
The filter coefficient files are used as the interface file to dsPICworks. These files can be
used by dsPICworks to apply a filter designed by dsPICFD or dsPICFDlite to any time
domain signal. Alternatively, one can execute the generated C program and create a file
that can be imported into dsPICworks.
This supported format for IIR filter coefficient files is: Fractional fixed point - cascaded
biquad sections
The options are selected from the Quantization Option screen and the method of realiza-
tion.
Let
N
2
b i0 z + b i1 z + b i2
H ( z ) = Hd ∏ ----------------------------------------
2
a i0 z + a i1 z + a i2
(EQ 1)
i=1
the transfer function of second order sections, where N is the number of second order
sections. H(z) can also be written as:
N
–2
b i0 + b i1 z – 1 + b i2 z
H ( z ) = Hd
∏ -----------------------------------------------
a i0 + a i1 z – 1 + a i2 z
–2
(EQ 2)
i=1
The z-1 terms represent a delay of 1 time period and the z-2 terms represent a delay of 2
time periods. The denominator coefficients have been complemented to allow adds in
the difference equation. First order sections are treated as second order sections by
multiplying both the numerator and denominator by z or in effect, setting the second order
delays to zero.
In converting from the floating point to fixed point format, the number of bits is used to
determine the number of significant positions in the fraction. Then bi0, bi1, bi2 are forced
to the same exponent value. This exponent value becomes the numerator shift count.
Similarly, ai1, ai2 are shifted until they have the same exponent value. This exponent
value becomes the denominator shift count. If a shift count is less than or equal to zero,
the number is a fraction. If the shift count is greater than zero, the absolute value of the
number is greater than 1. Coefficient ai0 is assumed to have a value of 1.0. Denominator
shift counts are normally greater than or equal to zero.
If the number of quantized bits is 'n', then the fractional coefficient is in the rightmost 'n'
bits. Also note that negative numbers are represented in their 2's complement form.
To implement a biquad section, it is necessary to align the binary point of the fractional
values prior to accumulating intermediate sums. This can be done by ensuring that each
operand in a sum has the same shift count or exponent. The final output of biquad sections
should have a shift count of 0 to avoid scaling in the subsequent biquad section.
Let x(n) be the input to the i'th section and y(n) be the output of the i'th section. Then the
difference equations for implementing cascaded second order sections in cascaded form
I (Transpose) are:
y ( n ) = b i0 x ( n ) + w i1 ( n – 1 )
w i1 ( n ) = b i1 x ( n ) + a i2 y ( n ) + w i2 ( n – 1 ) (EQ 3)
w i2 ( n ) = b i2 x ( n ) + a i2 y ( n )
Note: ai1 and ai2 are complemented in the coefficient file so an add is used instead of a
subtract. Hd is 1.0 for Direct Form I. .
The difference equations for cascaded second order sections in cascaded form II (Canonic)
are:
(EQ 4)
w i ( n ) = x ( n ) + a i1 w i ( n – 1 ) + a i2 w i ( n – 2 )
y ( n ) = b i0 w i ( n ) + b i1 w i ( n – 1 ) + b i2 w i ( n – 2 )
The discussion of implementation details in Section 4.1.1 is pertinent here, with the
following exception: all coefficients except ai0 have initially been scaled so that the
absolute value of the coefficient is less than 1.0. The largest coefficient in absolute value
has the maximum precision allowed. Coefficients with smaller absolute values may have
fewer bits to represent them. If the shift count is zero then the difference equations can
be used as specified in Section 4.1.1 .
The binary points must be aligned prior to the addition of two values. This means that
shift counts must be identical for this operation. While the filter is designed so that the
final gain is 0dB or the maximum range is -1 to +1, filter coefficients can be greater than
1.0 and intermediate results can be greater than 1.0. Coefficients are divided by a power
of 2 (i.e. 21, 22, 23, etc.) until the coefficients are less than 1.0. The power of 2 is the shift
count. The numerator values have been rescaled to prevent or minimize overflow. This
rescaling is realization dependent. Thus, form I coefficients are used in
form I equations, etc. Usually, the shift count is zero or one. However, it may exceed 1
for certain values particularly if cascaded canonic form II is used.
This discussion implies using intermediate shifts for implementation of the difference
equations. This may not, however, be necessary. It depends on how the difference
equations are implemented.
Cascaded Transpose (Form I)
A positive shift count k for a section means that the difference equations for form I are
actually as follows:
y(n) b i0 w i1 ( n – 1 )
----------
k
= ------k x ( n ) + ------------------------
k
-
2 2 2
w i1 ( n ) b a i1 w i2 ( n – 1 )
- = -----i1k- x ( n ) + ------
--------------- y ( n ) + ------------------------
- (EQ 5)
k k k
2 2 2 2
w i2 ( n ) b i2 a i2
---------------
k
- = ------
k
x ( n ) + -----k- y ( n )
2 2 2
Note: y(n) is the output of section i, is x(n) the input to section i + 1; also y(n) not y(n)/
2k, is needed in the computation for section i. So the true value of y(n) is formed by a
left shift of k bits and this value is used for the y(n) computations as well as for input to
the next section as x(n). The ai1 and ai2 have been complemented in the FLT file so an
add instead of a subtract is used. Hd, the overall gain, is normally 1.0 and can, therefore,
be ignored.
wi ( n ) x(n) a a i2
------------- = ---------- + -----i1- w i ( n – 1 ) + ------ w (n – 2)
k k k k i
2 2 2 2
(EQ 6)
y(n) b b i1 b i2
---------- = -----i0- w i ( n ) + ------ w ( n – 1 ) + ------ w (n – 2)
k k k i k i
2 2 2 2
1. The incoming signal x(n), must be right shifted k bits
2. Also the intermediate value wi(n)/2k must be shifted by k bits to get the true value
of wi(n).
3. Furthermore, y(n)/2k must be left shifted by k bits to obtain the true value for y(n).
4. y(n), the output of section i is x(n) the input for section i + 1, thus there is a left
shift on output of section i and a right shift on input to section i + 1. Clearly, these
two shifts can be combined into one shift. Also, ai1 and ai2 have been complemented.
5. Hd, the overall gain, is usually less than 1.0 and must be used as a multiplier of the
incoming signal prior to the first section, otherwise overflow will result.
Figure 4 - 1 and Figure 4 - 2 are examples of coefficient files for Cascade Forms 1 and
2 respectively.
For Form II this is the scale down factor for the incoming sample. To find the actual value
of the scale down factor, just shift the overall gain value by the shift count. If the shift
count is negative, shift left. If the shift count is positive, shift right.
Next are 6 values for each section, shift count, b0, b1, b2, a1, a2. The shift count for each
section shows the power of 2 that was needed to get the absolute value of each coefficient
less than 1. The actual values for b0, b1, b2, a1, a2 have been divided by this shift count,
and all values are displayed in both demical and hexadecimal.
Following the display of lines, a fixed point for each sections are three columns of floating
point numbers. The first column is the floating point equivalent of the fixed point
coefficients. The second column is the hexadecimal value of the quantized floating point
coefficients. The third column is the original coefficients and hence may have values >1.0
in absolute value.
The simplest way to implement an FIR filter is to use the following difference equation:
y(n) =
∑ h ( i )x ( n – i ) (EQ 7)
i=0
where N = filter order. See Figure 4 - 3 for an example of the coefficient file.
The first three lines are comments followed by four lines representing the number of taps
in decimal, hexadecimal and number of bits in quantized coeffiicients. Lines 8 and 9 are
essentially the overall gain factor. The next N lines are the quantized coefficients with
the first column being the decimal form and the second column the hexadecimal form of
the coefficient. The next N lines are the quantized coefficients in floating point form with
column 1 being the decimal value and column 2 the hexadecimal format of the number.
93 5D /* coefficient of tap 22 */
0.2838134765625000E-02 3F67400000000000/* coefficient of tap 0 */
-.9277343750000000E-02 BF83000000000000/* coefficient of tap 1 */
-.1940917968750000E-01 BF93E00000000000/* coefficient of tap 2 */
0.0000000000000000E+00 0000000000000000/* coefficient of tap 3 */
0.4232788085937500E-01 3FA5AC0000000000/* coefficient of tap 4 */
0.4580688476562500E-01 3FA7740000000000/* coefficient of tap 5 */
-.3472900390625000E-01 BFA1C80000000000/* coefficient of tap 6 */
-.1282348632812500E+00 BFC06A0000000000/* coefficient of tap 7 */
-.7174682617187500E-01 BFB25E0000000000/* coefficient of tap 8 */
0.2122802734375000E+00 3FCB2C0000000000/* coefficient of tap 9 */
0.5764465332031250E+00 3FE2724000000000/* coefficient of tap 10 */
0.7444763183593750E+00 3FE7D2C000000000/* coefficient of tap 11 */
0.5764465332031250E+00 3FE2724000000000/* coefficient of tap 12 */
0.2122802734375000E+00 3FCB2C0000000000/* coefficient of tap 13 */
-.7174682617187500E-01 BFB25E0000000000/* coefficient of tap 14 */
-.1282348632812500E+00 BFC06A0000000000/* coefficient of tap 15 */
-.3472900390625000E-01 BFA1C80000000000/* coefficient of tap 16 */
0.4580688476562500E-01 3FA7740000000000/* coefficient of tap 17 */
0.4232788085937500E-01 3FA5AC0000000000/* coefficient of tap 18 */
0.0000000000000000E+00 0000000000000000/* coefficient of tap 19 */
-.1940917968750000E-01 BF93E00000000000/* coefficient of tap 20 */
-.9277343750000000E-02 BF83000000000000/* coefficient of tap 21 */
0.2838134765625000E-02 3F67400000000000/* coefficient of tap 22 */
2. Bernhardt, Paul A. Simplified Design of High Order Recursive Group Delay Filters,
IEEE Transactions on Acoustics, Speech, and Signal Processing. Vol. ASSP-29, No.
5, October 1980.
3. Crochiere, R. E. & Rabiner L. R., Multirate Digital Signal Processing: Prentice Hall,
1983.
5. Harris F.J., On the Use of Windows for Harmonic Analysis with the Discrete Fourier
Transform. Proc. IEEE, Vol 66, No. 1, pp. 51-83, Jan 1978.
9. Kaiser, J. F. Nonrecursive Digital Filter Design using the IO - SinH Window Function:
Proceedings of IEEE International Symposium on Circuits and Systems, 1984.
10. Nuttal A. H. Some Windows with Very Good Sidelobe Behavior, IEEE Trans. Acoust.,
Speech and Signal Processing, vol ASSP-29, No. 1, pp 84-91, Feb. 1981.
11. Oppenheim, Alan V. & Shafer, Ronald W. Digital Signal Processing: Prentice Hall,
1975.
12. Parks, T.W. & Burrus C.S. Digital Filter Design. John Wiley & Sons, Inc., 1987.
13. Proakis John G & Manolakis Dimitris G. Digital Signal Processing Principles,
Algorithms, and Applications. 2nd Edition. Macmillan Publishing, New York, 1992.
14. Rabiner, Lawrence R & Gold, Bernard. Theory & Application of Digital Signal
Processing: Prentice Hall, 1975.
15. Roberts, Richard A & Mullis, Clifford T. Digital Signal Processing: Addison-Wesley,
1987.
16. Webster R.J. On Qualifying Windows for FIR Filter Design, IEEE Trans. Acoust.,
Speech & Signal Processing, vol. ASSP-31, no. 1, pp 237-240, Feb. 1983.
17. Charles Schuler and Mahesh Chugani. Digital Signal Processing: A Hands-On
Approach: Glencoe/McGraw-Hill, 2003
N
2
b i0 s + b i1 s + b i2
H(z) = Hd ∏ ----------------------------------------
2
s + a i1 s + a i2
(EQ 8)
i=1
2 2
s +ω
s → ⎛ ----------------0-⎞ (EQ 9)
⎝ Bs ⎠
where B is the bandwidth and ω0 is the center frequency. These values are pre-
warped for the bilinear transformation. Note: the order for the unnormalized trans-
fer function U(s) doubles for a Lowpass to Bandpass transformation. U(s) is the
product of the 2nd order sections.
4. The digital transfer function H(z) formed from the unnormalized transfer function
U(s) by applying the bilinear transformation H(z) is the product of the 2nd order
sections.
N N
⎛ b i0 z + b i1 + b i2⎞
2 –1 –2
( b i0 + b i1 z + b i2 z )
H ( z ) = Hd ∏ ⎜ -------------------------------------
⎝ z + a i1 + a i2 ⎠
2
-⎟ = H d
∏ ----------------------------------------------------
–1
( 1 + a i1 z + a i2 z )
–2
(EQ 10)
i=1 i=1
8. The maximum and minimum impulse response as calculated by simulating the filter
response to an impulse input response.