IT++ Logo

source.cpp

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

Generated on Sun Sep 14 18:57:06 2008 for IT++ by Doxygen 1.5.6