[Scilab-users] fourier series and fft
Samuel Gougeon
sgougeon at free.fr
Sat Sep 24 17:09:28 CEST 2016
Le 22/09/2016 23:09, paul.carrico at free.fr a écrit :
> dear all
>
> I'm novice in Fourier series and other and my question is probably
> naive (sorry for this) => I'm wondering if scilab can directly
> calculate the Fourier coefficient a0, a_k and b_k ?
>
> I'm currently doing it "by hand" is order to familiarise myself with
> it (and I'm looking at the same time to documents on FFT use and
> rules to refind the 2 natural frequencies of the example here bellow),
> but it seems I'll need to code the coefficient calculations ... Am I
> right ?
.
Doing that is the main purpose of fft().
fft() returns the (bilateral) complex coefficients c_k of the series.
a_k and b_k are easily computed from them.
// Example #1: n = 100;
x = linspace(0, 1, n+1);
x = x(1:n);
y = sin(2*%pi*x);
ft = fft(y);
ft = clean(ft);
c = ft/n;
c0 = c(1)
// c(k) = ft(k+1)
// c(-k) = ft(n+1-k)
a0 = c0 // average
a = (c(2:(n/2)) + c(n:-1:(n/2+2))) // a_k = c_k + c_(-k)
b = %i*(c(2:n/2) - c(n:-1:n/2+2)) // b_k = %i*(c_k - c_(-k))
// Example #2:
n = 100; x = linspace(0, 2, n+1); x = x(1:n); y = 2*sin(2 * %pi * x) - 3*cos(%pi * x);
ft = fft(y);
ft = clean(ft);
c = ft/n;
c0 = c(1)
a0 = c0
a = (c(2:(n/2)) + c(n:-1:(n/2+2)))
b = %i*(c(2:n/2) - c(n:-1:n/2+2))
--> a = (c(2:(n/2)) + c(n:-1:(n/2+2)))
a =
column 1 to 22
-3. 0. 0. 0. 0. 0. 0. ...
// here is the "-3" coeff of the first even cos() harmonic
--> b = %i*(c(2:n/2) - c(n:-1:n/2+2))
b =
column 1 to 22
0. 2. 0. 0. 0. 0. 0. ...
// here is the "3" coeff of the second odd sin() harmonic
Anyway, the full x-range is considered as the period.
If it is not actually the case, some artefacts appear in the results.
To avoid them, it is mandatory to sample /x/ over a multiple of its
"natural" period.
Example #1: The period of sin(2*%pi*x) is 2*pi, this is why x must be
sampled from 0 to 1.
Example #2: The period of 2*sin(2 * %pi * x) - 3*cos(%pi * x)is also 2*pi,
this is why x must be sampled from 0 to 2 (in the
cos() argument).
HTH
Samuel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20160924/cb4ec81f/attachment.htm>
More information about the users
mailing list