How to smooth the line curves in the plot?

25 visualizaciones (últimos 30 días)
Wiqas Ahmad
Wiqas Ahmad el 25 de Abr. de 2024
Comentada: Ram Prasanth el 26 de Abr. de 2024
I'm using the function "smooth" and ''abs'' to smooth the line curve (I_para) but can't acheive the smooth curves in the plot. I used the smooth function previously and got the curve smooth but this time it doesn't work for my case. Please your help willl be appreciated. Thank you
clear, clc, close all
a_ref = 0.05;
Reff_ref = 20;
z_bottom = 1000:1000:4000;
FOV = [0.2, 0.5, 1, 2, 5, 10];
cloud = 'Sub';
for h = 1:length(z_bottom)
main_fig = figure('NumberTitle', 'off', 'Name', ['Height ', num2str(z_bottom(h)), 'm']);
Area = []; % for sort
COLOR = {'r', 'g', 'b', [0.8500 0.3250 0.0980], [0.4660 0.6740 0.1880], [0 0.4470 0.7410]};
ylim_max = 0;
for a = 0.01:0.01:a_ref
for r = 4:Reff_ref
clf(main_fig); % Clear the current figure
subplot(1, 1, 1); % Create subplot for all FOVs
hold on;
for f = 1:length(FOV)
Height = genHeight(z_bottom(h))';
load(['MCdatabase_', cloud, '/', num2str(a), '-', num2str(r), 'um/', num2str(z_bottom(h)), 'm-', num2str(FOV(f)), 'mrad/I0.mat']);
load(['MCdatabase_', cloud, '/', num2str(a), '-', num2str(r), 'um/', num2str(z_bottom(h)), 'm-', num2str(FOV(f)), 'mrad/Q0.mat']);
I_para = abs(1/2 * (I0 + Q0));
I_per = abs(1/2 * (I0 - Q0));
% Plotting all FOV plots on a single figure for each cloud height
plot(Height, smooth(abs(sum(I_para, 2))) .* Height.^2, 'Color', COLOR{f}, 'LineWidth', 1.5);
hold on
plot(Height, smooth(abs(sum(I_per, 2))) .* Height.^2, 'Color', COLOR{f}, 'LineStyle', '--', 'LineWidth', 1.5);
title(['Sub-adiabatic cloud, Height ', num2str(z_bottom(h)), 'm'])
% Add annotation for polarized signals
if f == 1
annotation('textbox', [0.15, 0.15, 0.1, 0.1], 'String', 'P_{\perp}', 'Color', 'k', 'EdgeColor', 'none', 'FontWeight', 'bold', 'FontSize', 12);
annotation('textbox', [0.24, 0.5, 0.1, 0.1], 'String', 'P_{\mid\mid}', 'Color', 'k', 'LineStyle', '--', 'EdgeColor', 'none', 'FontWeight', 'bold', 'FontSize', 12);
end
Area = [Area, [z_bottom(h); FOV(f); pi * ((2.5 * cot(FOV(f) * 1e-3/2) + z_bottom(h)) * tan(FOV(f) * 1e-3/2))^2]];
% Calculate the maximum y-axis limit
ylim_max = max(ylim_max, max(sum(I_para, 2) .* Height.^2));
ylim_max = max(ylim_max, max(sum(I_per, 2) .* Height.^2));
end
hold off;
end
end
% Figure settings based on the data
zoom on
xlabel('Height of the cloud (m)','FontSize',14,'FontWeight','normal');
ylabel('Backscattering signal (Unit)','FontSize',14,'FontWeight','normal');
set(gca,'color','w','Fontsize',12,'LineWidth',1.5,'Fontweight','normal');
set(gca,'box','off','Fontname','Arial','Fontsmoothing','on');
% Convert y-axis to logarithmic scale
% set(gca, 'YScale', 'log');
% Define the offset for increasing x-axis limit
if h == 2
offset = 2; % Offset for h == 2
elseif h == 3
offset = 4; % Offset for h == 3
else
offset = 0; % No offset for other cases
end
% Set x-axis limits and ticks with a difference of 50 for all figures
xlim_min = z_bottom(h);
xlim_max = max(Height) + offset; % Adjusted xlim_max with an offset
% Adjust xticks for last tick visibility
if h == 1
xticks = 1000:50:(max(Height));
elseif h == 2
xticks = [2000:50:(max(Height) + offset)]; % Corrected x-ticks for Figure 2
elseif h == 3
xticks = [3000:50:(max(Height) + offset)]; % Corrected x-ticks for Figure 3
else
xticks = 4000:50:xlim_max;
end
set(gca,'xlim',[xlim_min, xlim_max],'xtick',xticks,'ylim',[0 ylim_max],'ytick',[0:0.02:ylim_max]);
set(gca,'xgrid','on','ygrid','on','gridcolor','k');
% Create legend entries for each FOV value
legend_labels = cell(1, length(FOV) * 2); % Preallocate space for legend labels
for i = 1:length(FOV)
legend_labels{(i-1)*2+1} = [num2str(FOV(i)), 'mrad'];
legend_labels{(i-1)*2+2} = [num2str(FOV(i)), 'mrad'];
end
% Plot the legend entries in two rows
legend(legend_labels, 'Location', 'NorthEast', 'NumColumns', 3);
legend boxoff
end

Respuestas (2)

Image Analyst
Image Analyst el 25 de Abr. de 2024
See attached demo. Adapt as needed.
% Demo to show spline interpolation.
% Clean up / initialize
clc;
close all;
clear all;
workspace; % Display workspace panel.
% Create the original knot points.
lengthX = 10;
x = 1:lengthX;
y = rand (lengthX,1);
% Plot it and show how the line has sharp bends.
plot(x, y, '-sr', 'LineWidth', 2);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Use splines to interpolate a smoother curve,
% with 10 times as many points,
% that goes exactly through the same data points.
samplingRateIncrease = 10;
newXSamplePoints = linspace(1, max(x), lengthX * samplingRateIncrease);
smoothedY = spline(x, y, newXSamplePoints);
% Plot smoothedY and show how the line is
% smooth, and has no sharp bends.
hold on; % Don't destroy the first curve we plotted.
plot(newXSamplePoints, smoothedY, '-ob');
title('Spline Interpolation Demo', 'FontSize', 20);
legend('Original Points', 'Spline Points');
% Mathworks Demo code from their Help
% x = 0:10;
% y = sin(x);
% xx = 0:.25:10;
% yy = spline(x,y,xx);
% plot(x,y,'o',xx,yy)
slopes = [0, diff(smoothedY)];
plot(newXSamplePoints, slopes, 'k-', 'LineWidth', 3);
% Draw x axis
line(xlim, [0,0], 'Color', 'k', 'LineWidth', 2);
grid on;
legend('Original Points', 'Spline Points', 'Slope');
  3 comentarios
Wiqas Ahmad
Wiqas Ahmad el 26 de Abr. de 2024
I increased the samplingRateIncrease = 1000; but still doesn't work. The portion of the code is here
% Smooth the signal
samplingRateIncrease = 10000;
newXSamplePoints = linspace(1, size(I_para, 1), size(I_para, 1) * samplingRateIncrease);
smoothedY = interp1(1:size(I_para, 1), sum(I_para, 2) .* Height.^2, newXSamplePoints,'linear');
% Interpolate smoothedY to match the length of Height
x_interp = linspace(1, numel(smoothedY), numel(Height));
smoothedY_interp = interp1(1:numel(smoothedY), smoothedY, x_interp, 'linear');
% Plotting
plot(Height, smoothedY_interp, 'Color', COLOR{f}, 'LineWidth', 1.5)
hold on
plot(Height, smooth(sum(I_per, 2)) .* Height.^2, 'Color', COLOR{f}, 'LineStyle', '--', 'LineWidth', 1.5)
title(['Sub-adiabatic cloud, Height ', num2str(z_bottom(h)), 'm'])
Ram Prasanth
Ram Prasanth el 26 de Abr. de 2024
You need to do few Modifications as i suggested before in your code.
I have changed the necessary and you can adopt it in your code.
smoothedY = interp1(1:size(I_para, 1), sum(I_para, 2 ...
) .* Height.^2, newXSamplePoints,'spline'); % Use spline interpolation
% Interpolate smoothedY to match the length of Height
smoothedY_interp = interp1(1:numel(smoothedY), smoothedY, linspace( ...
1, numel(smoothedY), numel(Height)), 'spline');
samplingRateIncrease = 10000; % Try increasing this value for smoother curves only
% after testing the spline interpolation method.

Iniciar sesión para comentar.


Ram Prasanth
Ram Prasanth el 25 de Abr. de 2024
You can use the spline interpolation method in matlab.
Which allow you to interpolate the values inbetween and increases the number of points, Hence giving you a smooth spline curve.
Also you can look in to the Spline function in matlab which can be useful too - Cubic spline data interpolation - MATLAB spline - MathWorks France
  2 comentarios
Wiqas Ahmad
Wiqas Ahmad el 25 de Abr. de 2024
I checked but it doesn't work for my case.
Image Analyst
Image Analyst el 25 de Abr. de 2024
How many data points does your smoothed array have? Like he said, you must use more points than your original data or else you will still have "kinks" in your curve.

Iniciar sesión para comentar.

Categorías

Más información sobre Interpolation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by