Interpolation of noisy data
23 views (last 30 days)
I have the following plot (can be plotted with plot(B,C) based on the attached mat file):
However, I would like to plot the "smoothed" trend, i.e something like:
What would be the best approach? I have seen the splines function, but I am not sure if this is what I am looking for.
Image Analyst on 2 Oct 2018
Edited: Image Analyst on 2 Oct 2018
You can use a clever trick to get unique numbers, and avoid the problem Star brought up. You can add an insignificant amount of noise and then sort the values. Try this:
% Load data.
storedStructure = load('dataForPlot.mat')
B = storedStructure.B;
C = storedStructure.C;
% B data (x axis) is not unique yet.
% Add noise and then sort to make it unique.
bPlusNoise = B + 0.00001 * rand(1, length(B));
% Now all B will be unique.
% Assume B is x and C is y
% Sort x in ascending order.
[x, sortOrder] = sort(bPlusNoise, 'ascend');
% Sort y in the same order.
y = C(sortOrder);
plot(x, y, 'b-');
% Filter the y data
windowWidth = 301; % Whatever - bigger odd number for more smoothing.
kernel = ones(1, windowWidth) / windowWidth;
% ySmoothed = conv(y, kernel, 'same');
ySmoothed = sgolayfilt(y, 1, windowWidth);
plot(x, ySmoothed, 'r-', 'LineWidth', 3);
legend('Original Data', 'Smoothed Data');
xlabel('B', 'FontSize', 20);
ylabel('C', 'FontSize', 20);
title('C and Smoothed C vs. B', 'FontSize', 20);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
What do you think? Will this work for you?