Fourier series expansion from data points

10 views (last 30 days)
Bartosz Kurylek
Bartosz Kurylek on 21 Oct 2021
Answered: nick on 9 May 2024
Hi all,
I was trying to find Fourer approximation for any function data set. I managed to write algorythm that gives me exact shape that i wanted, but for some reason when i exceed ~320 itarations graph starts to scale up. I spent too much time looking through code and maybe anyone else had simmilar problem.
Here are functions I used to find Foureir coefficients and final function:
Thank you for any help!
My code:
%Cleaning comand window
close all
clear
clc
%Number of iterations
it = 500;
%Data points input
%loop just to create dropdown
for data_input = 1:1
t = [0
0.017952
0.035904
0.053856
0.071808
0.08976
0.107712
0.125664
0.143616
0.161568
0.17952
0.197472
0.215423
0.233375
0.251327
0.269279
0.287231
0.305183
0.323135
0.341087
0.359039
0.376991
0.394943
0.412895
0.430847
0.448799
0.466751
0.484703
0.502655
0.520607
0.538559
0.556511
0.574463
0.592415
0.610367
0.628319
0.64627
0.664222
0.682174
0.700126
0.718078
0.73603
0.753982
0.771934
0.789886
0.807838
0.82579
0.843742
0.861694
0.879646
0.897598
0.91555
0.933502
0.951454
0.969406
0.987358
1.00531
1.023262
1.041214
1.059166
1.077117
1.095069
1.113021
1.130973
1.148925
1.166877
1.184829
1.202781
1.220733
1.238685
1.256637
1.274589
1.292541
1.310493
1.328445
1.346397
1.364349
1.382301
1.400253
1.418205
1.436157
1.454109
1.472061
1.490013
1.507964
1.525916
1.543868
1.56182
1.579772
1.597724
1.615676
1.633628
1.65158
1.669532
1.687484
1.705436
1.723388
1.74134
1.759292
1.777244
1.795196
1.813148
1.8311
1.849052
1.867004
1.884956
1.902908
1.92086
1.938811
1.956763
1.974715
1.992667
2.010619
2.028571
2.046523
2.064475
2.082427
2.100379
2.118331
2.136283
2.154235
2.172187
2.190139
2.208091
2.226043
2.243995
2.261947
2.279899
2.297851
2.315803
2.333755
2.351706
2.369658
2.38761
2.405562
2.423514
2.441466
2.459418
2.47737
2.495322
2.513274
2.531226
2.549178
2.56713
2.585082
2.603034
2.620986
2.638938
2.65689
2.674842
2.692794
2.710746
2.728698
2.74665
2.764602
2.782553
2.800505
2.818457
2.836409
2.854361
2.872313
2.890265
2.908217
2.926169
2.944121
2.962073
2.980025
2.997977
3.015929
3.033881
3.051833
3.069785
3.087737
3.105689
3.123641
3.141593
3.159545
3.177497
3.195449
3.2134
3.231352
3.249304
3.267256
3.285208
3.30316
3.321112
3.339064
3.357016
3.374968
3.39292
3.410872
3.428824
3.446776
3.464728
3.48268
3.500632
3.518584
3.536536
3.554488
3.57244
3.590392
3.608344
3.626296
3.644247
3.662199
3.680151
3.698103
3.716055
3.734007
3.751959
3.769911
3.787863
3.805815
3.823767
3.841719
3.859671
3.877623
3.895575
3.913527
3.931479
3.949431
3.967383
3.985335
4.003287
4.021239
4.039191
4.057143
4.075094
4.093046
4.110998
4.12895
4.146902
4.164854
4.182806
4.200758
4.21871
4.236662
4.254614
4.272566
4.290518
4.30847
4.326422
4.344374
4.362326
4.380278
4.39823
4.416182
4.434134
4.452086
4.470038
4.48799
4.505941
4.523893
4.541845
4.559797
4.577749
4.595701
4.613653
4.631605
4.649557
4.667509
4.685461
4.703413
4.721365
4.739317
4.757269
4.775221
4.793173
4.811125
4.829077
4.847029
4.864981
4.882933
4.900885
4.918836
4.936788
4.95474
4.972692
4.990644
5.008596
5.026548
5.0445
5.062452
5.080404
5.098356
5.116308
5.13426
5.152212
5.170164
5.188116
5.206068
5.22402
5.241972
5.259924
5.277876
5.295828
5.31378
5.331732
5.349683
5.367635
5.385587
5.403539
5.421491
5.439443
5.457395
5.475347
5.493299
5.511251
5.529203
5.547155
5.565107
5.583059
5.601011
5.618963
5.636915
5.654867
5.672819
5.690771
5.708723
5.726675
5.744627
5.762579
5.78053
5.798482
5.816434
5.834386
5.852338
5.87029
5.888242
5.906194
5.924146
5.942098
5.96005
5.978002
5.995954
6.013906
6.031858
6.04981
6.067762
6.085714
6.103666
6.121618
6.13957
6.157522
6.175474
6.193426
6.211377
6.229329
6.247281
6.265233
6.283185];
f = [0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1.345656
1.345656
1.46359
1.46359
1.46359
1.46359
1.46359
1.46359
1.46359
1.46359
1.46359
1.46359
1.46359
1.853533
1.853533
1.853533
1.908695
1.908695
1.908695
1.908695
1.908695
1.859239
1.859239
1.731795
1.731795
1.731795
1.731795
1.611959
1.611959
1.611959
1.611959
1.611959
0.938595
0.938595
0.234796
0.234796
0.234796
0.234796
0.234796
0.234796
0.234796
0.234796
0.234796
0.234796
0.234796
0.234796
0.234796
0.234796
0.234796
0.234796
0.234796
0.234796
0.234796
1.739403
1.739403
1.739403
1.739403
1.739403
1.739403
1.904891
1.904891
1.904891
1.904891
1.904891
1.904891
1.904891
1.904891
1.904891
1.904891
1.904891
1.904891
1.904891
1.904891
1.904891
1.904891
1.904891
1.581524
1.581524
1.581524
1.581524
1.581524
1.581524
1.581524
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.969029
0.390773
0.390773
0.390773
0.858758
1.022642
1.117587
1.19447
1.259128
1.315828
1.36746
1.414924
1.45791
1.496073
1.530751
1.565343
1.594598
1.608704
1.614128
1.613152
1.606161
1.585375
1.55787
1.52766
1.493909
1.452975
1.401511
1.344671
1.285349
1.220261
1.146139
1.059762
1.059762
1.059762
1.059762
1.059762
1.970569
1.970569
1.970569
1.970569
1.970569
1.970569
1.970569
2.228344
2.228344
2.228344
2.228344
2.228344
2.195105
2.098704
2.002302
1.905901
1.809499
1.713098
1.616696
1.520295
1.423893
1.327491
1.23109
1.134688
1.038287
0.941885
0.845484
0.749082
0.652681
0.556279
0.459878
0.363476
0.267075
0.203542
0.203542
0.203542
0.203542
0.203542
0.203542
0.203542
0.203542
0.203542
0.203542
0.203542
0.203542
0.203542
0.737289
0.737289
0.737289
0.737289
0.737289
0.737289
0.737289
0.737289
0.737289
0.737289
0.737289
0.737289
0.737289
0.737289
0.737289
0.737289
0.737289
0.737289
0.737289
0.648332
0.648332
0.648332
0.860617
0.860617
0.860617
0.860617
0.860617
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
1.126093
0.604427
0.604427
0.604427
0.604427
0.604427
0.604427
0.604427
0.604427
0.604427
0.10148
0.10148
0.10148
0.10148
0.10148
0.10148
0.116437
0.138592
0.154547
0.167611
0.174579
0.178716
0.18133
0.182603
0.183212
0.183552
0.183756
0.183878
0.183948
0.183981
0.263889
0.327583
0.354472
0.625727
0.674006
0.700521
0.719297
0.731877
0.740955
0.748438
0.760127
0.782385
0.799697
0.780789
0.76188
0.750682
0.745784
0.739718
0.730804
0.715644
0.689119
0.639306
0.237436
0.132665
0.132665
0.132665
0
0
0
0
0
0
0
0
0
0
0
0
0
0];
end
%Ploting graphs
f1 = figure;
title('Skyline graph from datapoints')
hold on
plot(t,f,"Color",[0,0,0])
scatter(t,f,'red')
hold off
%Period and array size
T = 2*pi;
m = size(t);
m = m(1);
m1 = m;
%Fourier 0 coefficient
a0 = (2/m)*sum(f,"all");
%Finding array for sin and cosine Fourier coefficiants for all iterations
for i = 1:it
san = 0;
sbn = 0;
j=0;
for j = 1:m
san = san + (f(j)*cos((2*pi/T)*i*t(j)));
sbn = sbn + (f(j)*sin((2*pi/T)*i*t(j)));
an(i) = (2/m)*san;
bn(i) = (2/m)*sbn;
end
end
%Final an and bn arrays
fan = an;
fbn = bn;
%Finding function array values
for ifn = 1:m
sfn = 0;
for jfn = 1:it
sfn = sfn + (fan(jfn)*cos(jfn*t(ifn))+fbn(jfn)*sin(jfn*t(ifn)));
fn(ifn) = (a0/2) + sfn;
end
end
fnn = fn;
%Data for plotting graphs
t = t(1:m1);
f = f(1:m1);
fnn = fnn(1:m1);
fn = (1:m1);
f2 = figure;
title('Best fourier approximation over graph from data points')
hold on
plot(t,fnn)
plot(t,f)
hold off
%Finidning quality of function
vi = fnn - f;
vi2 = vi.^2;
vi = sum(vi2,'all');
RMS = sqrt((vi)/m1)
RMS = 51.0386

Answers (1)

nick
nick on 9 May 2024
Hi Bartosz,
The functions you used for computation of the fourier coefficient is erroneous. The formula for discrete time fourier coefficients is as follows :
Furthermore, the functions defined in the code for the coefficients of the fourier series are erroneous. f(1) corresponds to the coefficient a0, not coefficient a(1) and therefore, the index of either the function or the sinusoidals needs to be changed appropriately as shown in the code snippet below:
m = size(t);
m = m(1);
m1 = m;
T = m;
%Finding array for sin and cosine Fourier coefficiants for all iterations
for i = 1:m
san = 0;
sbn = 0;
j=0;
for j = 1:m
san = san + (f(j)*cos((2*pi/T)*(i-1)*(j-1)));
sbn = sbn - (f(j)*sin((2*pi/T)*(i-1)*(j-1)));
an(i) = (1/m)*san;
bn(i) = (1/m)*sbn;
end
end
%Final an and bn arrays
fan = an;
fbn = bn;
%Finding function array values
for ifn = 1:m
sfn = 0;
for jfn = 1:m
sfn = sfn + (fan(jfn)*cos((jfn-1)*(2*pi/T)*(ifn-1))-fbn(jfn)*sin((jfn-1)*(2*pi/T)*(ifn-1)));
fn(ifn) = sfn;
end
end
fnn = fn;
%Code for RMSE
vi = fnn - f';
vi2 = vi.^2;
vi = sum(vi2,'all');
RMS = sqrt((vi)/m1)
Presented below is the output that was obtained following the aforementioned changes:
Hope this helps

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!