sine curve fitting by recursive method

조회 수: 8 (최근 30일)
MUHAMMAD SULEMAN
MUHAMMAD SULEMAN 2021년 6월 13일
댓글: Mathieu NOE 2021년 6월 18일
Find sine regression of periodic signal.
I have long period so I decomposed it in to small signals
Frequency = 50; % hertz
StopTime = 1/Frequency; % seconds
FittingTime = (0:dt:StopTime-dt)'; % seconds
%%Sine wave:
FittingVoltage = sin(2*pi*Fc*FittingTime);
Then I want to do curve fitting by recursive method. whereas
RangeOfVector= 5
for i = 0:1/Frequency:RangeOfVector
iter(i)=min(TrainTime):(1/Frequency):max(TrainTime)
end
This should process [x 0 0 0 0] then [0 x 0 0 0] then [0 0 x 0 0] and so on

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 6월 16일
hello
this is a little demo you can adapt to your own needs ...
clc
clearvars
close all
% dummy signal (sinus + noise)
dt = 1e-4;
samples = 1000;
f = 50;
t = (0:samples-1)*dt;
s = 0.75*sin(2*pi*f*t) + 0.1 *rand(1,samples);
%%%%%%%%%%%%% main code %%%%%%%%%%%%%%%%%
ym = mean(s); % Estimate offset
yu = max(s);
yl = min(s);
yr = (yu-yl); % Range of ‘y’
yz = s-ym;
yzs = smoothdata(yz,'gaussian',25); % smooth data to remove noise artifacts (adjust factors)
zt = t(yzs(:) .* circshift(yzs(:),[1 0]) <= 0); % Find zero-crossings
per = 2*mean(diff(zt)); % Estimate period
fre = 1/per; % Estimate FREQUENCY
% stationnary sinus fit
fit = @(b,x) b(1) .* (sin(2*pi*x*b(2) + b(3))) + b(4); % Objective Function to fit
fcn = @(b) norm(fit(b,t) - s); % Least-Squares cost function
B = fminsearch(fcn, [yr/2; fre; 0; 0;]); % Minimise Least-Squares
amplitude = B(1)
frequency_Hz = B(2)
phase_rad = B(3)
DC_offset = B(4)
xp = linspace(min(t),max(t),samples);
yp = fit(B,xp);
figure(1),
plot(t, s, 'db',xp, yp, '-r')
legend('data + noise','model fit');
  댓글 수: 4
MUHAMMAD SULEMAN
MUHAMMAD SULEMAN 2021년 6월 18일
Thank you very much. You are a life saver.
Best Wishes and Regards,
Mathieu NOE
Mathieu NOE 2021년 6월 18일
You're welcome !

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by