sine curve fitting by recursive method

12 次查看(过去 30 天)
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,

请先登录,再进行评论。

更多回答(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