IT++ Logo Newcom Logo

source.cpp

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

Generated on Sat Aug 25 23:40:03 2007 for IT++ by Doxygen 1.5.2