libnxter  0.1
LowPassFilter.nxc
Go to the documentation of this file.
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  LowPassFilter.nxc
4  Copyright (C) 2008 Naba Kumar <naba@gnome.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
25 #ifndef __LOW_PASS_FILTER_H
26 #define __LOW_PASS_FILTER_H
27 
32 {
33  int RC;
34  int alpha1000;
35  int lastOutput;
36 };
37 
43 void
44 LowPassFilterInit (LowPassFilterContext &context, int RC, int samplingRate)
45 {
46  context.RC = RC;
47  /* alpha scaled by 1000 for int ops */
48  context.alpha1000 = (RC * samplingRate * 1000)/(RC * samplingRate + 1);
49  context.lastOutput = -1;
50 }
51 
56 int
57 LowPassFilter (LowPassFilterContext &context, int sampleInput)
58 {
59  if (context.lastOutput < 0)
60  context.lastOutput = sampleInput;
61  else
62  context.lastOutput = context.lastOutput + context.alpha1000 * (sampleInput - context.lastOutput);
63  context.lastOutput /= 1000; /* Scaled down from int ops */
64  return context.lastOutput;
65 }
66 
67 #endif
68 
int LowPassFilter(LowPassFilterContext &context, int sampleInput)
Steps through the filter using the current sample sampleInput. This must be called at the sampling ra...
void LowPassFilterInit(LowPassFilterContext &context, int RC, int samplingRate)
Initializes a low pass filter context with the given RC constant and sampling rate. RC is given as 2 * PI * Freq (in cycles per second. Sampling rate is in samples per second.
Low Pass filter contex.