Sinusoidal curve from discrete dataset
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hi!
I have a few discrete data-set and when I plot them it looks like this -

Can anyone please tell me how can I fit these discrete points into a sinusoidal curve like this?

The data is a small 23x4000 matrix and I added the .mat file to the question. Any feedback will be really helpful. Thank you!
채택된 답변
William Rose
2023년 3월 6일
편집: William Rose
2023년 3월 7일
[edit: I changed the comments in the code for t1 and t. I added a line to display the fitted values.]
load('sinusoidal');
[rows,cols]=size(Sinusoidal); % read data form file
t1=(1:rows)'; % t1=vector
t=repmat(t1,1,cols); % t=array
Sinusoidal=reshape(Sinusoidal,rows*cols,1); % convert array to vector
T=Sinusoidal(~isnan(Sinusoidal)); % remove NaNs
t=t(~isnan(Sinusoidal)); % remove corresponding times
% next: define the model equation
myfittype=fittype('a*sin(w*t+p)','dependent',{'T'},'independent',{'t'},'coefficients',{'a','w','p'});
x0=[30,.1,0]; % initial guess for [a,b,c]
myfit=fit(t,T,myfittype,'StartPoint',x0); % fit the data
plot(myfit,'-b',t,T,'r.'); % plot results
xlabel('Time'); ylabel('Temperature')

% Display best-fit parameters on console.
fprintf('y=a*sin(wt+p): a=%.4f, w=%.4f, p=%.4f\n',myfit.a,myfit.w,myfit.p)
y=a*sin(wt+p): a=22.4003, w=0.1006, p=-0.1809
Try it.
댓글 수: 6
This is brilliant! Thank you!!
Ashfaq Ahmed
2023년 3월 7일
편집: Ashfaq Ahmed
2023년 3월 7일
@William Rose can you please tell me if there is anyway to get the amplitude and the period of this sine curve?
(A relevant post I found here - https://www.mathworks.com/matlabcentral/answers/121579-curve-fitting-to-a-sinusoidal-function where @Star Strider 's method gives some nice inputs.)
I was curious to know if I can obtain the Amplitude and period using this methos too.
@Ashfaq Ahmed, thank you for your kind comment. You're welcome.
The best fit parameters a, w, p can be displayed by adding the line
fprintf('y=a*sin(wt+p): a=%.4f, w=%.4f, p=%.4f\n',myfit.a,myfit.w,myfit.p)
If the initial guess for x0=[a,w,p] is very poor, then you may not find the best fit. In the case above, my initial guess x0 was based on eyeballing the data you provided.
@Star Strider, at the site you found, uses clever ideas to make a good initial guess for the parameters of a sinusoidal function. I suspect his method would not work well with your data set, since your data is less than one half cycle. Therefore his initial frequency estimate, which is based on the number of crossings of the mean, is unlikely to work. Also, I believe that his algorithm assumes that the x-values increase (or decrease) monotonically. Your data does not do this: you have many y-values for each x value, or, to put it another way, each x-value is repeated many times.
I was folowwing your code to make a sinusoidal signal from another temperature record. Your code works fine with the sinusoidal.mat file that I added to this question. But when I have a single array of temperature record, it does not work. I am getting this error message -
Error using fit>iFit (line 135)
X must be a matrix with one or two columns.
Error in fit (line 116)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain,
fittypeobj, ...
Error in BuoyCheck (line 140)
myfit=fit(t,TEMP,myfittype,'StartPoint',x0); % fit the data
I added the .mat file. Can you please give me an idea on how can I get a very good fit sinusoidal curve from the temprature record? The reocrd is in T.Temp array.
I will really appreciate any feedback coming from you.
@Ashfaq Ahmed, This is rather complicated to do properly, because the data in T.mat has values in datetime format, for the sample times. It also has the numeric values for time. Matlab is telling users to stop using numeric times, and start using datetime values. Therefore I am trying to adapt. The data in T.mat spans approximately 10 years, with uneven time intervals between samples (long data gaps every winter). Please contact me through Matab Central by clicking on my name above, then click on the envelope icon in the pop-up box. Please provide your email in the message, so we can discuss offline.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Correlation and Convolution에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
