Zero crossing for a curve fitting function(smoothing spline)

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

Accepted Answer

Star Strider
Star Strider on 3 Apr 2020
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 Comments
Star Strider
Star Strider on 3 Apr 2020
My pleasure!
If my Answer helped you solve your problem, please Accept it!

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!

Translated by