IT++ Logo

source.cpp

Go to the documentation of this file.
00001 
00030 #include <itpp/signal/source.h>
00031 
00032 
00033 namespace itpp
00034 {
00035 
00037 // Sine_Source
00039 
00040 Sine_Source::Sine_Source(double freq, double mean, double ampl, double inphase)
00041 {
00042   A = ampl;
00043   m = mean;
00044   theta = inphase;
00045   dtheta = 2.0 * pi * freq;
00046 }
00047 
00048 double Sine_Source::sample()
00049 {
00050   double samp = m + A * sin(theta);
00051 
00052   theta += dtheta;
00053   if (theta >= 2.0 * pi)
00054     theta -= 2.0 * pi;
00055 
00056   return samp;
00057 }
00058 
00059 vec Sine_Source::operator()(int n)
00060 {
00061   vec v(n);
00062 
00063   for (int i=0; i < n; i++)
00064     v(i) = sample();
00065 
00066   return v;
00067 }
00068 
00069 mat Sine_Source::operator()(int h, int w)
00070 {
00071   mat mm(h, w);
00072   int i, j;
00073 
00074   for (i = 0; i < h; i++)
00075     for (j = 0; j < w; j++)
00076       mm(i, j) = sample();
00077 
00078   return mm;
00079 }
00080 
00082 // Square_Source
00084 
00085 Square_Source::Square_Source(double freq, double mean, double ampl, double inphase)
00086 {
00087   A = ampl;
00088   m = mean;
00089   theta = inphase / (2.0 * pi);
00090   dtheta = freq;
00091 }
00092 
00093 double Square_Source::sample()
00094 {
00095   double samp = theta < 0.5 ? 1.0 : -1.0;
00096 
00097   theta += dtheta;
00098   if (theta >= 1.0)
00099     theta -= 1.0;
00100 
00101   return samp;
00102 }
00103 
00104 vec Square_Source::operator()(int n)
00105 {
00106   vec v(n);
00107 
00108   for (int i=0; i < n; i++)
00109     v(i) = sample();
00110 
00111   return v;
00112 }
00113 
00114 mat Square_Source::operator()(int h, int w)
00115 {
00116   mat mm(h, w);
00117   int i, j;
00118 
00119   for (i = 0; i < h; i++)
00120     for (j = 0; j < w; j++)
00121       mm(i, j) = sample();
00122 
00123   return mm;
00124 }
00125 
00127 // Triangle_Source
00129 
00130 Triangle_Source::Triangle_Source(double freq, double mean, double ampl, double inphase)
00131 {
00132   A = ampl;
00133   m = mean;
00134   theta = inphase / (2.0 * pi);
00135   dtheta = freq;
00136 }
00137 
00138 double Triangle_Source::sample()
00139 {
00140   double samp = m + 4.0 * A * (theta < 0.25 ? theta : 0.5 - theta);
00141 
00142   theta += dtheta;
00143   if (theta >= 0.75)
00144     theta -= 1.0;
00145 
00146   return samp;
00147 }
00148 
00149 vec Triangle_Source::operator()(int n)
00150 {
00151   vec v(n);
00152 
00153   for (int i=0; i < n; i++)
00154     v(i) = sample();
00155 
00156   return v;
00157 }
00158 
00159 mat Triangle_Source::operator()(int h, int w)
00160 {
00161   mat mm(h, w);
00162   int i, j;
00163 
00164   for (i = 0; i < h; i++)
00165     for (j = 0; j < w; j++)
00166       mm(i, j) = sample();
00167 
00168   return mm;
00169 }
00170 
00172 // Sawtooth_Source
00174 
00175 Sawtooth_Source::Sawtooth_Source(double freq, double mean, double ampl, double inphase)
00176 {
00177   A = ampl;
00178   m = mean;
00179   theta = inphase / (2.0 * pi);
00180   dtheta = freq;
00181 }
00182 
00183 double Sawtooth_Source::sample()
00184 {
00185   double samp = 2.0 * A * theta;
00186 
00187   theta += dtheta;
00188   if (theta >= 0.5)
00189     theta -= 1.0;
00190 
00191   return samp;
00192 }
00193 
00194 vec Sawtooth_Source::operator()(int n)
00195 {
00196   vec v(n);
00197 
00198   for (int i=0; i < n; i++)
00199     v(i) = sample();
00200 
00201   return v;
00202 }
00203 
00204 mat Sawtooth_Source::operator()(int h, int w)
00205 {
00206   mat mm(h, w);
00207   int i, j;
00208 
00209   for (i = 0; i < h; i++)
00210     for (j = 0; j < w; j++)
00211       mm(i, j) = sample();
00212 
00213   return mm;
00214 }
00215 
00217 // Impulse_Source
00219 
00220 Impulse_Source::Impulse_Source(double freq, double ampl, double inphase)
00221 {
00222   A = ampl;
00223   pos = inphase / (2.0 * pi);
00224   dtheta = freq;
00225 }
00226 
00227 double Impulse_Source::sample()
00228 {
00229   double samp;
00230 
00231   if (pos >= 1.0) {
00232     samp = A;
00233     pos -= 1.0;
00234   }
00235   else {
00236     samp = 0.0;
00237     pos += dtheta;
00238   }
00239 
00240   return samp;
00241 }
00242 
00243 vec Impulse_Source::operator()(int n)
00244 {
00245   vec v(n);
00246 
00247   for (int i=0; i < n; i++)
00248     v(i) = sample();
00249 
00250   return v;
00251 }
00252 
00253 mat Impulse_Source::operator()(int h, int w)
00254 {
00255   mat m(h, w);
00256   int i, j;
00257 
00258   for (i = 0; i < h; i++)
00259     for (j = 0; j < w; j++)
00260       m(i, j) = sample();
00261 
00262   return m;
00263 }
00264 
00266 // Pattern_Source
00268 
00269 Pattern_Source::Pattern_Source(const vec &pattern, int start_pos)
00270 {
00271   pat = pattern;
00272   pos = start_pos;
00273 
00274   // Calculate the mean and variance.  Note that the variance shall
00275   // be normalied by N and not N-1 in this case
00276   mean = var = 0.0;
00277   for (int i = pat.size() - 1; i >= 0; i--) {
00278     mean += pat(i);
00279     var += pat(i) * pat(i);
00280   }
00281   mean /= pat.size();
00282   var /= pat.size();
00283   var -= mean * mean;
00284 }
00285 
00286 double Pattern_Source::sample()
00287 {
00288   double samp = pat(pos);
00289 
00290   if (pos >= pat.size() - 1)
00291     pos = 0;
00292   else
00293     pos++;
00294 
00295   return samp;
00296 }
00297 
00298 vec Pattern_Source::operator()(int n)
00299 {
00300   vec v(n);
00301 
00302   for (int i=0; i < n; i++)
00303     v(i) = sample();
00304 
00305   return v;
00306 }
00307 
00308 mat Pattern_Source::operator()(int h, int w)
00309 {
00310   mat m(h, w);
00311   int i, j;
00312 
00313   for (i = 0; i < h; i++)
00314     for (j = 0; j < w; j++)
00315       m(i, j) = sample();
00316 
00317   return m;
00318 }
00319 
00320 } // namespace itpp
SourceForge Logo

Generated on Thu Apr 23 20:06:43 2009 for IT++ by Doxygen 1.5.8