Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Fitting sine curve to data points

406 views
Skip to first unread message

Poulomi

unread,
Jan 18, 2014, 6:27:08 PM1/18/14
to
Hi,

I have a 343x1 discrete data matrix, which I need to fit into sine curve function. I don't know how to compute amplitude and phase of the data points. I saw some function fft but that gives a huge result 70. Can anybody please give me proper suggestion, how to fit sine curve of the form A*Sin(2*pi*f*t) or A*Sin(2*pi*f*t)+B form. Kindly help in this regard. Thanks in advance.

TideMan

unread,
Jan 18, 2014, 10:44:56 PM1/18/14
to
On Sunday, January 19, 2014 12:27:08 PM UTC+13, Poulomi wrote:
> Hi,
>
>
>
> I have a 343x1 discrete data matrix, which I need to fit into sine curve function. I don't know how to compute amplitude and phase of the data points. I saw some function fft but that gives a huge result 70. Can anybody please give me proper suggestion, how to fit sine curve of the form A*Sin(2*pi*f*t) or A*Sin(2*pi*f*t)+B form. Kindly help in this regard. Thanks in advance.

Do you know f a priori?
If so, it's easy using the \ least squares facility.
If not, it's more tricky.

Poulomi

unread,
Jan 18, 2014, 11:46:06 PM1/18/14
to
TideMan <mul...@gmail.com> wrote in message <f5ecd74f-2ef3-4a94...@googlegroups.com>...
No, I don't know f. Pls. explain how to do so & fitting other parameters A,..C using least squares. Look forward..

Nasser M. Abbasi

unread,
Jan 19, 2014, 12:57:55 AM1/19/14
to

Ram Dwivedula

unread,
May 4, 2014, 6:07:11 AM5/4/14
to
Try following function:

function [freq,amp,phase]=obtain_fft(x,w1,w2,ts,varargin)
%
%gives frequency and amplitude content of the signal in vector x
%
% Usage : [freq,amplitude,phase]=obtain_fft(x,w1,w2,ts,p,'units','signal')
%
% x : signal whose fft is sought
% [w1,w2] : is frequency range of interest
% ts : sampling period in seconds
% freq : frequency in rad/sec
% amp : amplitude
% phase : phase in degrees
% p : if equal to 1 also draws amplitude plot (closes all open plots)
% if equal to 2 draws phase plot also
% 'units' : units of the elements of vector x (lbf, N, rad/sec, etc)
% 'signal' : indicates the physical quantity (tension, velocity, etc)
% p, 'units', 'signal' are optional and may be omitted

N=length(x);
dd_fft=abs(fft(x));
phase=angle(fft(x));
dd_fft=dd_fft/N*2;
NN=floor([(w1*N*ts+1):(w2*N*ts+1)]);
freq=(NN-1)/N/ts;
amp=dd_fft(NN);
phase=phase(NN)*180/pi/2;
%[w1,a1,w2,a2]=pick12natfreq(freq,amp)
if nargin==5 | nargin==6 | nargin==7% only p is specified
p=cell2mat(varargin(1));
if p==1 | p==2
if nargin==6 | nargin==7
units=char(varargin(2));
yunits=sprintf('Amplitude (%s)',units);
else
yunits='Amplitude';
end
% close all
% figure(1)
if p==2
subplot(2,1,1)
end
if nargin==7
titlestr=sprintf('of %s',char(varargin(3)));
else
titlestr='';
end
titlestr=sprintf('Frequency Content %s',titlestr);
plot(freq,amp,'r-')
ylabel(yunits)
xlabel('\omega (Hz)')
grid on
title(titlestr)
if p==2
subplot(2,1,2)
plot(freq,phase,'r-')
ylabel('Phase (degrees)')
xlabel('\omega (Hz)')
grid on
end
end
end
return




"Poulomi" wrote in message <lbf2kc$rn6$1...@newscl01ah.mathworks.com>...

Charlie Roberts

unread,
May 4, 2014, 10:45:48 AM5/4/14
to

>
>"Poulomi" wrote in message <lbf2kc$rn6$1...@newscl01ah.mathworks.com>...
>> Hi,
>>
>> I have a 343x1 discrete data matrix, which I need to fit into sine curve function. I don't know how to compute amplitude and phase of the data points. I saw some function fft but that gives a huge result 70. Can anybody please give me proper suggestion, how to fit sine curve of the form A*Sin(2*pi*f*t) or A*Sin(2*pi*f*t)+B form. Kindly help in this regard. Thanks in advance.

If all you want to do is fit a sine, fminsearch will work very well.
I use it to for just this purpose and find it quite stable.

You will have to write a very short function that takes your
data as input and returns the residual sum of squares -- which
fminseach will seek to minimize.

The documentation for fminsearch provideds a detailed
example, function handles and all.

You will have to check for negative amplitudes and shift
the phase by pi in that case. Also, check for phases values
larger than 2*pi.

Charlie
0 new messages