title: FIR Low Pass Filter in C
body: <p>I'm implementing a FIR Low Pass Filter in C. Got some coefficients generated in MATLAB, which I use to calculate the output.
  The audio comes from the Zynq Zedboard and is in real time.
  If I run the application, I get lots of noise. But I don't know how the signal gets distorted..
  Do I need an audio buffer to keep the processed samples? Or do I just have crappy code? I analyzed practically all elements in my code, but can't get my head around it...</p>
  
  <p>Here is the function call</p>
  
  <pre><code>#define FILTER_LEN_LP 44
  int32_t coeffsLPF[ FILTER_LEN_LP ] =
  {
      87, 76, 106, 143, 185, 234, 289, 349,
      414, 483, 555, 628, 701, 773, 842, 907,
      966, 1017, 1060, 1093, 1115, 1126, 1126,
      1115, 1093, 1060, 1017, 966, 907, 842, 773,
      701, 628, 555, 483, 414, 349, 289, 234, 185,
      143, 106, 76, 87
  };
  
  void low_pass_filter(){
  
      int k = 0;
      int filter_length = FILTER_LEN_LP;
      int32_t *coeffp; // pointer to coefficients
      int32_t in_left;
  
      while (!XUartPs_IsReceiveData(UART_BASEADDR)) {
          int32_t out_left;
          int32_t acc; // accumulator
  
          in_left = Xil_In32(I2S_DATA_RX_L_REG);
  
          //printf("%d\r\n", in_left);
  
          coeffp = coeffsLPF;
  
          acc = 1 &lt;&lt; 14 ;
          for ( k = 0; k &lt; filter_length; k++ )
          {
               acc += (int32_t)(*coeffp++) * (int32_t)(in_left);
          }
  
          //printf("%d\r\n", acc);
  
          //printf("\r\n");
  
          // saturate the result
          // 32768^2
          if ( acc &gt; 1073741823)
          {
               acc = 1073741823;
          } else if ( acc &lt; -1073741824 ){
              acc = -1073741824;
          }
  
         out_left = (int32_t) (acc &gt;&gt; 13);
  
         //xil_printf("%d\n\r", out_left);
  
         //in_right = Xil_In32(I2S_DATA_RX_R_REG);
         Xil_Out32(I2S_DATA_TX_L_REG, out_left);
  
         //Xil_Out32(I2S_DATA_TX_R_REG, in_right);
  
      }
  
  // break
  if(XUartPs_ReadReg(UART_BASEADDR, XUARTPS_FIFO_OFFSET) == 'q') menu();
  
  else low_pass_filter();
  
  }
  </code></pre>