220 likes | 681 Views
Circular buffer. Implementing real-time convolution. Problem Statement. Implement the 4-th order FIR filter shown in Figure 3.2 on page 28. n = 4 x[] is the buffer for the input b[] is the coefficients vector
E N D
Circular buffer Implementing real-time convolution
Problem Statement • Implement the 4-th order FIR filter shown in Figure 3.2 on page 28. • n = 4 • x[] is the buffer for the input • b[] is the coefficients vector • the newest input will be multiplied by b[0] while the oldest will be multiplied by b[4] • y is the output to the DAC • sn denotes the value of the n-th input sample
Initially • The system is relaxed, that is, the input buffer is filled with 0. • x[0] = x[1] = … = x[4] = 0 • We need an index idx to denote where is the latest input. • Initially, idx = 0. that means x[0] will hold the current input at t = 0. Note that’s different from what we did in problem 2 where x[n] ALWAYS stores the latest input.
t = 0 idx = 0 x[] b[] y = x[idx] * b[0] = x[0] * b[0]
t = 1 idx= 0+1=1 x[] b[] y = x[idx] * b[0] + x[idx-1] * b[1] = x[1] * b[0] + x[0] * b[1]
t = 4 idx = 3+1 = 4 x[] b[] y = x[i] * b[0] + x[i-1] * b[1] + x[i-2] * b[2] + x[i-3] * b[3] + x[i-4] * b[4] = x[4] * b[0] + x[3] * b[1] + x[2] * b[2] + x[1] * b[3] + x[0] * b[4]
t = 5 idx = 4 +1 = 5 = 0 x[] b[] y = x[idx] * b[0] + x[idx-1] * b[1] + x[idx-2] * b[2] + x[idx-3] * b[3] + x[idx-4] * b[4] = x[0] * b[0] + x[-1] * b[1] + x[-2] * b[2] + x[-3] * b[3] + x[-4] * b[4] = x[0] * b[0] + x[4] * b[1] + x[3] * b[2] + x[2] * b[3] + x[1] * b[4]
Mod operator • b[0] should be always multiplied by the latest data while b[4] times the oldest one. • Also notice the negative indexes in the equation. Since it’s a “circular” buffer, x[-1] stands for x[4] • But how to mathematically convert -1 to 4? • Mod operation: (k + n) mod n = k mod n • (3+5) mod 5 = 3 mod 5 =3 • (-1+5) mod 5 = 4 mod 5 = 4 • In C/C++, % is the mod operator
Solution #define N 4 int idx = 0; int i; float y; Interrupt void McBSP_Rx_ISR() { … idx = (idx+1) % (N+1); y = 0.0; for (i=0; i<=N;i++) { y = y + x[(idx-i+N+1)%(N+1)] * b[i]; } }