sine curve fitting by recursive method

8 vues (au cours des 30 derniers jours)
MUHAMMAD SULEMAN
MUHAMMAD SULEMAN le 13 Juin 2021
Commenté : Mathieu NOE le 18 Juin 2021
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

Réponse acceptée

Mathieu NOE
Mathieu NOE le 16 Juin 2021
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 commentaires
MUHAMMAD SULEMAN
MUHAMMAD SULEMAN le 18 Juin 2021
Thank you very much. You are a life saver.
Best Wishes and Regards,
Mathieu NOE
Mathieu NOE le 18 Juin 2021
You're welcome !

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by