OpenWareLaboratory
InterpolatingSignalGenerator.h
Go to the documentation of this file.
1 #ifndef __InterpolatingSignalGenerator_h__
2 #define __InterpolatingSignalGenerator_h__
3 
4 #include "FloatArray.h"
5 #include "CircularBuffer.h"
6 #include "Interpolator.h"
7 
14 template<InterpolationMethod im = LINEAR_INTERPOLATION>
16 protected:
18  float mu = 0;
19  float inc = 0;
23  }
24 public:
27  void setPeriod(float period){
28  inc = 1/period;
29  }
31  float getPeriod(){
32  return 1/inc;
33  }
38  void update(float x){
39  points->write(x);
40  mu = 0;
41  }
46  void set(float x){
47  points->setAll(x);
48  mu = 0;
49  }
50  float generate();
51  void generate(FloatArray output);
53  static InterpolatingSignalGenerator<im>* create(size_t period){
55  obj->setPeriod(period);
56  return obj;
57  }
60  delete obj;
61  }
62 };
63 
64 template<>
66  output.setAll(points->read());
67 }
68 
69 template<>
71  return points->read();
72 }
73 
74 template<>
77  return allocate(1);
78 }
79 
80 template<>
82  float pts[2];
83  points->setDelay(2);
84  points->read(pts, 2);
85  float y = Interpolator::linear(pts[0], pts[1], mu);
86  mu += inc;
87  return y;
88 }
89 
90 template<>
92  float pts[2];
93  points->setDelay(2);
94  points->read(pts, 2);
95  size_t len = output.getSize();
96  float* out = output.getData();
97  while(len--){
98  *out++ = Interpolator::linear(pts[0], pts[1], mu);
99  mu += inc;
100  }
101 }
102 
103 template<>
106  return allocate(2);
107 }
108 
109 template<>
111  float pts[2];
112  points->setDelay(2);
113  points->read(pts, 2);
114  float y = Interpolator::cosine(pts[0], pts[1], mu);
115  mu += inc;
116  return y;
117 }
118 
119 template<>
121  float pts[2];
122  points->setDelay(2);
123  points->read(pts, 2);
124  size_t len = output.getSize();
125  float* out = output.getData();
126  while(len--){
127  *out++ = Interpolator::cosine(pts[0], pts[1], mu);
128  mu += inc;
129  }
130 }
131 
132 template<>
135  return allocate(2);
136 }
137 
138 template<>
140  float pts[3];
141  points->setDelay(3);
142  points->read(pts, 3);
143  float y = Interpolator::cubic(pts[0], pts[1], pts[2], mu);
144  mu += inc;
145  return y;
146 }
147 
148 template<>
150  float pts[3];
151  points->setDelay(3);
152  points->read(pts, 3);
153  size_t len = output.getSize();
154  float* out = output.getData();
155  while(len--){
156  *out++ = Interpolator::cubic(pts[0], pts[1], pts[2], mu);
157  mu += inc;
158  }
159 }
160 
161 template<>
164  return allocate(3);
165 }
166 
167 template<>
169  float pts[4];
170  points->setDelay(4);
171  points->read(pts, 4);
172  float y = Interpolator::cubic(pts[0], pts[1], pts[2], pts[3], mu);
173  mu += inc;
174  return y;
175 }
176 
177 template<>
179  float pts[4];
180  points->setDelay(4);
181  points->read(pts, 4);
182  size_t len = output.getSize();
183  float* out = output.getData();
184  while(len--){
185  *out++ = Interpolator::cubic(pts[0], pts[1], pts[2], pts[3], mu);
186  mu += inc;
187  }
188 }
189 
190 template<>
193  return allocate(4);
194 }
195 
196 template<>
198  float pts[4];
199  points->setDelay(4);
200  points->read(pts, 4);
201  float y = Interpolator::cubicSmooth(pts[0], pts[1], pts[2], pts[3], mu);
202  mu += inc;
203  return y;
204 }
205 
206 template<>
208  float pts[4];
209  points->setDelay(4);
210  points->read(pts, 4);
211  size_t len = output.getSize();
212  float* out = output.getData();
213  while(len--){
214  *out++ = Interpolator::cubicSmooth(pts[0], pts[1], pts[2], pts[3], mu);
215  mu += inc;
216  }
217 }
218 
219 template<>
222  return allocate(4);
223 }
224 
225 template<>
227  float pts[4];
228  points->setDelay(4);
229  points->read(pts, 4);
230  float y = Interpolator::hermite(pts[0], pts[1], pts[2], pts[3], mu);
231  mu += inc;
232  return y;
233 }
234 
235 template<>
237  float pts[4];
238  points->setDelay(4);
239  points->read(pts, 4);
240  size_t len = output.getSize();
241  float* out = output.getData();
242  while(len--){
243  *out++ = Interpolator::hermite(pts[0], pts[1], pts[2], pts[3], mu);
244  mu += inc;
245  }
246 }
247 
248 template<>
251  return allocate(4);
252 }
253 
254 #endif // __InterpolatingSignalGenerator_h__
void setAll(const DataType value)
static void destroy(CircularBuffer< float > *obj)
static CircularBuffer< float > * create(size_t len)
void write(DataType c)
This class contains useful methods for manipulating arrays of floats.
Definition: FloatArray.h:12
void setAll(float value)
Set all the values in the array.
Definition: FloatArray.cpp:220
Signal Generator that generates an arbitrary number of in-between points, interpolated between two (l...
static void destroy(InterpolatingSignalGenerator< im > *obj)
float getPeriod()
Get period in samples.
void generate(FloatArray output)
void setPeriod(float period)
Set period in samples.
InterpolatingSignalGenerator(CircularFloatBuffer *points)
void set(float x)
Reset interpolator to a fixed value.
static InterpolatingSignalGenerator< im > * allocate(size_t len)
void update(float x)
Update the interpolator with a new reference point.
static InterpolatingSignalGenerator< im > * create()
static InterpolatingSignalGenerator< im > * create(size_t period)
static float cubic(float y0, float y1, float y2, float mu)
Three-point cubic interpolation of point between y1 and y2 ref: http://www.ebyte.it/library/codesnipp...
Definition: Interpolator.h:19
static float hermite(float y0, float y1, float y2, float y3, float mu, float tension=0, float bias=0)
Definition: Interpolator.h:54
static float linear(float y1, float y2, float mu)
Definition: Interpolator.h:8
static float cosine(float y1, float y2, float mu)
Definition: Interpolator.h:11
static float cubicSmooth(float y0, float y1, float y2, float y3, float mu)
Definition: Interpolator.h:38
size_t getSize() const
Definition: SimpleArray.h:31
T * getData()
Get the data stored in the Array.
Definition: SimpleArray.h:27