Mozzi  version 2016-12-11-17:03
sound synthesis library for Arduino
DCfilter.h
1 /*
2  * DCfilter.h
3  *
4  * Copyright 2012 Tim Barrass.
5  *
6  * This file is part of Mozzi.
7  *
8  * Mozzi is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
9  *
10  */
11 
12 #ifndef DCFILTER_H
13 #define DCFILTER_H
14 
15 /*
16 tb2010 adapted from:
17 robert bristow-johnson, DSP Trick: Fixed-Point DC Blocking Filter with Noise-Shaping
18 http://www.dspguru.com/book/export/html/126
19 
20 y[n] = x[n] - x[n-1] + a * y[n-1]
21 
22 Where y[n] is the output at the current time n, and x[n] is the input at the current time n.
23 
24 also, see DC Blocker Algorithms, http://www.ingelec.uns.edu.ar/pds2803/materiales/articulos/04472252.pdf
25  */
26 
32 class DCfilter {
33 public:
39  DCfilter(float pole):acc(0),prev_x(0),prev_y(0)
40  {
41  A = (int)(32768.0*(1.0 - pole));
42  }
43 
44 /* almost original
45  // timing: 20us
46  int next(int x)
47  {
48  setPin13High();
49  acc -= prev_x;
50  prev_x = (long)x<<15;
51  acc += prev_x;
52  acc -= A*prev_y;
53  prev_y = acc>>15; // quantization happens here
54  int filtered = (int)prev_y;
55  // acc has y[n] in upper 17 bits and -e[n] in lower 15 bits
56  setPin13Low();
57  return filtered;
58  }
59  */
60 
66  // timing :8us
67  inline
68  int next(int x)
69  {
70  acc += ((long)(x-prev_x)<<16)>>1;
71  prev_x = x;
72  acc -= (long)A*prev_y; // acc has y[n] in upper 17 bits and -e[n] in lower 15 bits
73  prev_y = (acc>>16)<<1; // faster than >>15 but loses bit 0
74  if (acc & 32784) prev_y += 1; // adds 1 if it was in the 0 bit position lost in the shifts above
75  return prev_y;
76  }
77 
78 private:
79  long acc;
80  int prev_x, prev_y,A;
81 };
82 
88 #endif // #ifndef DCFILTER_H
89 
DCfilter(float pole)
Instantiate a DC-blocking filter.
Definition: DCfilter.h:39
int next(int x)
Filter the incoming value and return the result.
Definition: DCfilter.h:68
A DC-blocking filter useful for highlighting changes in control signals.
Definition: DCfilter.h:32