Differential Equation

Stiganator

Platinum Member
Oct 14, 2001
2,492
3
81
So, I am curious how one would go about fitting dataset with FFT preferably. I have a set of data which I obtained by scanning graph and using photoshop to calculate the distances and normalize badck to the roughly original graph. Now, I don't know how to fit it. Excel certainly can't fit it properly, and it is smooth with peaks, so I was thinking, hmm periodic, I could fit it with a fast fourier. That is where I run aground. I didn't really do well with fouriers so, how do I fit this data? I have access to Matlab if that helps, I know lots of engineers use it. Actually, I wouldn't need to use fouriers, but I don't know of any other methods that will fit data like this. Additionally, after my fourier tranform is completed, I have to perform the inverse fourier to get it in a time domain again, correct? Thus the final result is a multi termed expression of sin and cos which roughly fit my data?
Also due to the method of sampling the data, I didn't use a constant interval in "x" direction from photoshop, since I more or less just clicked and measured a ton of points from the origin of the graphs. Any direction here would be greatly appreciated.
 

TuxDave

Lifer
Oct 8, 2002
10,571
3
71
Exactly what is the origin of these datasets? I ask this because I feel it's meaningless to find use an arbitrary method of fitting if there's no connection to the origin of the data set. Sure you can use polynomial fitting, FFT fitting, or complex power fitting, but how does this give any intuitive sense on why the data behaves in that manner?
 

Stiganator

Platinum Member
Oct 14, 2001
2,492
3
81
Well, I am looking at graphs of force and moment data from a publication. In photshop, I set the origin to the origin of the the scanned graph. Then I used the measure tool to measure the x an y displacement of about 200 points on the graph. I then calibrated the recorded values by using the gradient on the graphs themselves. Kinda like getting force data from a voltage if you follow what I'm saying and using known weights. Now I have this data which I would like to fit a funciton to and it is roughtly periodic. The requisite functions were not provided so I have to back calculate it in this very unpleasant manner.
 

TuxDave

Lifer
Oct 8, 2002
10,571
3
71
Originally posted by: Stiganator
Well, I am looking at graphs of force and moment data from a publication. In photshop, I set the origin to the origin of the the scanned graph. Then I used the measure tool to measure the x an y displacement of about 200 points on the graph. I then calibrated the recorded values by using the gradient on the graphs themselves. Kinda like getting force data from a voltage if you follow what I'm saying and using known weights. Now I have this data which I would like to fit a funciton to and it is roughtly periodic. The requisite functions were not provided so I have to back calculate it in this very unpleasant manner.

O_O lol... that's a very interesting way of getting data. As far as I know, to get an discrete FFT transform you need uniform sampling points which you stated that you don't. If you can somehow extract almost uniform points, you can use the FFT in matlab.

Some documentation

You're gonna get back a frequency spectrum and if you want to simplify your fitting equation, you could just take the peak frequencies instead of having it as the summation of all of those cos/sin
 

CycloWizard

Lifer
Sep 10, 2001
12,348
1
81
I would recommend contacting the publisher/author. They would probably give you the data if you asked for it. Of course, that's not so much fun, is it? :p
 

f95toli

Golden Member
Nov 21, 2002
1,547
0
0
Exactly what function are you trying to fit your data to? You say the data is "periodic", are you assuming that you can fit it as a sum of sines? Using how many terms?
The thing is, you can ALWAYS fit the data using a sum of sines -or any other basis function- but unless the function is truly perodic you need a lot of terms. Using fft is also tricky unless you have a lot of data,

Btw, you do NOT need uniform sampling points in order to use DFT, you can always resample you data.

I would recommend looking at the documentation for the nonlinear least-square routines in Matlab, they are very powerfull and if you want to fit a set of data to a specific expression it is usually the best method.

Note that if you just want to fit your data to ANY expression (for further data processing) you can (almost) always use splines, this is very easy to do in Matlab.




 

harrkev

Senior member
May 10, 2004
659
0
71
Well, if the data is periodic at all, then all you have to do is put it into matlab and run the FFT command on it. Note that I have not used matlab in a while. I forget if a 100 element array is numbered 0 to 99 or 1 to 100.

Let's assume that your data is 100 points long. and that vector elements are numbered 1 through 100. Here are some facts about the FFT of this data.

* You will get 100 COMPLEX points out. If you want to graph is, take the absolute value of the data to get the magnitude. The phase information will likely not be very useful to you.

* The data will be symmetrical. In other words, you could fold the graph is the middle, and the data will overlap. This is just how the FFT works. So you will be interested in points 1 through 50.

* The data at point 1 will be the average of all of the data (DC level).

* Let's assume that this plot was taken over T length of time, and you have N samples. Obviously, the sampling frequency F=N/T.

* The frequency of point P in your FFT is (p-1)/T. The "p-1" is there because vectors are numbered beginning at 1. Remove the one if your first element is numbered 0.

* The above results are only valid for values of P between 1 and 50, assuming that N=100. This is because if you sample at F frequency, you can only resolve signals at a frequency of F/2 or below (Nyquist frequency).

So, take the data, do an FFT, take the complex magnitude, and plot the first half of the data. Bada bing, bada boom. You are done. If you already know Matlab pretty well, we are talking all of five minutes here. Interpreting the results, however, is another matter.

You might also want to look into the DCT (discrete cosine transform). It is very closely related to the FFT, but it only gives real values, and does not have repeated data. JPEG and MPEG juse DCT for data compression.
 

silverpig

Lifer
Jul 29, 2001
27,703
12
81
You can only run an fft if your data points are equally spaced and there are no holes. If they're not equally spaced you need to run a regular FT.

I wrote my own lafler-kinman string length script in perl for an astro project I had to do... but it didn't work so well. I have a simple FT code I found too... I'll dig it up for you.
 

silverpig

Lifer
Jul 29, 2001
27,703
12
81
fourier.c

Save your data the same directory as input.dat
compile the program
run the program
add the real and imaginary parts of your components from output.dat and then square them. This will give you your frequency amplitudes. Plot the distributiion you get.
 

Stiganator

Platinum Member
Oct 14, 2001
2,492
3
81
6107 93
6087 25
6073 0
6058 -51
6041 -43
6011 -117
5991 -154
5978 -175
5951 -215
5922 -250
5883 -290
5889 -318
5794 -325
5749 -342
5709 -326
5641 -318
5599 -305
5557 -286
5514 -266
5453 -247
5397 -253
5321 -214
5245 -198
5181 -182
5050 -169
4979 -149
4911 -121
4838 -89
4778 -54
4690 -23
4599 12
4505 29
4371 30
4195 26
4111 50
4038 86
3981 113
3918 144
3869 170
3813 207
3746 247
3673 272
3603 304
3551 330
3505 353
3415 376
3325 374
3250 353
3203 324
3151 252
3119 217
3099 181
3073 154
3042 109
3022 72
2991 29
2969 -15
2946 -62
2895 -110
2870 -159
2826 -195
2795 -230
2733 -265
2678 -283
2615 -294
2553 -287
2488 -269
2423 -250
2362 -243
2319 -227
2257 -247
2197 -256
2147 -271
2067 -279
2042 -295
2015 -298
1949 -281
1877 -257
1817 -224
1751 -171
1715 -141
1678 -147
1643 -77
1519 -43
1523 0
1497 16
1453 58
1401 41
1345 31
1275 21
1215 13
1171 19
1115 -41
1095 -51
999 -73
946 -59
878 -22
825 -21
778 72
714 165
673 242
611 334
591 370
559 411
539 442
515 484
469 540
423 576
356 602
305 594
261 562
223 524
187 482
169 444
146 411
130 382
103 333
82 276
85 217
14 133

so here is the data, if anyone wan'ts to give it a go, I tried several methods and failed to get anything that looked even remotely close to ok. Um yeah, if anyone out there has a good means for fitting data of this nature, let me know
 

f95toli

Golden Member
Nov 21, 2002
1,547
0
0
You still haven't explained what you are trying to do. I took a look at the data and there is no way there is a 'simple' function that looks like that. There is no reason even to assume that the function is really periodic.

However, if you only want be able to interpolate you can for example use piecewise cubic Hermite polynomials.

In MATLAB (data in x,y)

x_interp=6107:-1:13;
y_interp=interp1(x,y,x_new,'pchip')
plot(x,y)
hold on
plot(x_interp,y_interp,'r')

The question is if this helps? Why do you need to find a function?




 

Stiganator

Platinum Member
Oct 14, 2001
2,492
3
81
I know there isn't a simple function, but something comprised of simple functions there could be, If there were 25 or fewer terms that is fine. I tried up to a 20 degree poly fit to no avail. Well I need this function so solve a system of ODEs.
 

f95toli

Golden Member
Nov 21, 2002
1,547
0
0
You do not need a function for that.
If you are solving it numerically (using for example Matlab) you can just use the raw data and interpolate the way I showed above. you simply define a function f(z)=interp1(x,y,z,'pchip')
this will return a value for any z-values (in the range defined by the x-vector) hence you can use it in whatever numerical algorithm you choose.

 

f95toli

Golden Member
Nov 21, 2002
1,547
0
0
The equations look ok as far as I can tell, but I don't really understand what you are trying to do in your program.

Why not use Ode45 to solve the system? Look up Ode45 in the Matlab help file and you will find an example which is very similar to your problem.
This should be easy to solve.