libnxter  0.1
FiltersTest.nxc
Go to the documentation of this file.
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  FiltersTest.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 */
24 #include "Sampler.nxc"
25 #include "LowPassFilter.nxc"
26 #include "HighPassFilter.nxc"
27 
28 #define LIGHT_SENSOR IN_3
29 #define LIGHT_SENSOR_SAMPLING_PERIOD 36 /* degrees */
30 #define LIGHT_SENSOR_SAMPLING_RATE 30 /* samples per cycle (1080 degrees) */
31 
32 task
33 main ()
34 {
35  int t = 0;
36  int x1 = 0, x2 = 0;
37  int max = 0;
38  SamplerContext sampler;
39  HighPassFilterContext highPass;
40  LowPassFilterContext lowPass;
41 
42  SetSensorLight(LIGHT_SENSOR);
43  ClearScreen();
44  ResetAllTachoCounts(OUT_B);
45  SamplerInit(sampler, 0, LIGHT_SENSOR_SAMPLING_PERIOD);
46  HighPassFilterInit(highPass, 3 /* 0.5Hz * 2 * PI */, LIGHT_SENSOR_SAMPLING_RATE);
47  LowPassFilterInit(lowPass, 24 /* 4Hz * 2 * PI */, LIGHT_SENSOR_SAMPLING_RATE);
48 
49  OnFwd(OUT_B, 100);
50  while (1)
51  {
52  int tachoCount = GetOutput(OUT_B, TachoCount);
53  int lightSense = Sensor(LIGHT_SENSOR);
54 
55  do {
56  int lightSenseSample = SamplerGet(sampler, tachoCount, lightSense);
57  if (lightSenseSample < 0)
58  break;
59  int highPassOut = HighPassFilter(highPass, lightSenseSample);
60  int lowPassOut = LowPassFilter(lowPass, highPassOut);
61 
62  /* Plot both intput and output samplings */
63  //LineOut(t, 48, t, 48 + lightSense - 50, false);
64  LineOut(t, 16, t, 16 + lowPassOut, false);
65  t++;
66  if (t >= 60)
67  {
68  ClearScreen();
69  t = 0;
70  }
71  } while (sampler.hasMore);
72  }
73 }
74 
75 
int LowPassFilter(LowPassFilterContext &context, int sampleInput)
Steps through the filter using the current sample sampleInput. This must be called at the sampling ra...
High Pass filter implementation.
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.
A sampler to sample a stream of inputs at periodic interval.
void SamplerInit(SamplerContext &context, int currentPosition, int samplingPeriod)
Initializes the sampler with given sampling period and initial position.
Definition: Sampler.nxc:44
Low Pass filter contex.
void HighPassFilterInit(HighPassFilterContext &context, int RC, int samplingRate)
Initializes the given high pass filter context with the given RC constant and sampling rate...
High Pass filter contex.
Low Pass filter implementation.
int HighPassFilter(HighPassFilterContext &context, int sampleInput)
Steps through the filter using the current sample sampleInput. This must be called at the sampling ra...
int SamplerGet(SamplerContext &context, int currentPosition, int sampleInput)
Returns sampleInput at the sampling periods and -1 for all others.
Definition: Sampler.nxc:56
The class to hold sampler context.
Definition: Sampler.nxc:31