Mozzi  alpha 0.01.1t
sound synthesis library for Arduino
 All Classes Functions Typedefs
LowPass1stOrder.h
00001 /*
00002  * LowPass1stOrder.h
00003  *
00004  * Copyright 2012 Tim Barrass
00005  *
00006  * This file is part of Mozzi.
00007  *
00008  * Mozzi is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 3 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Mozzi is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Mozzi.  If not, see <http://www.gnu.org/licenses/>.
00020  *
00021  */
00022 
00023 /*
00024  * Adapted from a six-zero low-pass filter implementation of H(z) = ( 1 - z^-20) / (1 - z^-1)
00025  * Tompkins, W. J. (1993) Biomedical Digital Signal Processing: C Language Examples And Laboratory Experiments for the IBM PC, p.1209
00026  *
00027 */
00028 
00029 
00030 #ifndef LOWPASS1STORDER_H_
00031 #define LOWPASS1STORDER_H_
00032 
00033 
00042 template <class T, unsigned char LENGTH>
00043 class LowPass1stOrder
00044 {
00045 
00046 public:
00047 
00053                 LowPass1stOrder()
00054                 {
00055                                 x_delay = &x[0];
00056                 }
00057 
00063                 inline
00064                 T next(T x_current)
00065                 {
00066                                 y += (T)(x_current - *x_delay);                 /* y(nT)=y(nT-T)+x(nT)-x(nT-20T) */
00067                                 *x_delay = x_current;                                           /* x_current becomes x(nT-T) in FIFO*/
00068                                 x_delay = (x_delay == &x[LENGTH-1]) ? &x[0] : ++x_delay; /* increment pointer x_delay along the x array and wrap*/
00069                                 return(y);
00070                 }
00071 
00072 
00073 private:
00074                 T x[LENGTH];                    /* FIFO buffer of past samples */
00075                 T y;                                                            /* serves as both y(nT) and y(nT-T) */
00076                 T *x_delay;                     /* pointer to x(nT-20T)*/
00077 };
00078 
00079 
00080 #endif /* LOWPASS1STORDER_H_ */