I have a series of data given by an x array and a corresponding y array. The arrays are equal in size. The x array is approximately equally spaced, but it has a few small gaps, and some very large gaps. I would like to interpolate to get numeric values in the small gaps, but I'd like to replace the big gaps with NaNs.
Here is an example of some small gaps, and one big gap. I'd like to replace the obviously incorrect interpolation with NaNs. How can I do this?
% create some data:
x = 0:.02:15;
y = sin(x);
% create some holes in the data
x([3 25 32:33 200:280 410:415]) = [];
y([3 25 32:33 200:280 410:415]) = [];
plot(x,y,'ko'); hold on
% the interpolation I'd like to improve:
xi = 0:.015:15;
yi = interp1(x,y,xi,'cubic');
plot(xi,yi,'b.')

댓글 수: 3

Image Analyst
Image Analyst 2014년 2월 21일
편집: Image Analyst 2014년 2월 21일
To determine "hole" or "gap" size, it helps to have the Image Processing Toolbox. Do you have that? Type ver to find out. If you do you can use functions like bwareaopen() or regionprops() to easily determine the size of the hole.
Here is a loop that almost solves the problem. I'd rather avoid loops, but the biggest problem with this solution is the little tails that persist:
x = 0:.02:15;
y = sin(x);
x([3 25 32:33 200:280 410:415]) = [];
y([3 25 32:33 200:280 410:415]) = [];
plot(x,y,'ko'); hold on
xi = 0:.015:15;
yi = interp1(x,y,xi,'cubic');
for n = 1:length(xi)
if min(abs(xi(n)-x))>.1
yi(n) = NaN;
end
end
plot(xi,yi,'b.')
Above, a threshold is set to set to change any values more than 0.1 x units away from an x data point to NaN.
Chad Greene
Chad Greene 2014년 2월 21일
I do have the Image Processing Toolbox, and I'm reading the documentation right now. Application of bwareaopen and regionprops is not immediately apparent.

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

 채택된 답변

Paul
Paul 2014년 2월 21일

0 개 추천

x_gap = x(2:end)-x(1:end-1);
ind=find(x_gap>0.025);
ind_int=[];
for j=1:numel(ind)
ind_int = [ind_int,find((xi>x(ind(j)) & xi<x(ind(j)+1)))];
end
yi(ind_int)=NaN;

댓글 수: 4

Chad Greene
Chad Greene 2014년 2월 22일
This works well; thank you!
Well I couldn't get it to work but you did so that's all that counts:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
% create some data:
x = 0:.02:15;
y = sin(x);
% Create some holes in the data
x([3 25 32:33 200:280 410:415]) = [];
y([3 25 32:33 200:280 410:415]) = [];
% Plot data
subplot(3,1,1);
plot(x, y, 'ro-');
grid on;
title('Original Signal', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
xi = 0:.015:15;
x_gap = x(2:end)-x(1:end-1);
subplot(3,1,2);
bar(x_gap);
title('x_gap', 'FontSize', fontSize, 'Interpreter', 'None');
ind=find(x_gap>0.025)
ind_int=[];
for j=1:numel(ind)
ind_int = [ind_int,find((xi>x(ind(j)) & xi<x(ind(j)+1)))];
end
yi(ind_int)=NaN;
% Plot interpolated arrays.
subplot(3,1,3);
title('Interpolated Signal', 'FontSize', fontSize);
plot(xi, yi, 'bs-');
And I won't bother with using regionprops() since you already found a working solution.
add this:
yi = interp1(x,y,xi,'cubic');
after xi = 0:.015:15; and then it works.
Chad Greene
Chad Greene 2014년 3월 11일
I turned Paul's solution into a function: http://www.mathworks.com/matlabcentral/fileexchange/45842. Thank you both for your help.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2014년 2월 21일

댓글:

2014년 3월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by