How to smooth the matlab plot to get the desired plot shape?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Is there a way to change figure one to figure 2 (like the lines I draw in red and black color) without changing the values of y1 and y2? Please help.
Figure 1:
Figure 2:
Below is my code
clear all; close all; clc;
x= [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1];
y1 = [0 0.0833 0.1583 0.2167 0.1500 0.3250 0.3750 0.3000 0.5917 0.3750 0.5000];
y2= [ 0 0 0.0167 0.0750 0.1000 0.0917 0.1167 0.1583 0.1083 0.2000 0.1833];
figure
plot (x,y1,'o')
hold on
plot (x,y2,'o')
Xi = 0:0.005:1;
Yi = pchip(x,y1,Xi);
Yi_spline = spline(x,y1,Xi);
h(1) = plot(Xi,Yi,'-','color',lines(1));
h(2) = plot(Xi, Yi_spline, '--', 'color', lines(1));
Yj = pchip(x,y2,Xi);
Yj_spline = spline(x, y2, Xi);
h(3) = plot(Xi,Yj,'-','color',[0.85, 0.325, 0.098]);
h(4) = plot(Xi,Yj_spline,'--','color',[0.85, 0.325, 0.098]);
legend(h, "Yi pchip", "Yi spline", "Yj pchip", "Yj spline", "location", "NW")
0 Commenti
Risposta accettata
Angelo Yeo
il 24 Lug 2023
I'm not sure about your intention. But the easiest way to smooth signals is moving average. See the doc below for more information about moving average.
https://www.mathworks.com/help/releases/R2023a/matlab/ref/movmean.html
x= [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1];
y1 = [0 0.0833 0.1583 0.2167 0.1500 0.3250 0.3750 0.3000 0.5917 0.3750 0.5000];
y2= [ 0 0 0.0167 0.0750 0.1000 0.0917 0.1167 0.1583 0.1083 0.2000 0.1833];
figure
plot (x,y1,'o')
hold on
plot (x,y2,'o')
dt = 0.005;
Xi = 0:dt:1;
Yi = pchip(x,y1,Xi);
Yi_spline = spline(x,y1,Xi);
h(1) = plot(Xi,Yi,'-','color',lines(1));
h(2) = plot(Xi, Yi_spline, '--', 'color', lines(1));
Yj = pchip(x,y2,Xi);
Yj_spline = spline(x, y2, Xi);
h(3) = plot(Xi,Yj,'-','color',[0.85, 0.325, 0.098]);
h(4) = plot(Xi,Yj_spline,'--','color',[0.85, 0.325, 0.098]);
%% Smoothing
Yi_smooth = movmean(Yi_spline, 100);
Yj_smooth = movmean(Yj_spline, 100);
h(5) = plot(Xi, Yi_smooth, 'r','linewidth',2);
h(6) = plot(Xi, Yj_smooth, 'k','linewidth',2);
legend(h, "Yi pchip", "Yi spline", "Yj pchip", "Yj spline", "Yi smoothed", "Yj smoothed", "location", "NW")
4 Commenti
Angelo Yeo
il 24 Lug 2023
Anyways, if you insist that the resultant curve should pass (0, 0), you can think of something like curve fitting for a quadratic polynomial without a bias term.
clear; close all; clc;
x= [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1];
y1 = [0 0.0833 0.1583 0.2167 0.1500 0.3250 0.3750 0.3000 0.5917 0.3750 0.5000];
y2= [ 0 0 0.0167 0.0750 0.1000 0.0917 0.1167 0.1583 0.1083 0.2000 0.1833];
figure
plot (x,y1,'o')
hold on
plot (x,y2,'o')
dt = 0.005;
Xi = 0:dt:1;
Yi = pchip(x,y1,Xi);
Yi_spline = spline(x,y1,Xi);
h(1) = plot(Xi,Yi,'-','color',lines(1));
h(2) = plot(Xi, Yi_spline, '--', 'color', lines(1));
Yj = pchip(x,y2,Xi);
Yj_spline = spline(x, y2, Xi);
h(3) = plot(Xi,Yj,'-','color',[0.85, 0.325, 0.098]);
h(4) = plot(Xi,Yj_spline,'--','color',[0.85, 0.325, 0.098]);
legend(h, "Yi pchip", "Yi spline", "Yj pchip", "Yj spline", "location", "NW")
%% Fitting a quadratic curve
% Set up fittype and options.
[xData, yData] = prepareCurveData( Xi, Yi_spline );
ft = fittype( 'p1*x^2+p2*x', 'independent', 'x');
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.StartPoint = [0, 0];
Yi_fit = fit( xData, yData, ft, opts );
[xData, yData] = prepareCurveData( Xi, Yj_spline );
ft = fittype( 'p1*x^2+p2*x', 'independent', 'x');
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.StartPoint = [0, 0];
Yj_fit = fit( xData, yData, ft, opts );
h(5) = plot(Xi, Yi_fit.p1*Xi.^2 + Yi_fit.p2*Xi,'r','linewidth', 2);
h(6) = plot(Xi, Yj_fit.p1*Xi.^2 + Yj_fit.p2*Xi,'k','linewidth', 2);
legend(h, "Yi pchip", "Yi spline", "Yj pchip", "Yj spline", "Yi smoothed", "Yj smoothed", "location", "NW")
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Smoothing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!