Zero crossing for a curve fitting function(smoothing spline)

조회 수: 7 (최근 30일)
Angelavtc
Angelavtc 2020년 4월 3일
댓글: Star Strider 2020년 4월 3일
Hello,
I want find the zero-crossings of a smoothing spline function that I got using the Curve Fitting toolbox and plot the points were this occurs. The function code is the following:
%% Fit: 'Jan 2012'.
[xData, yData] = prepareCurveData( x_jan_12_s, Price_jan_12_s );
% Set up fittype and options.
ft = fittype( 'smoothingspline' );
excludedPoints = excludedata( xData, yData, 'Indices', [2 276] );
opts = fitoptions( 'Method', 'SmoothingSpline' );
opts.SmoothingParam = 8.24530273269464e-08;
opts.Exclude = excludedPoints;
% Fit model to data.
[fitresult{2}, gof(2)] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'Jan 2012 Supply France Peak' );
plot( fitresult{2}, 'k');
% Label axes
xlabel( 'Volume [MWh]', 'Interpreter', 'none' );
ylabel( 'Price', 'Interpreter', 'none' );
However with his code, you need to define the y, which is not available for my fit curve (y is contained in fitresult{2}):
t = [1:0.01:5]; % Time Vector
y = sin(2*pi*t); % Signal
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Zero-Crossing Indices Of Argument Vector
zx = zci(y); % Approximate Zero-Crossing Indices
figure(1)
plot(t, y, '-r')
hold on
plot(t(zx), y(zx), 'bp')
hold off
grid
legend('Signal', 'Approximate Zero-Crossings')
How can I solve the problem?
Thanks in advance!

채택된 답변

Star Strider
Star Strider 2020년 4월 3일
I don’t have the Curve Fitting Toolbox. (I only need it to reply to Answers Questions, and that’s not enough justification for me to buy it.) However from the documentation, the cfit function is likely what you need to recover the fitted values from the ‘fitresult{2}’ output:
y = cfit(fitresult{2}, xData);
I have no idea what the curve or data look like. It may be necessary to subtract the mean of the cfit output from the rest of it so that it has the necessary zero-crossings to use with my code. If you want the exact zero-crossings (my code returns the closest indices to them), it will be necessary to interpolate them. This is not difficult, however it will require your data to demonstrate it.
  댓글 수: 5
Angelavtc
Angelavtc 2020년 4월 3일
This works amazing! thank you!
Star Strider
Star Strider 2020년 4월 3일
My pleasure!
If my Answer helped you solve your problem, please Accept it!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Smoothing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by