1
\$\begingroup\$

I have to make a digital filter with a dsPIC30F, but I don't know much about it. I've read part of the documentation about the dsPIC30F and the DSP Library, and I found a page on Microchip website where I found some example codes for dsPICs. I need to make a digital filter and I found there a code that implements a FIR filter with DSP Library. The code where they define the FIRStruct variable is the following:

; ..............................................................................
;    File   lowpassexample_psv.s
; ..............................................................................

                .equ lowpassexample_psvNumTaps, 75

; ..............................................................................
; Allocate and initialize filter taps

;               .section .lowpassexample_psvconst, "x"  ;<-Syntax supported in MPLAB C30
                                            ;v1.20 and before
                .section .lowpassexample_psvconst, code ;<-Syntax supported in MPLAB C30
                                            ;v1.30 and later
                .align 256

lowpassexample_psvTaps:
.hword  0xFFFA, 0xFFFB, 0x0000, 0x000B, 0x0017, 0x001E, 0x0017, 0x0000, 0xFFDF
.hword  0xFFBF, 0xFFB2, 0xFFC7, 0x0000, 0x004F, 0x0095, 0x00AC, 0x007A, 0x0000
.hword  0xFF61, 0xFEDA, 0xFEB2, 0xFF16, 0x0000, 0x012E, 0x022B, 0x0277, 0x01BD
.hword  0x0000, 0xFDB7, 0xFBB4, 0xFAF3, 0xFC45, 0x0000, 0x05CF, 0x0CB1, 0x1337
.hword  0x17E1, 0x1993, 0x17E1, 0x1337, 0x0CB1, 0x05CF, 0x0000, 0xFC45, 0xFAF3
.hword  0xFBB4, 0xFDB7, 0x0000, 0x01BD, 0x0277, 0x022B, 0x012E, 0x0000, 0xFF16
.hword  0xFEB2, 0xFEDA, 0xFF61, 0x0000, 0x007A, 0x00AC, 0x0095, 0x004F, 0x0000
.hword  0xFFC7, 0xFFB2, 0xFFBF, 0xFFDF, 0x0000, 0x0017, 0x001E, 0x0017, 0x000B
.hword  0x0000, 0xFFFB, 0xFFFA

; ..............................................................................
; Allocate delay line in (uninitialized) Y data space

;               .section .ybss,  "b"            ;<-Syntax supported in MPLAB C30
                                            ;v1.20 and before
                .section .ydata, data, ymemory  ;<-Syntax supported in MPLAB C30
                                            ;v1.30 and later
                .align 256

lowpassexample_psvDelay:
                .space lowpassexample_psvNumTaps*2

; ..............................................................................
; Allocate and intialize filter structure

                .section .data
                .global _lowpassexample_psvFilter

_lowpassexample_psvFilter:
.hword lowpassexample_psvNumTaps
.hword psvoffset(lowpassexample_psvTaps)
.hword psvoffset(lowpassexample_psvTaps)+lowpassexample_psvNumTaps*2-1
.hword psvpage(lowpassexample_psvTaps)
.hword lowpassexample_psvDelay
.hword lowpassexample_psvDelay+lowpassexample_psvNumTaps*2-1
.hword lowpassexample_psvDelay

I have a doubt about the coefficients lowpassexample_psvTaps. I used WinFilter to make a FIR filter and generated the C code, and these are the coefficients:

float FIRCoef[Ntap] = { 
    -0.00000058743658087605,
    -0.00000170525260960957,
    -0.00000009806565977364,
    0.00000925056898872310,
    0.00001872073503672877,
    -0.00001502619952749291,
    -0.00013573239910459012,
    -0.00018365135507150430,
    0.00042104037187822555,
    0.00193257551455663080,
    0.00137326885639539030,
    -0.00929872209942904540,
    -0.02854945035703963800,
    0.00637252183193085400,
    0.27686786791380363000,
    0.50237945474486490000,
    0.27686786791380363000,
    0.00637252183193085400,
    -0.02854945035703963800,
    -0.00929872209942904540,
    0.00137326885639539030,
    0.00193257551455663080,
    0.00042104037187822555,
    -0.00018365135507150430,
    -0.00013573239910459012,
    -0.00001502619952749291,
    0.00001872073503672877,
    0.00000925056898872310,
    -0.00000009806565977364,
    -0.00000170525260960957,
    -0.00000058743658087605
};

In the documentation of the DSP Library it says that the coefficients for the FIRStruct are fractional, and I saw that a fractional can have values between -1 and 0.99999... The doubt I have is the following: if I want to make a FIR filter, the only thing I would have to do is to convert my coefficients to fractional (with the operation defined in the macro

#define Q15(X) \
   ((X < 0.0) ? (int)(32768*(X) - 0.5) : (int)(32767*(X) + 0.5))

in the dsp.h) and replace them in the lowpassexample_psvTaps and change the value of lowpassexample_psvNumTaps?

\$\endgroup\$
  • 1
    \$\begingroup\$ Side note: your coefficients are for a FIR that looks like it tries to emulate some Butterworth filter? If so, why bother using a FIR instead of an IIR, which is much cheaper in terms of computation? If IIR is not the target, but FIR, then why not use the FIR to its full potential (because the frequency response is awful)? Before going further, maybe you should consider what you want your filter to do, so you'd know exactly how to build it. \$\endgroup\$ – a concerned citizen Nov 15 '18 at 21:01
  • \$\begingroup\$ You can try. The example is in assembly -- how do you propose to use a C preprocessor macro in there? \$\endgroup\$ – TimWescott Nov 15 '18 at 21:14
  • \$\begingroup\$ @TimWescott I was thinking to make that operation on each coefficient to get the fractional representation and then substitute the coefficients in the assembly code. \$\endgroup\$ – user25707 Nov 15 '18 at 22:05
  • \$\begingroup\$ @aconcernedcitizen I'm barely learning about digital filters and PIC programming, but I have to do a filter for a work for school. I found another example using IIRTransposedStruct, but I don't understand how they define the coefficients: ExampleHPFCoefs: .hword 0x2392 ; b( 1,0)/2 .hword 0xB8FF ; b( 1,1)/2 .hword 0x3D5F ; a( 1,1)/2 .hword 0x2392 ; b( 1,2)/2 .hword 0xEF39 ; a( 1,2)/2 .hword 0x2FBB ; b( 2,0)/2 .hword 0xA1F2 ; b( 2,1)/2 .hword 0x579D ; a( 2,1)/2 .hword 0x2FBB ; b( 2,2)/2 .hword 0xDA17 ; a( 2,2)/2 .hword 0x3818 ; b( 3,0)/2 ... \$\endgroup\$ – user25707 Nov 15 '18 at 22:13
  • \$\begingroup\$ @user25707 you don't just use "a" filter. You design the filter to fulfill your needs. You haven't told us about what that filter should be doing. So we can't tell you how that filter should be looking. But I agree with the first commentor – whatever the filter from the example was designed for, it's pretty certainly not what you need. \$\endgroup\$ – Marcus Müller Nov 15 '18 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.