# [SciPy-user] Fourier series

Ivo Maljevic ivo.maljevic@gmail....
Wed Oct 22 12:32:32 CDT 2008

Maybe this simple matlab code can help you to develop ideas further:

clear
N = 1000;

%time covers only one period, excluding the last point, as that
% is the begining of the next period
t=(0:N-1)/N;
dt = t(2) - t(1);
% set the sampling frequency to some reasonable value
f=1/dt/5

% uncomment if you want only one cos function. This function is periodic,
% period T = 1 / f => fundamental frequency is
%x=cos(2*pi*f*t);
x=cos(2*pi*f*t)+cos(4*pi*f*t);
X = 1/length(t)*fft(x);

nonzero_index = find(abs(X) > 1e-3);
l = length(nonzero_index);
n = (nonzero_index(1:l/2)-1)/f
X(nonzero_index)

a = 2*real(X(nonzero_index))
b = -2*imag(X(nonzero_index))

As you know, cos() is a periodic function with only one non-zero coefficient
in the fourier sereis. In this trivial example,
you will get n =1 and a_n and b_n coefficients if you use the signal from
the first line, or n=1,2 for the signal from the second line.
It works even if you lower N down to 10 or some other number that is a
multiple of 5 (for this example).

Look at the comments too, to know when to end the period of the signal.

Granted, this is a simplified example, and you may want to figure out how
many sample points you need. Just to remind you,
from Parseval's theorem you know that the power of a periodic signal (which
has a finite power) is the sum of all these
coefficients squared. This means that for any signal that is F-transformable
you will find that you only need a finite number
of sample points.

This is of course far away from the real deal, I just thougt that this might
give you some ideas on which way to go. Or, maybe somebody else has already
done this, which I wouldn't be surprised, and you will get your answer
without much work.

Ivo

. In thi

2008/10/22 Nils Wagner <nwagner@iam.uni-stuttgart.de>

> Hi all,
>
> Is there a function in scipy to compute the Fourier
> coefficients
> a_0, a_1, b_1, a_2, b_2 of a periodic function f(t)=f(t+T)
>
> http://en.wikipedia.org/wiki/Fourier_series
>
> An example would be appreciated.
>
> Nils
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20081022/a256e06a/attachment.html