4
$\begingroup$

I imported some data in Mathematica which I cleaned a bit. This process went well. I also managed to plot it, and tried to take the Fourier transform of the data and plot it again. I'm not sure if I did this correctly (see code). Now I would like to fit the data with help of NonlinearModelFit and use the initial guesses of the Fourier transform. Can someone help me with this? Sorry for the long data...

This is the data after I imported and cleaned it:

raw = {{-3.84945, 
  0.41909}, {-3.43009, -0.0701917}, {-3.73715, -0.0764585}, {-4.35638,
   0.302424}, {-3.67285, -0.147868}, {-3.39557, -0.256599}, \
{-3.78187, -0.369644}, {-3.47578, -0.251411}, {-3.5905, -0.361445}, \
{-3.32659, 0.140039}, {-3.33448, -0.0781868}, {-3.2252, 
  0.180387}, {-3.42825, 0.0858649}, {-2.87733, 
  0.120913}, {-2.97688, -0.15721}, {-3.40708, 0.788854}, {-3.06072, 
  0.939109}, {-2.60687, 0.734961}, {-2.30167, 0.66035}, {-2.70302, 
  0.926581}, {-2.6086, 0.938341}, {-3.19459, 1.08926}, {-2.56533, 
  1.34221}, {-3.00272, 1.50904}, {-2.81819, 1.30194}, {-2.36322, 
  1.79618}, {-2.64252, 1.17821}, {-2.35468, 1.26401}, {-2.36687, 
  2.09873}, {-2.20697, 1.43182}, {-2.35019, 1.67195}, {-2.11614, 
  1.02184}, {-1.72058, 1.27401}, {-1.96324, 2.19287}, {-1.80049, 
  1.60412}, {-1.39003, 1.32896}, {-1.73336, 1.75028}, {-1.787, 
  1.04856}, {-1.77951, 1.00185}, {-1.37982, 0.647201}, {-1.42423, 
  0.916547}, {-1.69202, 
  0.268595}, {-1.26665, -0.184731}, {-1.62388, -0.929862}, {-1.70419, \
-0.250538}, {-1.38926, -1.41213}, {-1.40997, -0.830337}, {-1.35806, \
-1.22583}, {-0.800889, -1.15913}, {-1.44766, -1.29397}, {-1.39447, \
-1.65196}, {-0.981133, -1.87578}, {-1.17633, -2.31197}, {-0.889797, \
-1.66592}, {-0.901875, -1.44375}, {-0.76148, -1.4741}, {-0.951907, \
-1.68008}, {-0.845173, -1.5895}, {-0.782567, -0.947551}, {-0.265738, \
-0.753805}, {0.203795, -0.232802}, {-0.203745, -0.707963}, \
{-0.489359, -0.227754}, {-0.686342, 
  0.130555}, {-0.210241, -0.0794845}, {0.0358392, 
  0.0663956}, {0.0993884, 0.542909}, {0.0527959, 
  0.664387}, {-0.391205, 1.38693}, {0.198899, 1.96552}, {-0.3053, 
  1.42773}, {0.307131, 2.10399}, {0.494083, 1.91009}, {0.118677, 
  2.19167}, {0.483712, 2.54929}, {0.727716, 2.41371}, {0.434194, 
  2.67301}, {0.698968, 2.89684}, {0.752124, 1.99418}, {0.863017, 
  1.83665}, {0.693201, 2.4055}, {1.2444, 2.20828}, {0.48213, 
  1.9665}, {1.17306, 1.76448}, {0.910287, 1.71914}, {1.31443, 
  1.91416}, {1.04699, 1.08547}, {1.57716, 0.978914}, {1.37886, 
  0.670282}, {1.61043, 
  0.734624}, {1.58938, -0.0783559}, {1.15152, -0.731489}, {0.99385, \
-0.348013}, {2.32758, -0.829126}, {1.32128, -0.756824}, {1.78092, \
-1.19643}, {1.56061, -1.35187}, {1.79447, -1.43311}, {2.53148, \
-1.19062}, {1.68578, -0.95508}, {2.22307, -1.53828}, {2.0539, \
-1.20883}, {2.49747, -1.87838}, {2.58535, -1.33704}, {2.01095, \
-1.50991}, {2.29196, -1.87351}, {2.59162, -1.04007}, {2.67627, \
-0.987542}, {2.16628, -0.955456}, {2.82849, -0.325863}, {2.55187, 
  0.0104294}, {2.4016, -0.00569572}, {2.72076, -0.357926}, {2.66823, 
  0.0574908}, {3.0795, 1.0393}, {3.05506, 0.707203}, {2.44507, 
  0.678553}, {2.77322, 0.639886}, {3.2439, 0.318019}, {3.33037, 
  0.992247}, {3.61846, 1.46687}, {2.91627, 1.13617}, {3.32023, 
  1.30646}, {3.79645, 1.79151}, {3.29877, 0.384929}, {3.49623, 
  1.18847}, {3.77365, 0.723413}, {2.9929, 1.41775}, {3.96285, 
  1.16511}, {4.12542, 1.42441}, {3.51613, 0.912884}, {3.68492, 
  0.614977}, {4.19006, 0.877668}, {3.95165, 0.0847349}, {4.16917, 
  0.831189}, {4.07692, 0.476545}, {4.0942, -0.252477}, {4.66, 
  0.25104}, {4.42228, 0.0328571}, {4.08766, 0.0525696}, {4.23193, 
  0.765989}, {4.51484, -0.0114567}, {4.16436, 0.801093}, {4.6914, 
  0.486112}, {4.58733, 0.259372}, {4.80848, 0.192874}, {4.4567, 
  0.397505}, {4.89128, 0.0168966}, {5.05161, 
  0.843062}, {5.32769, -0.0367531}, {5.25601, -0.233841}, {5.35209, 
  0.563215}, {5.41444, 1.26534}, {4.87629, 0.926255}, {4.77206, 
  0.506746}, {5.19298, 0.401957}, {5.5446, 0.321771}, {5.06212, 
  0.770791}, {5.25317, 0.319211}, {5.53843, -0.065951}, {5.71233, 
  1.42287}, {5.88261, 0.311685}, {6.0609, 0.601066}, {5.25909, 
  0.0540723}, {5.98049, 0.385066}, {5.88725, 
  0.347108}, {5.74741, -0.449404}, {6.18538, 
  0.119061}, {6.19502, -0.162133}, {6.41258, -0.332392}, {6.33224, 
  0.098131}, {6.46565, -0.380567}, {6.67733, -0.812768}, {6.61725, \
-0.81194}, {5.82928, -0.765799}, {6.3932, -0.710182}, {6.13977, \
-0.810558}, {6.20525, -1.86373}, {6.24772, -0.672002}, {6.65897, \
-0.881107}, {6.45743, -1.00039}, {6.66579, -1.16609}, {6.93156, \
-0.349315}, {6.75027, -0.60295}, {6.73847, -0.987719}, {7.27493, \
-0.154233}, {6.9856, -0.277668}, {6.93401, 0.341431}, {7.23205, 
  0.657917}, {7.31986, 0.713966}, {7.38186, 1.00859}, {7.55577, 
  0.905797}, {7.69449, 1.07072}, {7.07342, 0.923475}, {7.55928, 
  1.4013}, {7.86274, 1.63709}, {8.05143, 1.85425}, {7.84046, 
  2.08721}, {8.16941, 1.7188}, {7.71405, 1.67075}, {8.06282, 
  2.72949}, {7.59044, 2.25971}, {7.82557, 2.58164}, {7.97188, 
  2.05505}, {7.97032, 2.31209}, {7.67221, 2.05983}, {8.41049, 
  1.96376}, {8.20171, 1.96529}, {8.4695, 1.10212}, {8.55157, 
  1.53035}, {9.11358, 1.23181}, {8.67359, 
  0.40237}, {8.57122, -0.211248}, {8.87154, -0.0230956}, {8.96914, \
-0.555904}, {8.44334, -0.305055}, {8.80015, -0.394893}, {9.23605, \
-0.0958523}, {9.08507, -1.11365}, {9.18054, -1.15248}, {9.14974, \
-1.46007}, {9.3732, -0.704947}, {8.70995, -1.11768}, {9.26443, \
-1.55281}, {9.51184, -1.42069}, {9.86547, -2.0223}, {9.28803, \
-1.88908}, {9.53138, -1.62807}, {9.52, -1.91898}, {9.88404, \
-1.37459}, {9.68215, -1.36107}, {9.76359, -1.41102}, {9.6818, \
-1.28005}, {9.85947, -1.11897}, {10.3673, -1.2498}, {10.196, \
-0.963601}, {9.64796, 0.36641}, {10.0035, 0.43566}, {10.6284, 
  0.582454}, {9.98143, 0.751192}, {9.91869, 0.544871}, {10.0103, 
  1.75997}, {10.8443, 1.45478}, {10.2117, 1.59547}, {10.2591, 
  2.17922}, {10.7609, 1.93402}, {10.3892, 2.19155}, {10.4554, 
  2.32495}, {10.8657, 2.55519}, {10.4471, 2.16553}, {11.3071, 
  2.67997}, {10.9587, 2.20251}, {10.9507, 2.66354}, {11.5903, 
  1.71928}, {11.0943, 1.64894}, {10.7085, 2.36607}, {10.926, 
  1.10288}, {11.1153, 1.18289}, {11.769, 0.542454}, {11.4704, 
  0.708442}, {11.4349, 1.05461}, {11.7382, 0.398513}, {11.0161, 
  0.215053}, {11.4169, -0.153773}, {11.0926, 
  0.252947}, {11.8178, -0.312971}, {11.2996, -0.320814}, {11.8535, \
-1.18744}, {11.504, 
  0.104621}, {11.9314, -1.28179}, {12.149, -0.640361}, {11.7889, \
-0.98922}, {12.2323, -0.424164}, {12.3322, -0.63422}, {12.4638, \
-0.615115}, {11.2903, -0.851769}, {11.9001, -1.28174}, {12.3405, \
-0.942403}, {12.0453, -0.380617}, {12.7846, -1.10662}, {12.3655, \
-0.336232}, {12.5602, -0.871577}, {12.2629, -0.569801}, {13.1561, \
-0.160132}, {12.8923, 0.12894}, {13.3888, 0.352126}, {13.1609, 
  0.288666}, {13.0405, 0.445245}, {13.1501, 0.237405}, {12.4543, 
  0.153608}, {12.9637, 0.446174}, {13.5627, 0.600116}, {13.5393, 
  0.452977}, {13.7631, 0.0165395}, {13.3105, 0.764879}, {13.0265, 
  0.848369}, {13.0003, 0.215074}, {12.9275, 0.433183}, {13.416, 
  0.420133}, {14.4767, 0.141986}, {14.0554, 0.0950555}, {14.0878, 
  0.967855}, {14.2177, -0.0206047}, {14.0884, 0.177076}, {14.2077, 
  0.38328}, {13.8715, 0.369896}, {13.6763, 
  0.640263}, {13.7049, -0.338782}, {14.36, 0.204381}, {14.1027, 
  0.105494}, {14.0862, -0.174187}, {14.1833, 0.403098}, {14.3719, 
  0.058492}, {14.3079, 0.178501}, {14.3452, 0.603739}, {14.7612, 
  0.0737788}, {15.0454, 1.20668}, {14.6153, 0.566124}, {15.07, 
  1.11673}, {15.1651, 0.330462}, {14.7917, 0.386365}, {14.4027, 
  0.970053}, {15.4735, 1.34522}, {15.0933, 1.40533}, {14.8998, 
  1.92972}, {14.9064, 1.21432}, {14.9657, 1.60656}, {15.5177, 
  1.7208}, {15.6814, 1.65606}, {15.6126, 1.36729}, {16.0271, 
  1.27835}, {15.3855, 0.994342}, {15.3689, 1.07696}, {15.4025, 
  0.770641}, {16.1808, 0.588181}, {15.7075, 0.857503}, {15.9243, 
  0.573647}, {16.0403, 
  0.118569}, {15.9525, -0.717673}, {16.0019, -0.551354}, {16.3071, 
  0.116131}, {16.0534, -0.633346}, {16.3187, -0.703357}, {16.5247, \
-0.84655}, {15.8993, -0.544992}, {16.3177, -1.52559}, {16.5718, \
-1.4958}, {16.83, -1.56958}, {17.2945, -1.79144}, {16.595, -1.79918}, \
{17.2579, -2.66737}, {17.0576, -1.89032}, {17.4953, -1.33387}, \
{16.9285, -1.30165}, {16.7132, -1.36638}, {17.0759, -1.22273}, \
{16.8522, -0.932177}, {16.9162, -0.904871}, {17.2847, -0.728184}, \
{17.6323, -0.457726}, {16.8706, 0.493485}, {17.7072, 
  0.834704}, {17.3644, -0.172504}, {17.8227, 0.239913}, {17.6079, 
  0.676131}, {17.9362, 1.4603}, {17.7336, 2.13347}, {17.9287, 
  1.69107}, {17.0874, 2.3189}, {17.751, 2.22933}, {17.7757, 
  1.90009}, {17.37, 2.94452}, {18.3753, 2.6209}, {18.4807, 
  1.95453}, {18.3759, 1.73523}, {18.1076, 2.51405}, {18.1103, 
  2.71764}, {18.1963, 3.15662}, {18.3686, 2.21991}, {18.3298, 
  1.37076}, {18.2522, 2.05346}, {18.1177, 2.38566}, {18.8152, 
  1.43102}, {18.6016, 1.53189}, {18.8635, 
  1.3898}, {18.8634, -0.038433}, {18.7331, 
  0.431433}, {18.8891, -0.511772}, {18.7633, -0.869485}, {18.7467, \
-1.04817}, {18.7679, -1.15884}, {19.1501, 
  0.00665202}, {19.562, -1.42274}, {19.305, -1.4593}, {19.5042, \
-1.62149}, {19.6718, -2.18218}, {19.7465, -1.39044}, {19.4772, \
-2.25948}, {18.9855, -1.57927}, {19.7925, -1.11085}, {19.6894, \
-1.34807}, {19.4497, -1.18651}, {19.7962, -1.70302}, {19.6606, \
-0.849571}, {19.9106, -1.23803}, {19.997, -1.53098}, {19.1314, \
-1.00194}, {20.5465, -0.432927}, {19.7919, -0.536015}, {20.1419, \
-0.255262}, {20.7293, 0.389489}, {19.8333, 0.426571}, {20.4569, 
  0.43273}, {20.2052, 0.599052}, {20.2941, 0.984699}, {20.555, 
  1.15831}, {20.6175, 0.780267}, {21.0713, 1.1623}, {20.5974, 
  2.08726}, {20.9737, 1.60641}, {20.8813, 1.79997}, {21.1295, 
  1.83323}, {20.4977, 1.1313}, {21.1927, 1.87942}, {21.0748, 
  1.29658}, {21.5025, 1.74935}, {21.659, 1.88678}, {21.3913, 
  1.29054}, {21.4118, 1.11935}, {21.0063, 0.844062}, {22.0389, 
  0.645558}, {21.5241, 1.15722}, {22.0532, 1.02463}, {21.732, 
  1.40229}, {21.8578, 0.646097}, {21.9427, -0.380885}, {22.21, 
  0.543236}, {21.9268, 0.00279749}, {21.6922, -0.331825}, {21.7628, 
  0.365264}, {22.0055, 0.00881415}, {22.09, 
  0.0444232}, {22.7028, -0.188965}, {22.0422, -0.317193}, {22.254, \
-0.132849}, {22.0351, -0.488951}, {22.5506, -0.368061}, {22.2605, 
  0.0353715}, {22.9485, 0.0632572}, {22.6993, 0.594619}, {22.3263, 
  0.183907}, {22.3584, 0.531802}, {22.9162, 0.419526}, {22.8722, 
  0.352037}, {22.2147, 0.455423}, {22.6277, 0.857066}, {22.7334, 
  0.603724}, {22.4725, 0.221121}, {23.0407, 0.715812}, {22.8647, 
  0.56642}, {23.5909, 0.279728}, {22.7275, 0.341009}, {23.6831, 
  0.281944}, {23.52, 0.584931}, {23.7145, 0.187598}, {23.9755, 
  0.148709}, {23.9498, -0.517361}, {23.8175, -0.202544}, {23.4968, 
  0.00215051}, {24.1129, -0.440127}, {23.8927, -0.568414}, {23.949, \
-0.652151}, {23.843, -0.474553}, {24.0124, -0.0738271}, {23.9999, \
-0.835919}, {24.1444, -0.87771}, {24.2111, 
  0.386522}, {24.2819, -0.962728}, {24.3082, -1.48557}, {23.9496, \
-0.893721}, {24.6539, 
  0.0288777}, {25.0443, -0.519709}, {24.3552, -0.164626}, {24.6644, 
  0.688696}, {24.6359, -0.189839}, {24.6877, 0.200085}, {24.5175, 
  0.0464359}, {24.6399, 0.534559}, {24.8586, 0.968424}, {24.8228, 
  0.577755}, {25.1139, 1.27134}, {25.4055, 1.50194}, {25.0748, 
  1.32378}, {24.7524, 2.53979}, {25.1419, 2.06432}, {25.984, 
  2.30706}, {25.4521, 2.42612}, {25.588, 2.65184}, {25.6702, 
  1.30271}, {25.6293, 2.02464}, {25.806, 1.35835}, {25.7163, 
  2.49362}, {25.5422, 2.30273}, {25.3588, 2.08415}, {25.9634, 
  1.23174}, {25.8309, 1.2558}, {25.2417, 0.877313}, {26.4366, 
  0.880501}, {25.524, 0.0670857}, {26.0663, 0.348963}, {26.3491, 
  0.176462}, {26.2812, -0.052508}, {26.498, 
  0.0692194}, {26.018, -0.573689}, {25.9109, -0.72646}, {26.5291, \
-0.658361}, {26.4644, -1.01012}, {26.3802, -1.0911}, {26.6247, \
-1.98007}, {26.8425, -1.98544}, {26.569, -1.31316}, {26.9653, \
-1.89887}, {26.9101, -2.30085}, {26.7882, -2.44873}, {27.1402, \
-1.64809}, {27.2906, -1.13718}, {27.0004, -1.54148}, {27.1244, \
-1.50756}, {27.6901, -1.44902}, {27.2557, -1.66404}, {27.6999, \
-0.822905}, {27.4095, -0.604445}, {27.635, -0.583706}, {27.1973, \
-0.194272}, {28.1471, 0.806316}, {27.2559, 0.776763}, {27.4588, 
  0.633466}, {27.5602, 0.580108}, {27.8279, 1.26428}, {27.3865, 
  1.72679}, {28.1308, 1.58991}, {28.0229, 1.89539}, {27.7915, 
  1.82904}, {28.5813, 1.88801}, {28.0183, 2.42584}, {28.0626, 
  2.45304}, {28.6464, 2.48027}, {28.619, 2.27061}, {28.668, 
  2.48265}, {28.419, 1.76506}, {28.5921, 2.03448}, {28.8383, 
  1.85392}, {29.1858, 1.98876}, {28.4366, 1.74753}, {28.6991, 
  1.42565}, {29.2494, 1.61516}, {28.9407, 0.626596}, {28.6204, 
  1.02512}, {28.8652, 0.261672}, {29.2054, 1.06004}, {29.242, 
  0.335609}, {28.7962, -0.205257}, {28.9726, 
  0.0328908}, {29.711, -0.636743}, {29.6339, -0.886832}, {28.6801, \
-0.550744}, {29.3622, -0.147409}, {29.3855, -0.923175}, {29.8079, \
-1.57947}, {29.5734, -1.50112}, {29.8371, -1.32898}, {29.2352, \
-1.23009}, {29.8375, -1.11249}, {30.1836, -1.40015}, {30.2696, \
-0.965731}, {30.0036, -0.769046}, {30.1643, -0.86547}, {29.9663, \
-1.10144}, {30.2157, -0.646887}, {30.4718, -0.749548}, {29.9071, 
  0.0479569}, {30.7434, -0.780383}, {29.9202, 0.031815}, {30.6368, 
  0.560066}, {30.0345, -0.478445}, {30.674, 0.085706}, {30.8219, 
  0.0425936}, {31.0691, 0.873446}, {30.799, 0.533679}, {31.4399, 
  0.976093}, {31.1288, 0.887502}, {31.0745, 1.25471}, {30.7318, 
  0.435779}, {31.2397, 0.626573}, {30.7587, 0.555628}, {31.2223, 
  0.314188}, {31.3343, 0.470874}, {31.2738, 0.224425}, {31.6043, 
  0.945618}, {31.2746, 0.933819}, {31.7258, 0.191166}, {31.6803, 
  0.945932}, {31.8472, 0.718703}, {31.4919, 0.198104}, {31.374, 
  0.0570873}, {31.679, 0.425878}, {31.7958, 
  0.255821}, {31.7202, -0.133875}, {31.9048, 
  0.166323}, {32.3564, -0.381504}, {31.9391, 0.294844}, {32.3781, 
  0.07096}, {32.5547, -0.0629246}, {32.5829, 0.310593}, {32.364, 
  0.424169}, {31.8653, 0.674023}, {32.3417, 0.939597}, {32.5952, 
  0.910231}, {32.3203, 0.0350652}, {33.1148, 1.28598}, {32.3861, 
  1.05036}, {32.6759, 1.04992}, {32.6562, 1.36831}, {32.8406, 
  0.878374}, {32.8919, 1.05826}, {33.2898, 0.888194}, {33.5007, 
  1.18185}, {33.3491, 1.43886}, {33.3031, 1.15295}, {33.5922, 
  1.27276}, {33.6061, 0.608601}, {33.0871, 1.00682}, {33.4731, 
  0.520619}, {33.7312, 
  0.437054}, {33.7397, -0.286281}, {33.6321, -0.444827}, {34.3418, \
-0.0509425}, {34.13, -0.531038}, {33.5703, -0.79537}, {33.5757, \
-0.716751}, {33.978, 
  0.146486}, {33.8335, -0.485087}, {34.1577, -1.088}, {34.7128, \
-0.791766}, {33.9744, -1.46449}, {34.2497, -1.2427}, {34.6004, \
-1.16748}, {34.3232, -1.62984}, {34.3331, -1.65776}, {34.1108, \
-1.41943}, {34.6287, -1.14177}, {34.0175, -1.25963}, {34.501, \
-1.1609}, {34.1351, -0.965139}, {34.2951, -1.06006}, {35.4621, \
-0.733495}, {35.2401, -0.16984}, {34.5547, 
  0.524605}, {34.6849, -0.223118}, {34.644, 0.452821}, {35.0712, 
  0.951466}, {35.6274, 0.715463}, {35.343, 0.58703}, {35.0355, 
  1.41219}, {35.2829, 2.04928}, {34.6803, 2.0764}, {35.6433, 
  1.43312}, {34.967, 3.1381}, {35.7756, 2.32153}, {36.1304, 
  1.68146}, {35.8756, 2.0023}, {35.7894, 2.5185}, {35.5619, 
  2.52503}, {35.981, 2.30594}, {36.1471, 1.70983}, {36.0046, 
  3.01538}, {35.6653, 1.54435}, {36.1309, 1.85956}, {36.1551, 
  1.73553}, {36.1421, 1.39904}, {36.7871, 1.55462}, {36.6833, 
  1.23175}, {35.9592, 0.877241}, {36.9764, 
  0.711913}, {36.5436, -0.0172662}, {36.6802, 
  0.52497}, {36.5682, -0.539816}, {36.6328, -1.39402}, {36.8861, \
-0.815778}, {36.8163, -1.37152}, {36.2794, -1.40483}, {37.1657, \
-1.52705}, {37.1711, -1.94097}, {37.0081, -2.36248}, {36.8495, \
-1.44548}, {37.0087, -2.02753}, {36.8872, -1.47031}, {37.0955, \
-1.83628}, {36.722, -1.91471}, {37.5261, -1.62925}, {37.2238, \
-1.0598}, {37.8914, -2.10623}, {37.8759, -0.837536}, {37.5999, \
-0.672926}, {37.8223, -0.301448}, {37.5606, -0.52661}, {37.4943, \
-0.018456}, {37.8968, 0.118564}, {38.2857, 0.135554}, {38.6949, 
  0.95564}, {37.7182, 0.52255}, {38.054, 1.5001}, {38.154, 
  1.10949}, {37.8945, 0.864205}, {38.4649, 1.83546}, {38.2179, 
  1.99229}, {38.3884, 1.44735}, {38.1528, 1.69533}, {38.6398, 
  1.46291}, {38.8975, 1.85623}, {38.9552, 2.0899}, {38.935, 
  1.79664}, {38.7826, 2.10252}, {38.6051, 1.27689}, {38.6416, 
  0.967159}, {38.5503, 2.06388}, {38.7716, 0.753808}, {39.1189, 
  0.993019}, {39.2947, 1.34225}, {39.0673, 0.657109}, {38.6314, 
  0.8712}, {39.1433, 0.748528}, {39.6508, 
  0.81095}, {39.016, -0.052668}, {38.9931, -0.0363756}, {39.3453, 
  0.175443}, {39.768, 
  0.0251906}, {39.2448, -0.708407}, {39.7637, -0.345208}, {39.9792, \
-0.1199}, {39.9314, -0.636056}, {39.8347, -0.329086}, {39.6093, \
-0.952763}, {39.6335, -0.749898}, {40.2087, 
  0.0265311}, {39.9769, -0.561427}, {40.5278, -0.755097}}

Here I plot the data

(* Plot data *)
x = raw[[All, 1]];
y = raw[[All, 2]];
data = Transpose[{x, y}];
ListLinePlot[data, Mesh -> All, AxesOrigin -> {0, 0}, 
 AxesLabel ->  {"t(ms)", "Signal(V)"}]

Here I take the Fourier transform. I'm not sure if this part is correct. How can I extract parameters to help me with the Nonlinearmodelfit[] on the original data?

(* Fourier transform *)
fourier = Abs[Fourier[data]];
ListLinePlot[fourier, Mesh -> All, AxesOrigin -> {0, 0}, 
 AxesLabel ->  {"F[t(ms)]", "F[Signal(V)]"}]
$\endgroup$
8
  • $\begingroup$ What exactly are you trying with Nonlinearmodelfit ? Could you specify your Model ? $\endgroup$
    – Lotus
    Commented Feb 15, 2019 at 9:09
  • $\begingroup$ Also, your data looks very choppy. Try smoothing it using MovingAverage $\endgroup$
    – Lotus
    Commented Feb 15, 2019 at 9:16
  • $\begingroup$ I’m trying to fit the data with NonlinearModelFit, hereafter I would like to add the fitted function to the graph $\endgroup$
    – jenny
    Commented Feb 15, 2019 at 9:35
  • $\begingroup$ I will look into MovingAverage, never heard of that, tnx $\endgroup$
    – jenny
    Commented Feb 15, 2019 at 9:36
  • 1
    $\begingroup$ No need transpose raw=data. You can use ListLinePlot[raw,....] $\endgroup$ Commented Feb 15, 2019 at 13:33

2 Answers 2

10
$\begingroup$

I am guessing that you are trying to fit the data to a sum of sinusoids, and use frequency analysis to provide guesses for the parameters. Here is one approach using Periodogram:

(* sort the data and convert time to seconds *)
data = {.001, 1} # & /@ Sort[raw, First];

dataPlot = 
 ListPlot[data, PlotStyle -> {Blue, PointSize[.01]}, 
  PlotTheme -> "Scientific"]

enter image description here

(* construct an interpolating function *)
interpolatedData = Interpolation[data, InterpolationOrder -> 1];

(* resample the data with a constant sample rate *)
resampledData = Table[
   {t, interpolatedData[ t]},
   {t, -.00435, .0405, .00001}
   ];

(* the periodogram *)
frequencyPlot1 =  
      Periodogram[resampledData[[All, 2]], SampleRate -> 1/.00001, 
       PlotRange -> {All, All}]

enter image description here

(* the major peaks in the periodogram *)
frequencyPlot2 = 
     Periodogram[resampledData[[All, 2]], SampleRate -> 1/.00001, 
      PlotRange -> {{0, 500}, {-10, 35}}, Frame -> True, 
      FrameLabel -> {"Frequency", "dB"}]

enter image description here

(* a two-peak model *)
model = a1 Cos[w1 t + p1] + a2 Cos[w2 t + p2] + offset;

(* fit the data, with guesses from above *)
(* remember, w = 2 Pi f *)
fit = NonlinearModelFit[data, 
   model, {{a1, 1}, {w1, 1800}, {p1, 0}, {a2, 1}, {w2, 2400}, {p2, 
     0}, {offset, 0}}, t];

finalPlot = 
 Show[dataPlot, Plot[fit[t], {t, -.004, .04}, PlotStyle -> Red]]

enter image description here

The fit function rescaled to the original data:

rawDataFit = Evaluate[fit["Function"][t/1000]]

(* 0.30698 + 0.833353 Cos[1.35273 - 2.49589 t] + 
 0.932178 Cos[1.19477 - 1.80098 t] *)

Show[{ListPlot[raw, PlotTheme -> "Detailed"], 
  Plot[rawDataFit, {t, Min[raw[[All, 1]]], Max[raw[[All, 1]]]}, 
   PlotStyle -> {Thick, Red}]}, AspectRatio -> 1/4, ImageSize -> 1000]

enter image description here

$\endgroup$
5
  • 1
    $\begingroup$ Very instructive! $\endgroup$ Commented Feb 17, 2019 at 19:33
  • 1
    $\begingroup$ Yes, this is a pretty good how-to in itself. $\endgroup$ Commented Feb 18, 2019 at 1:03
  • $\begingroup$ @Anton, I converted the data from milliseconds to seconds so Periodogram could be read in Hz. And I like your answer. $\endgroup$ Commented Feb 19, 2019 at 16:14
  • $\begingroup$ @DavidKeith See my changes to your answer. $\endgroup$ Commented Feb 19, 2019 at 20:53
  • $\begingroup$ @Anton Antonov, excellent. Thank you! $\endgroup$ Commented Feb 19, 2019 at 22:22
7
$\begingroup$

Introduction

As stated in the previous answer:

I am guessing that you are trying to fit the data to a sum of sinusoids, and use frequency analysis to provide guesses for the parameters.

Below is an answer that is somewhat of a brute force identification of significant Sin and Cos expansion terms using Quantile Regression. (The coefficients of the basis functions found by Quantile Regression are used instead of, say, Periodogram.)

The computations are done with the package QRMon:

Import["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/MonadicProgramming/MonadicQuantileRegression.m"]

Procedure outline

  1. Make a reference fit with an appropriate B-spline basis.

  2. Compute a Quantile Regression fit with a large enough Sin/Cos basis functions.

    • Use suitable ranges for frequency factors and phase offsets.
  3. Find the most significant contributors to the fit of step 2.

    • Pick the obvious outliers.
  4. Compute Quantile Regression fit with the Sin/Cos functions found in the previous step.

  5. Examine the results and if needed re-iterate steps 2-5 with different function bases or Quantile Regression parameters.

Fit with B-splines

In this section we do a fit with B-splines basis for a references.

qrObj =
  QRMonUnit[raw]⟹
   QRMonEchoDataSummary⟹
   QRMonQuantileRegression[70, 0.5]⟹
   QRMonSetRegressionFunctionsPlotOptions[{PlotStyle -> Red}]⟹
   QRMonPlot[PlotTheme -> "Detailed", AspectRatio -> 1/4, ImageSize -> Large];

enter image description here

Here we take the fitted regression quantile:

qFunc = PiecewiseExpand[(qrObj⟹QRMonTakeRegressionFunctions)[0.5][t]];

Search for Sin/Cos model

Let us a make a large number of basis functions based on Fouriers expansion:

bFuncs = Prepend[Flatten[Table[{Sin[b + h x], Cos[b + h x]}, {h, 1, 6, 0.1}, {b, 0, 1, 0.5}]], 1];
Length[bFuncs]

(* 307 *)

Here is a fit with selected basis.

AbsoluteTiming[
 qrObj2 =
   QRMonUnit[raw]⟹
    QRMonQuantileRegressionFit[bFuncs, 0.5]⟹
    QRMonSetRegressionFunctionsPlotOptions[{PlotStyle -> Red}]⟹
    QRMonPlot[PlotTheme -> "Detailed", AspectRatio -> 1/4, ImageSize -> Large];
]

enter image description here

Here we take regression function from the monad object:

qFunc2 = (qrObj2⟹QRMonTakeRegressionFunctions)[0.5][t];

Here we can examine the most significant terms of the fit with the Sin/Cos basis:

terms = Cases[qFunc2, (f_?NumberQ*c_) :> {f, c}];
TakeLargestBy[terms, Abs@*First, 6]
ListPlot[terms[[All, 1]], PlotRange -> All, Filling -> Axis, PlotTheme -> "Scientific"]

(* {{0.902471, Sin[0. + 1.8 t]}, {0.889902, Sin[0. + 2.5 t]}, {0.138808, 
  Cos[1. + 1.8 t]}, {0.124119, Sin[0.5 + 1.8 t]}, {0.110791, 
  Cos[1. + 4.1 t]}, {0.100188, Cos[1. + 2.4 t]}} *)

enter image description here

Let us compare the two fits:

Plot[Evaluate[{qFunc, qFunc2}], {t, Min[raw[[All, 1]]], Max[raw[[All, 1]]]}, 
 AspectRatio -> 1/4, PlotLegends -> {"B-splines fit", "Sin/Cos fit"}, 
 PlotTheme -> "Scientific", PlotRange -> All, ImageSize -> Large]

enter image description here

Re-do the fit with a more informed basis

Here we select the Sin/Cos terms with the largest factors:

largestTerms = TakeLargestBy[terms, First, 5]

(* {{0.902471, Sin[0. + 1.8 t]}, {0.889902, Sin[0. + 2.5 t]}, 
    {0.138808, Cos[1. + 1.8 t]}, {0.124119, Sin[0.5 + 1.8 t]}, 
    {0.110791, Cos[1. + 4.1 t]}} *)

Here we do the fit:

qrObj3 =
  QRMonUnit[raw]⟹
   QRMonQuantileRegressionFit[Prepend[largestTerms[[All, 2]], 1], 0.5]⟹
   QRMonSetRegressionFunctionsPlotOptions[{PlotStyle -> Red}]⟹
   QRMonPlot[PlotTheme -> "Detailed", AspectRatio -> 1/4, ImageSize -> Large];

enter image description here

Take the fitted regression quantile:

qFunc3 = (qrObj3⟹QRMonTakeRegressionFunctions)[0.5][t];
qFunc3 = FullSimplify[qFunc3]

(* 0.306379 + 0.535272 Cos[1. + 1.8 t] + 0.0846008 Cos[1. + 4.1 t] + 
 1.362 Sin[1.8 t] + 0.953544 Sin[2.5 t] + 0.0595543 Sin[0.5 + 1.8 t] *)

(Compare this result with the result of the answer by David Keith. Note that the fit function found here is in the domain of the original data.)

Again, let us compare with the reference fit:

Plot[Evaluate[{qFunc, qFunc3}], {t, Min[raw[[All, 1]]], Max[raw[[All, 1]]]}, 
 AspectRatio -> 1/4, PlotLegends -> {"B-splines fit", "Informed Sin/Cos fit"},
  PlotTheme -> "Scientific", PlotRange -> All, ImageSize -> Large]

enter image description here

$\endgroup$
2
  • $\begingroup$ This is really very interesting. Thanks, Anton. $\endgroup$ Commented Feb 19, 2019 at 16:11
  • $\begingroup$ @DavidKeith I am teaching a "Quantile Regression workflows workshop" soon, so this question and our answers provide a good "second wave" example. $\endgroup$ Commented Feb 19, 2019 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.