math - Keeping exact wave form in memory -
let's have program calculates value of sine wave @ time t
. sine wave of form sin(f*t + phi)
. amplitude 1
.
if have 1 sin
term fine. can calculate value @ time t
.
but, @ runtime, wave form becomes modified when combines other waves. sin(f1 * t + phi1) + sin(f2 * t + phi2) + sin(f3 * t + phi3) + ...
the simplest solution have table columns phi
, f
, iterate on rows, , sum results. me feels once reach thousands of rows, computation become slow.
is there different way of doing this? combining sines 1 statement/formula?
if have fourier series (i.e. f_i
= i f
f) can use clenshaw recurrence relation faster computing sines (but might less accurate).
in case can consider sequence:
f_k = exp( ( k f t + phi_k) ) , imaginary unit.
notice im(f_k)
= sin( k f t + phi_k )
, sequence.
also
f_k = exp( ( k f t + phi_k) ) = exp( k f t ) exp( phi_k )
hence have a_k = exp(i phi_k)
. can precompute these values , store them in array. simplicity on assume a_0 = 0
.
now, exp( (k + 1) f t) = exp(i k f t) * exp(i f t)
, alpha_k = exp(i f t)
, beta_k = 0
.
you can apply recurrence formula, in c++ can this:
complex<double> clenshaw_fourier(double f, double t, const vector< complex<double> > & ) { const complex<double> alpha = exp(f * t * i); complex<double> b = 0; (int k = a.size() - 1; k >0; -- k ) b = a[k] + alpha * b; return a[0] + alpha * b; }
assuming a[k] == exp( phi_k )
.
the real part of answer sum of cos(k f t + phi_k)
, while imaginary part sum of sin(k f t + phi_k)
.
as can see uses addition , multiplications, except exp(f * t * i)
computed once.
Comments
Post a Comment