sine curve fitting by recursive method

4 views (last 30 days)
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

Accepted Answer

Mathieu NOE
Mathieu NOE on 16 Jun 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 Comments
MUHAMMAD SULEMAN
MUHAMMAD SULEMAN on 18 Jun 2021
Thank you very much. You are a life saver.
Best Wishes and Regards,

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!