2

I would like to fit a function cos(2*pi*299.792258*t/x+p) to values given in the array vals to obtain values for t and p. This is my code:

myfit=fit(vals(:,1),vals(:,2),'cos(2*pi*299.792258*t/x+p)');
myfig=figure();
plot(vals(:,1),vals(:,2));hold all;
fplot(@(x) cos(2*pi*299.792258*myfit.t/x+myfit.p),[525,552]);

This yields the following plot where you can see that the fit is far off:

plot 1.

If I specify somewhat realistic start points, using myfit=fit(vals(:,1),vals(:,2),'cos(2*pi*299.792258*t/x+p)','StartPoint',[0,430]); I get a value of 424.4677 for t which fits much better, but still not perfectly (might be because the data is not 100% following the function):

plot 2

However,

  1. I am not sure how to deal with deviations in the data from the expected function like the peak between 535 and 540 which is clearly too narrow (they should get broader for higher values).
  2. I need to perform this fit for many measurements, which is why it would be nice not to have to set start values

Is there a nice(r) way to find both, t and p? I thought about taking the Fourier transform, but it doesn't work with my non-uniform spacing of data points.

Here's the data for vals that I was using for my example.

  525.0000   -1.1000
  525.0500   -0.9531
  525.1000   -0.7108
  525.1500   -0.5938
  525.2000   -0.4685
  525.2500   -0.3344
  525.3000   -0.3640
  525.3500   -0.1657
  525.4000   -0.0321
  525.4400    0.1252
  525.4900    0.1110
  525.5400    0.4472
  525.5900    0.4995
  525.6400    0.5651
  525.6900    0.8409
  525.7400    1.0000
  525.7900    0.8394
  525.8400    0.6728
  525.8800    0.5172
  525.9300    0.4648
  525.9800    0.3428
  526.0300    0.2219
  526.0800    0.0458
  526.1300   -0.0933
  526.1800   -0.3774
  526.2300   -0.4071
  526.2800   -0.5634
  526.3300   -0.5759
  526.3700   -0.7518
  526.4200   -0.9367
  526.4700   -1.0965
  526.5200   -1.0953
  526.5700   -0.9659
  526.6200   -0.9670
  526.6700   -1.0120
  526.7200   -1.0000
  526.7700   -0.9044
  526.8200   -0.8829
  526.8600   -0.8024
  526.9100   -0.6515
  526.9600   -0.5906
  527.0100   -0.5775
  527.0600   -0.5628
  527.1100   -0.3677
  527.1600   -0.2762
  527.2100   -0.1161
  527.2600    0.0335
  527.3100    0.1088
  527.3500    0.2409
  527.4000    0.3748
  527.4500    0.5507
  527.5000    0.5926
  527.5500    0.7832
  527.6000    0.8023
  527.6500    0.8028
  527.7000    0.8516
  527.7500    0.9808
  527.7900    1.0000
  527.8400    0.9331
  527.8900    0.9773
  527.9400    0.8937
  527.9900    0.7885
  528.0400    0.7090
  528.0900    0.6356
  528.1400    0.6109
  528.1900    0.4625
  528.2400    0.3755
  528.2800    0.2070
  528.3300    0.0542
  528.3800   -0.0264
  528.4300   -0.1577
  528.4800   -0.4389
  528.5300   -0.6134
  528.5800   -0.6532
  528.6300   -0.7401
  528.6800   -0.8190
  528.7300   -0.8971
  528.7700   -0.9827
  528.8200   -0.9645
  528.8700   -1.0000
  528.9200   -0.9400
  528.9700   -0.9053
  529.0200   -0.7697
  529.0700   -0.5713
  529.1200   -0.4241
  529.1700   -0.2051
  529.2100    0.0135
  529.2600    0.2426
  529.3100    0.3499
  529.3600    0.4279
  529.4100    0.5036
  529.4600    0.7153
  529.5100    0.7778
  529.5600    0.8562
  529.6100    0.8958
  529.6600    0.9538
  529.7000    1.0108
  529.7500    1.0091
  529.8000    1.0000
  529.8500    0.9402
  529.9000    0.8784
  529.9500    0.8820
  530.0000    0.8340
  530.0500    0.7770
  530.1000    0.6969
  530.1500    0.6544
  530.1900    0.5121
  530.2400    0.4394
  530.2900    0.3541
  530.3400    0.2367
  530.3900    0.1156
  530.4400    0.0206
  530.4900   -0.1054
  530.5400   -0.2046
  530.5900   -0.2951
  530.6300   -0.3490
  530.6800   -0.5051
  530.7300   -0.6063
  530.7800   -0.7500
  530.8300   -0.8191
  530.8800   -0.8779
  530.9300   -0.8637
  530.9800   -0.8580
  531.0300   -0.9665
  531.0800   -1.0000
  531.1200   -0.9702
  531.1700   -0.9975
  531.2200   -0.9919
  531.2700   -0.9271
  531.3200   -0.8668
  531.3700   -0.8524
  531.4200   -0.8012
  531.4700   -0.7345
  531.5200   -0.5624
  531.5700   -0.3037
  531.6100   -0.2050
  531.6600   -0.1268
  531.7100   -0.1359
  531.7600    0.0996
  531.8100    0.3117
  531.8600    0.4100
  531.9100    0.5910
  531.9600    0.8181
  532.0100    0.8417
  532.0600    0.9448
  532.1000    0.9096
  532.1500    1.0000
  532.2000    0.9737
  532.2500    0.8031
  532.3000    0.5236
  532.3500    0.4489
  532.4000    0.4240
  532.4500    0.3079
  532.5000    0.1911
  532.5400   -0.0560
  532.5900   -0.2665
  532.6400   -0.4700
  532.6900   -0.5619
  532.7400   -0.6287
  532.7900   -0.6853
  532.8400   -0.7193
  532.8900   -0.8840
  532.9400   -0.9882
  532.9900   -0.9498
  533.0300   -0.9596
  533.0800   -0.9790
  533.1300   -1.0000
  533.1800   -0.9380
  533.2300   -0.9404
  533.2800   -0.8582
  533.3300   -0.7732
  533.3800   -0.6395
  533.4300   -0.5452
  533.4800   -0.4859
  533.5200   -0.4455
  533.5700   -0.3387
  533.6200   -0.2137
  533.6700   -0.0607
  533.7200    0.0415
  533.7700    0.1501
  533.8200    0.2585
  533.8700    0.3499
  533.9200    0.4231
  533.9600    0.5564
  534.0100    0.6348
  534.0600    0.7583
  534.1100    0.7754
  534.1600    0.8272
  534.2100    0.8538
  534.2600    0.9319
  534.3100    0.9217
  534.3600    0.9750
  534.4100    0.9934
  534.4500    0.9922
  534.5000    0.9878
  534.5500    1.0000
  534.6000    0.9497
  534.6500    0.9044
  534.7000    0.8488
  534.7500    0.8210
  534.8000    0.7329
  534.8500    0.7144
  534.9000    0.6669
  534.9400    0.5800
  534.9900    0.4744
  535.0400    0.3950
  535.0900    0.2793
  535.1400    0.1530
  535.1900    0.0600
  535.2400   -0.0211
  535.2900   -0.1341
  535.3400   -0.2141
  535.3800   -0.3233
  535.4300   -0.3941
  535.4800   -0.5181
  535.5300   -0.5882
  535.5800   -0.7054
  535.6300   -0.8081
  535.6800   -0.8747
  535.7300   -0.9486
  535.7800   -1.0000
  535.8300   -1.0371
  535.8700   -1.0417
  535.9200   -1.1081
  535.9700   -1.2124
  536.0200   -1.2970
  536.0700   -1.2946
  536.1200   -1.2352
  536.1700   -1.2726
  536.2200   -1.2838
  536.2700   -1.2992
  536.3200   -1.0946
  536.3600   -1.0587
  536.4100   -0.9981
  536.4600   -0.6557
  536.5100   -0.7914
  536.5600   -0.5578
  536.6100   -0.4713
  536.6600   -0.1590
  536.7100    0.3006
  536.7600    0.8302
  536.8000    1.0000
  536.8500    0.9092
  536.9000    0.9436
  536.9500    0.8408
  537.0000    1.0000
  537.0500    0.4950
  537.1000    0.3749
  537.1500    0.2737
  537.2000   -0.1488
  537.2500   -0.1851
  537.2900   -0.5675
  537.3400   -0.7166
  537.3900   -0.7574
  537.4400   -0.6974
  537.4900   -0.7826
  537.5400   -0.8295
  537.5900   -0.8402
  537.6400   -0.9679
  537.6900   -0.9624
  537.7400   -1.0440
  537.7800   -1.1150
  537.8300   -1.1104
  537.8800   -1.1265
  537.9300   -1.0608
  537.9800   -1.0299
  538.0300   -1.0423
  538.0800   -1.0741
  538.1300   -1.0107
  538.1800   -1.0658
  538.2300   -1.0167
  538.2700   -1.0000
  538.3200   -0.9282
  538.3700   -0.8739
  538.4200   -0.8625
  538.4700   -0.7903
  538.5200   -0.7136
  538.5700   -0.6588
  538.6200   -0.5777
  538.6700   -0.5083
  538.7100   -0.4690
  538.7600   -0.3299
  538.8100   -0.2286
  538.8600   -0.1704
  538.9100   -0.0719
  538.9600    0.0083
  539.0100    0.1137
  539.0600    0.1972
  539.1100    0.3075
  539.1600    0.4015
  539.2000    0.4803
  539.2500    0.5866
  539.3000    0.6844
  539.3500    0.7447
  539.4000    0.7833
  539.4500    0.8842
  539.5000    0.9298
  539.5500    0.9515
  539.6000    0.9604
  539.6500    0.9959
  539.6900    1.0000
  539.7400    0.9990
  539.7900    0.9872
  539.8400    0.9631
  539.8900    0.9221
  539.9400    0.9133
  539.9900    0.8637
  540.0400    0.8377
  540.0900    0.8204
  540.1300    0.8116
  540.1800    0.7283
  540.2300    0.6547
  540.2800    0.6080
  540.3300    0.5645
  540.3800    0.5043
  540.4300    0.4358
  540.4800    0.3722
  540.5300    0.3259
  540.5800    0.2286
  540.6200    0.2023
  540.6700    0.0963
  540.7200   -0.0249
  540.7700   -0.1430
  540.8200   -0.2607
  540.8700   -0.3918
  540.9200   -0.4483
  540.9700   -0.5095
  541.0200   -0.5836
  541.0700   -0.6704
  541.1100   -0.7295
  541.1600   -0.7924
  541.2100   -0.8971
  541.2600   -0.9749
  541.3100   -0.9550
  541.3600   -0.9783
  541.4100   -1.0000
  541.4600   -0.9650
  541.5100   -0.9674
  541.5500   -0.8814
  541.6000   -0.8534
  541.6500   -0.8102
  541.7000   -0.7576
  541.7500   -0.6679
  541.8000   -0.6153
  541.8500   -0.5856
  541.9000   -0.4698
  541.9500   -0.3490
  542.0000   -0.2035
  542.0400   -0.1218
  542.0900   -0.0102
  542.1400    0.1237
  542.1900    0.2250
  542.2400    0.2819
  542.2900    0.3594
  542.3400    0.4767
  542.3900    0.5554
  542.4400    0.6098
  542.4900    0.6575
  542.5300    0.7315
  542.5800    0.8207
  542.6300    0.8470
  542.6800    0.8921
  542.7300    0.8906
  542.7800    0.9144
  542.8300    0.9315
  542.8800    0.9628
  542.9300    0.9508
  542.9800    1.0000
  543.0200    0.9541
  543.0700    0.9344
  543.1200    0.8845
  543.1700    0.9157
  543.2200    0.8910
  543.2700    0.8914
  543.3200    0.7862
  543.3700    0.7562
  543.4200    0.7119
  543.4600    0.6365
  543.5100    0.4962
  543.5600    0.3867
  543.6100    0.2561
  543.6600    0.1928
  543.7100    0.1094
  543.7600    0.1054
  543.8100   -0.0342
  543.8600   -0.1034
  543.9100   -0.2209
  543.9500   -0.2948
  544.0000   -0.4151
  544.0500   -0.4166
  544.1000   -0.5028
  544.1500   -0.5590
  544.2000   -0.6216
  544.2500   -0.6051
  544.3000   -0.6577
  544.3500   -0.7203
  544.4000   -0.8148
  544.4400   -0.8656
  544.4900   -1.0000
  544.5400   -0.9584
  544.5900   -0.9993
  544.6400   -1.0445
  544.6900   -1.0358
  544.7400   -1.0597
  544.7900   -1.1020
  544.8400   -1.0974
  544.8800   -1.1099
  544.9300   -1.0965
  544.9800   -1.1110
  545.0300   -0.9856
  545.0800   -0.9418
  545.1300   -0.8764
  545.1800   -0.8397
  545.2300   -0.7457
  545.2800   -0.7009
  545.3300   -0.5820
  545.3700   -0.4724
  545.4200   -0.3525
  545.4700   -0.1648
  545.5200   -0.0132
  545.5700    0.2190
  545.6200    0.3475
  545.6700    0.4397
  545.7200    0.5649
  545.7700    0.5535
  545.8200    0.7693
  545.8600    0.9241
  545.9100    0.9968
  545.9600    1.0000
  546.0100    0.9528
  546.0600    0.8844
  546.1100    0.8604
  546.1600    0.8692
  546.2100    0.7248
  546.2600    0.6089
  546.3000    0.3552
  546.3500    0.2552
  546.4000    0.1719
  546.4500    0.1662
  546.5000    0.0449
  546.5500   -0.0490
  546.6000   -0.1754
  546.6500   -0.2916
  546.7000   -0.3144
  546.7500   -0.3588
  546.7900   -0.4253
  546.8400   -0.5250
  546.8900   -0.6858
  546.9400   -0.7997
  546.9900   -0.8641
  547.0400   -0.8925
  547.0900   -0.8946
  547.1400   -0.9498
  547.1900   -0.9727
  547.2400   -1.0246
  547.2800   -1.0507
  547.3300   -1.0421
  547.3800   -1.0257
  547.4300   -1.0273
  547.4800   -0.9969
  547.5300   -1.0000
  547.5800   -0.9510
  547.6300   -0.9157
  547.6800   -0.8474
  547.7300   -0.8093
  547.7700   -0.7610
  547.8200   -0.7131
  547.8700   -0.6131
  547.9200   -0.5680
  547.9700   -0.4640
  548.0200   -0.4302
  548.0700   -0.3732
  548.1200   -0.2839
  548.1700   -0.2637
  548.2100   -0.2200
  548.2600   -0.1664
  548.3100   -0.1274
  548.3600   -0.0538
  548.4100    0.0337
  548.4600    0.1273
  548.5100    0.2211
  548.5600    0.2582
  548.6100    0.2802
  548.6600    0.3311
  548.7000    0.3429
  548.7500    0.4219
  548.8000    0.4644
  548.8500    0.5038
  548.9000    0.5790
  548.9500    0.6305
  549.0000    0.6990
  549.0500    0.7754
  549.1000    0.8110
  549.1500    0.8616
  549.1900    0.8614
  549.2400    0.8697
  549.2900    0.9070
  549.3400    0.9382
  549.3900    0.9429
  549.4400    0.9809
  549.4900    1.0000
  549.5400    0.9999
  549.5900    0.9758
  549.6300    0.9330
  549.6800    0.8869
  549.7300    0.8409
  549.7800    0.7811
  549.8300    0.7373
  549.8800    0.6869
  549.9300    0.6711
  549.9800    0.6422
  550.0300    0.5927
  550.0800    0.5134
  550.1200    0.4720
  550.1700    0.4076
  550.2200    0.3190
  550.2700    0.2680
  550.3200    0.2257
  550.3700    0.1799
  550.4200    0.1487
  550.4700    0.0835
  550.5200    0.0674
  550.5700    0.0218
  550.6100   -0.0450
  550.6600   -0.1028
  550.7100   -0.1615
  550.7600   -0.2260
  550.8100   -0.2616
  550.8600   -0.3127
  550.9100   -0.3548
  550.9600   -0.4267
  551.0100   -0.5147
  551.0500   -0.5597
  551.1000   -0.6274
  551.1500   -0.6562
  551.2000   -0.6779
  551.2500   -0.7514
  551.3000   -0.7731
  551.3500   -0.7989
  551.4000   -0.8420
  551.4500   -0.8679
  551.5000   -0.9131
  551.5400   -0.9229
  551.5900   -0.9526
  551.6400   -1.0000
  551.6900   -1.0065
  551.7400   -0.9542
  551.7900   -0.9381
  551.8400   -0.9151
  551.8900   -0.9639
  551.9400   -0.9017
  551.9900   -0.8389
techraf
  • 64,883
  • 27
  • 193
  • 198
riddleculous
  • 267
  • 4
  • 15
  • Could you probably use the FFT to estimate the frequency? If you have one cosine, there should be one distinct peak in the spectrum.. – hbaderts Mar 09 '16 at 13:01
  • You can interpolate the data to uniform spacing using `interp1` and then use `fft` to find the frequency. – buzjwa Mar 09 '16 at 13:14
  • 1
    I have a bad and a good news. The bad one is that your example cannot be fiited by function You show. The good news is that you can use more complex function than `cos(a/x+b)` see `doc fittype`. – Crowley Mar 09 '16 at 13:23
  • @Naveh That's what came to my mind, too. I had the problem of getting just NaNs, but I figured it out. Give me some time for trying out the fft resp. ifft in my case. Thanks for the hint anyway – riddleculous Mar 09 '16 at 13:24
  • @hbaderts: I'm trying to do an ifft on the omega space (angular frequency rather than wavelength) now – riddleculous Mar 09 '16 at 13:25
  • @Crowley: the problem is that I would like to get a result like that function (with an error that can be quite large), see my point one. I'm trying some transformations and an ifft now – riddleculous Mar 09 '16 at 13:28
  • this actually led me to a completely [new question on fft/ifft](http://stackoverflow.com/questions/35899611/right-way-of-performing-inverse-fft-in-matlab) which I need to solve before I can make a progress here. Consequently, I need some more time to figure it out. – riddleculous Mar 09 '16 at 18:26

0 Answers0