Fixed-point IIR Filter Code Generator for ARM
This is a fixed-point IIR filtering routine written in ARM assembly. We also provide an online tool that takes a Matlab coefficient file as input and generates a source file, header file, and example code as output. The code generator also works on FIR filters.
Detailed writeup and tutorial: http://ieee.ucsd.edu/wiki/tutorials:fixed_point_filtering_library
Online Code Generator: http://ieee.ucsd.edu/projects/qfilt/
Note that while the example below has a lowpass filter response, the code can be used with any filter response by changing the coefficients.
The detailed writeup also contains flavors written in C and C++. The C version is the most versatile, but uses more memory and instructions than the C++ or ARM version. The C++ version uses templates to eliminate a few pointers and constants by evaluating them at compile time. The hand-written ARM assembly version is the fastest and most memory efficient, and leverages the smlal 64-bit single cycle multiply accumulate instruction.
/*
* fixedp lowpass_iir(fixedp *w, fixedp x);
*
* Fixed point IIR filtering routine for ARM. Computes output y for
* input x. The output will have the same fracbits as the input.
* w: caller-allocated array for state storage. Should be length 2*L.
* x: sample to filter
*
* Required data:
* LENGTH: number of sections
* .sos: sos matrix
* SOS_FRACBITS: sos fracbits
* .gain: scale values array
* G_FRACBITS: scale values fracbits
*
* Register usage:
* r0: address of internal state array (w)
* r1: x
* r2: address of SOS array
* r3: address of gain array
* r4: w0
* r5: w1
* r6: y
* r7: long multiply lo word
* r8: long multiply hi word
* r9: B1
* r10: B2
* r11: A1
* r12: A2
* r14: loop counter
*/
.set LENGTH, 3
.set SOS_FRACBITS, 30
.set G_FRACBITS, 31
.section .rodata
.align 4
.sos:
.word 0x49be7eaf, 0x40000000, 0xc4c9d93a, 0x251f228c
.word 0x1d81c8a5, 0x40000000, 0xdaa0b600, 0x37cef3c1
.word 0x40000000, 0x00000000, 0xd87b730c, 0x00000000
.gain:
.word 0x06b1dbb5, 0x42fe27a0, 0x613d5d5a
.text
.arm
.global lowpass_iir
.func lowpass_iir
lowpass_iir:
push {r4-r11, lr}
mov r14, #0 /* i = 0 */
ldr r2, =.sos /* load address of SOS matrix */
ldr r3, =.gain /* load address of gain coefficient array */
cmp r14, #LENGTH
bge .endloop
.loop:
/* load all the SOS coefficients we need into r8-r12 and increment the SOS pointer */
ldmia r2!, {r9-r12} /* B1, B2, A1, A2 */
/* x = gain[i]*x */
ldr r6, [r3], #4 /* load current element of gain array into r6 and increment by 4 */
smull r7, r8, r1, r6 /* 64-bit multiply: r5:r4 = x*gain[i]; */
mov r1, r7, lsr#G_FRACBITS /* shift lo word to the right by G_FRACBITS */
orr r1, r1, r8, lsl#(32 - G_FRACBITS) /* shift hi word to the right by G_FRACBITS and OR with lo word*/
/* load w0 and w1 into r4, r5, but do NOT increment */
ldm r0, {r4-r5}
/* y(r6) = x(r1) + w[W0](r4)*/
add r6, r1, r4
/* w0(r4) = .sos[B1](r9)*x(r1) - .sos[A1](r11)*y(r6) + w[W1](r5); */
rsb r11, r11, #0 /* .sos[A1] = -sos[A1] */
smull r7, r8, r9, r1 /* r8:r7 = sos[B1]*x */
smlal r7, r8, r11, r6 /* r8:r7 += -sos[A1]*y */
mov r4, r7, lsr#SOS_FRACBITS /* shift lo word to the right by SOS_FRACBITS */
orr r4, r4, r8, lsl#(32 - SOS_FRACBITS) /* shift hi word to the right by SOS_FRACBITS and OR with lo word*/
add r4, r4, r5 /* add w1 */
/* w2 = sos[B2]*x(r1) - .sos[A2](r12)*y(r6); */
rsb r12, r12, #0 /* .sos[A2] = -sos[A2] */
smull r7, r8, r10, r1 /* r8:r7 = sos[B2](r10)*x(r1) */
smlal r7, r8, r12, r6 /* r8:r7 += -sos[A2](r12)*y(r6) */
mov r5, r7, lsr#SOS_FRACBITS /* shift lo word to the right by SOS_FRACBITS */
orr r5, r5, r8, lsl#(32 - SOS_FRACBITS) /* shift hi word to the right by SOS_FRACBITS and OR with lo word*/
/* need to store w0, w1 back to memory and increment */
stmia r0!, {r4-r5}
/* x = y */
mov r1, r6
/* increment pointer and branch to top of loop */
add r14, r14, #1
cmp r14, #LENGTH
blt .loop
.endloop:
/* set return val, restore stack, and return */
mov r0, r6
pop {r4-r11, lr}
bx lr
.endfunc
.end