Nsound  0.9.4
Kernel.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: Kernel.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2009 to Present Nick Hilton
6 //
7 // weegreenblobbie_yahoo_com (replace '_' with '@' and '.')
8 //
9 //-----------------------------------------------------------------------------
10 
11 //-----------------------------------------------------------------------------
12 //
13 // This program is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation; either version 2 of the License, or
16 // (at your option) any later version.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Library General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 //
27 //-----------------------------------------------------------------------------
28 
29 #include <Nsound/Buffer.h>
30 #include <Nsound/Kernel.h>
31 #include <Nsound/RngTausworthe.h>
32 
33 #include <string.h>
34 #include <cmath>
35 #include <iomanip>
36 #include <iostream>
37 
38 using namespace Nsound;
39 
40 using std::cerr;
41 using std::endl;
42 using std::setw;
43 using std::setprecision;
44 using std::showpoint;
45 
46 //-----------------------------------------------------------------------------
48 Kernel(uint32 b_length, uint32 a_length,
49  int32 f1,
50  int32 f2,
51  int32 f3,
52  int32 f4)
53  :
54  b_length_(b_length),
55  a_length_(a_length),
56  f1_(f1),
57  f2_(f2),
58  f3_(f3),
59  f4_(f4),
60  b_(NULL),
61  a_(NULL)
62 {
63  b_ = new float64 [b_length];
64  a_ = new float64 [a_length];
65 }
66 
67 //-----------------------------------------------------------------------------
69 Kernel(const Kernel & copy)
70  :
71  b_length_(copy.b_length_),
72  a_length_(copy.a_length_),
73  f1_(copy.f1_),
74  f2_(copy.f2_),
75  f3_(copy.f3_),
76  f4_(copy.f4_),
77  b_(NULL),
78  a_(NULL)
79 {
80  b_ = new float64 [b_length_];
81  a_ = new float64 [a_length_];
82 
83  *this = copy;
84 }
85 
86 //-----------------------------------------------------------------------------
89 {
90  delete [] b_;
91  delete [] a_;
92 }
93 
94 //-----------------------------------------------------------------------------
95 Kernel &
97 operator=(const Kernel & rhs)
98 {
99  if(this == & rhs)
100  {
101  return *this;
102  }
103 
104  setLength(rhs.b_length_, rhs.a_length_);
105 
106  memcpy(b_, rhs.b_, sizeof(float64) * b_length_);
107  memcpy(a_, rhs.a_, sizeof(float64) * a_length_);
108 
109  f1_ = rhs.f1_;
110  f2_ = rhs.f2_;
111  f3_ = rhs.f3_;
112  f4_ = rhs.f4_;
113 
114  return *this;
115 }
116 
117 boolean
118 Kernel::
119 operator<(const Kernel & rhs) const
120 {
121  return (b_length_ < rhs.b_length_) ||
122 
123  (b_length_ == rhs.b_length_ &&
124  a_length_ < rhs.a_length_) ||
125 
126  (b_length_ == rhs.b_length_ &&
127  a_length_ == rhs.a_length_ &&
128  f1_ < rhs.f1_) ||
129 
130  (b_length_ == rhs.b_length_ &&
131  a_length_ == rhs.a_length_ &&
132  f1_ == rhs.f1_ &&
133  f2_ < rhs.f2_) ||
134 
135  (b_length_ == rhs.b_length_ &&
136  a_length_ == rhs.a_length_ &&
137  f1_ == rhs.f1_ &&
138  f2_ == rhs.f2_ &&
139  f3_ < rhs.f3_) ||
140 
141  (b_length_ == rhs.b_length_ &&
142  a_length_ == rhs.a_length_ &&
143  f1_ == rhs.f1_ &&
144  f2_ == rhs.f2_ &&
145  f3_ == rhs.f3_ &&
146  f4_ < rhs.f4_) ||
147 
148  false;
149 
150 }
151 
152 float64
153 Kernel::
154 getSum() const
155 {
156  float64 sum = 0.0;
157 
158  for(uint32 i = 0; i < b_length_; ++i)
159  {
160  sum += std::fabs(b_[i]);
161  }
162 
163  for(uint32 i = 0; i < a_length_; ++i)
164  {
165  sum += std::fabs(a_[i]);
166  }
167 
168  return sum;
169 }
170 
171 //-----------------------------------------------------------------------------
172 std::ostream &
173 Nsound::
174 operator<<(std::ostream & out, const Kernel & rhs)
175 {
176  #define FORMAT setw(12) << setprecision(9) << std::setiosflags(std::ios::fixed)
177 
178  for(uint32 i = 0; i < rhs.b_length_; ++i)
179  {
180  out << "b[" << i << "] = " << FORMAT << rhs.b_[i];
181 
182  if(i < rhs.a_length_)
183  {
184  out << " a[" << i << "] = " << FORMAT << rhs.a_[i];
185  }
186  out << std::endl;
187  }
188 
189  return out;
190 }
191 
192 void
193 Kernel::
194 randomize(const float64 & min, const float64 & max)
195 {
196  RngTausworthe rng;
197 
198  for(uint32 i = 0; i < b_length_; ++i)
199  {
200  b_[i] = rng.get(min, max);
201  }
202 
203  for(uint32 i = 0; i < a_length_; ++i)
204  {
205  a_[i] = rng.get(min, max);
206  }
207 }
208 
209 void
210 Kernel::
211 setA(const float64 * a)
212 {
213  memcpy(a_, a, sizeof(float64) * a_length_);
214 }
215 
216 void
217 Kernel::
218 setA(const Buffer & a)
219 {
220  uint32 a_len = a.getLength();
221 
222  for(uint32 i = 0; i < a_length_; ++i)
223  {
224  if(i < a_len)
225  {
226  a_[i] = a[i];
227  }
228  }
229 }
230 
231 void
232 Kernel::
233 setB(const float64 * b)
234 {
235  memcpy(b_, b, sizeof(float64) * b_length_);
236 }
237 
238 void
239 Kernel::
240 setB(const Buffer & b)
241 {
242  uint32 b_len = b.getLength();
243 
244  for(uint32 i = 0; i < b_length_; ++i)
245  {
246  if(i < b_len)
247  {
248  b_[i] = b[i];
249  }
250  }
251 }
252 
253 void
254 Kernel::
255 setLength(uint32 b_length, uint32 a_length)
256 {
257  if(b_length_ != b_length)
258  {
259  b_length_ = b_length;
260  delete [] b_;
261  b_ = new float64 [b_length_];
262  }
263 
264  if(a_length_ != a_length)
265  {
266  a_length_ = a_length;
267  delete [] a_;
268  a_ = new float64 [a_length_];
269  }
270 
271 }
272 
273 void
274 Kernel::
276 {
277  Kernel temp(rhs);
278 
279  rhs.setA(getB());
280  rhs.setB(getA());
281 
282  setA(temp.getB());
283  setB(temp.getA());
284 }
285 
286 void
288 {
289  float64 temp = b;
290  b = a;
291  a = temp;
292 }
293 
294 void
295 Kernel::
297 {
298  Kernel temp(rhs);
299 
300  for(uint32 i = 0; i < b_length_; ++i)
301  {
302  if(i % 2)
303  {
304  swap_(b_[i], rhs.b_[i]);
305  }
306  }
307 
308  for(uint32 i = 0; i < a_length_; ++i)
309  {
310  if(i % 2)
311  {
312  swap_(a_[i], rhs.a_[i]);
313  }
314  }
315 
316 }
317 
318 void
319 Kernel::
321 {
322  for(uint32 i = 0; i < b_length_; ++i)
323  {
324  if(i % 2 && i < a_length_)
325  {
326  swap_(b_[i], a_[i]);
327  }
328  }
329 
330 }
331 
332 
333 
uint32 b_length_
Definition: Kernel.h:164
unsigned int uint32
Definition: Nsound.h:153
std::ostream & operator<<(std::ostream &out, const Buffer &rhs_buffer)
Definition: Buffer.cc:1338
void ga_interleave()
Genitic Algorithm Helper Function: interleave B coefs with A.
Definition: Kernel.cc:320
Kernel & operator=(const Kernel &rhs)
Definition: Kernel.cc:97
int32 f1_
Definition: Kernel.h:167
float64 getB(uint32 i)
Returns kernel 'b[i]' coef.
Definition: Kernel.h:86
float64 getSum() const
Definition: Kernel.cc:154
void setB(const float64 *b)
Copy getBLength() values from the array a into the Kernel. Use setLength() to set the number of value...
Definition: Kernel.cc:233
#define FORMAT
float64 * getB()
Returns kernel 'b' array, the length is returned by getBLegnth()
Definition: Kernel.h:91
boolean operator<(const Kernel &rhs) const
Definition: Kernel.cc:119
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
void ga_swap_ab(Kernel &rhs)
Genitic Algorithm Helper Function: swaps the A & B coefs.
Definition: Kernel.cc:275
void setLength(uint32 b_length, uint32 a_length)
Definition: Kernel.cc:255
void randomize(const float64 &min=-1.0, const float64 &max=1.0)
Sets all coefs to random float64 values between min & max.
Definition: Kernel.cc:194
virtual ~Kernel()
Definition: Kernel.cc:88
Kernel(uint32 b_length, uint32 a_length, int32 f1=0, int32 f2=0, int32 f3=0, int32 f4=0)
Definition: Kernel.cc:48
int32 f2_
Definition: Kernel.h:168
A Buffer for storing audio samples.
Definition: Buffer.h:60
signed int int32
Definition: Nsound.h:142
int32 f3_
Definition: Kernel.h:169
void swap_(float64 &a, float64 &b)
Definition: Kernel.cc:287
float64 * a_
Definition: Kernel.h:173
uint32 get()
Get a random number.
float64 * getA()
Returns Kernel 'a' array, the length is returned by getALegnth()
Definition: Kernel.h:75
uint32 a_length_
Definition: Kernel.h:165
float64 getA(uint32 i)
Returns kernel 'a[i]' coef.
Definition: Kernel.h:70
int32 f4_
Definition: Kernel.h:170
float64 * b_
Definition: Kernel.h:172
void setA(const float64 *a)
Copy getALength() values from the array a into the Kernel. Use setLength() to set the number of value...
Definition: Kernel.cc:211