Sine curve for a given period value.

I want to fit a sine curce to my data for a given period value. How should I do it?

 Accepted Answer

Star Strider
Star Strider on 13 May 2018
See for example: Curve fitting to a sinusoidal function (link), and How to filter noise from time-frequency data and find natural frequency of a cantilever? (link). You can easily adapt the code in those to your data if you need to.

13 Comments

Hey Star Strider, I am new to MATLAB. COuld you please help me.
I have x and y data and y error..and I know its period.. how to fit a sine train..please help
I tried to understand through the following link:
https://in.mathworks.com/matlabcentral/answers/121579-curve-fitting-to-a-sinusoidal-function
but could not understand because running it is giving errors. X axis of my data is phases while y axis is the data..
‘x and y data and y error.’
My code calculates the period, as well as the phase and y-offset. You simply need to give it the x vector and y vector.
I have no idea what ‘y error’ is, however it may not be possible to use it with my code.
‘... it is giving errors’
Since I do not know what the errors are, I cannot suggest a solution.
Giru Mishra
Giru Mishra on 13 May 2018
Edited: Giru Mishra on 13 May 2018
OKay ignore "error".
I already know period, my data's x axis is phases from 0 to 2.. and I have some y data.. now I want to fit the sine curve.
Using your code I am not able to fit.. what changes should I make according to my data?
I have no idea.
I do not have your data, and I do not know what problems you are having.
This is the sample data file. Please ignore 3rd column
Naming your matrix ‘data’, this works:
x = data(:,1);
y = data(:,2);
yu = max(y);
yl = min(y);
yr = (yu-yl); % Range of ‘y’
yz = y-yu+(yr/2);
zx = x(yz(:) .* circshift(yz(:),[1 0]) <= 0); % Find zero-crossings
per = 2*mean(diff(zx)); % Estimate period
ym = mean(y); % Estimate offset
fit = @(b,x) b(1).*(sin(2*pi*x./b(2) + 2*pi/b(3))) + b(4); % Function to fit
fcn = @(b) sum((fit(b,x) - y).^2); % Least-Squares cost function
s = fminsearch(fcn, [yr; per; -1; ym]) % Minimise Least-Squares
xp = linspace(min(x),max(x));
figure(1)
plot(x,y,'pb', xp,fit(s,xp), 'r')
grid
fprintf(1, 'Amplitude = %7.3f\nFrequency = %7.3f\nPhase = %7.3f\nOffset = %7.3f\n\n', s(1), 1/s(2), 1/s(3), s(4))
estimating these parameters:
Amplitude = 0.503
Frequency = 0.960
Phase = -0.803
Offset = 21.477
and produces this plot:
It was not even necessary to change my original code.
I am so sorry.. I sent a wrong data file: this one is giving error.. its giving NaN values.. and only data is plotting not the fit. Apologies
No worries.
To use your new data, in place of the original x and y assignments, substitute:
Lidx = all(isfinite(data),2); % All Columns In Every Row Must Be Finite (Not ‘±Inf’ Or ‘NaN’)
x = data(Lidx,1);
y = data(Lidx,2);
to include only those rows with finite data. (This is a standard MATLAB convention, since ±Inf and NaN values will produce non-finite results.)
My code then produces these parameters:
Amplitude = 0.291
Frequency = 0.991
Phase = -0.702
Offset = 3.900
and this plot:
I also edited some of the code in my earlier Comment to make it look neater, so all the comments begin in the same column. The code itself did not change.
Giru Mishra
Giru Mishra on 14 May 2018
Edited: Giru Mishra on 14 May 2018
woww.. you are great.. Thanks a lot.. 1 more thing.. is it possible to increase amplitude of the peak coming around 1 and also shift it to 0.8?? and also decrease the width of the central peak?
Thanks a lot again
As always, my pleasure. Thank you.
You would need to re-write the ‘fit’ function, perhaps as some version of sin(x)/x, or use the current code with a weighting vector (to give more ‘weight’ to some data) to get different results. It would depend on what your data represent, and the process that created them. A mathematical model that describes the process that created your data is always the best regression (here ‘fit’) function.
OH okay.. thanks..
As always, my pleasure.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!