How to interpolate and smooth across values in a matrix, while ignoring NaN values?

조회 수: 33 (최근 30일)
Hi there!
I am having some trouble figuring out how to interpolate across a matrix while ignoring NaN values. I have matrix data where each cell represents information from a single microphone, some of the microphones in the array malfunctioned, returning 0 values. I don't want these microphones contributing 0s to my interpolation, as the 0s don't represent real data, but rather nonfunctioning microphones. I set these malfunctioning microphones to NaN. My matrix is called 'scan' and looks something like this.
.3 .4 .3 .2 NaN .2
.4 .4 .3 .4 .2 .2
.3 NaN .5 .2 .4. .4
.3 .3 .4 .2 .2 .1
I need to interpolate across values in this matrix but I want the interpolation to ignore the NaN values, interpolating across them but without receiving them as input. Below is the interpolation code before I tried to have it ignore the NaN values
scan2 = scan'; %tranposes scan to scan2
interpImage = scatteredInterpolant(micsX(:),micsY(:), scan2(:), 'natural'); %interpolates scan2 onto fine grid, calls it enInterp
enInterp = interpImage(meshImageX, meshImageY);
K = 0.125*ones(1, 1); %smooths the interpolated data
enInterpSmooth = conv2(enInterp, K, 'valid');
enInterpSmooth = enInterpSmooth'; %puts enInterpSmooth back to the original orientation
The above code returns the interpolation with huge chunks of NaNs surrounding where the NaN microphones were.
I added the below code to try and get an interpolation that ignored the NaN values, but it still returned large amounts of NaNs.
% Replace NaN values in enInterpSmooth with interpolated values
nanIndices = isnan(enInterpSmooth);
enInterpSmooth(nanIndices) = enInterp(nanIndices); % Replace NaNs with interpolated values
Any recommendations for how to remedy this problem would be much appreciated!!
Thank y'all so much!

채택된 답변

Voss
Voss 2024년 4월 25일
Avoid putting NaNs into the scatteredInterpolant:
idx = ~isnan(scan2);
interpImage = scatteredInterpolant(micsX(idx),micsY(idx), scan2(idx), 'natural'); %interpolates scan2 onto fine grid, calls it enInterp
enInterp = interpImage(meshImageX, meshImageY);

추가 답변 (1개)

Mathieu NOE
Mathieu NOE 2024년 4월 26일
hello
just for your information, if you have a recent matlab release you could use fillmissing2 function
scan = [0.3 0.4 0.3 0.2 NaN 0.2
0.4 0.4 0.3 0.4 0.2 0.2
0.3 NaN 0.5 0.2 0.4 0.4
0.3 0.3 0.4 0.2 0.2 0.1]
scan = 4x6
0.3000 0.4000 0.3000 0.2000 NaN 0.2000 0.4000 0.4000 0.3000 0.4000 0.2000 0.2000 0.3000 NaN 0.5000 0.2000 0.4000 0.4000 0.3000 0.3000 0.4000 0.2000 0.2000 0.1000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
scan2 = fillmissing2(scan,"v4")
scan2 = 4x6
0.3000 0.4000 0.3000 0.2000 0.1961 0.2000 0.4000 0.4000 0.3000 0.4000 0.2000 0.2000 0.3000 0.4308 0.5000 0.2000 0.4000 0.4000 0.3000 0.3000 0.4000 0.2000 0.2000 0.1000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  댓글 수: 2
Mathieu NOE
Mathieu NOE 2024년 4월 26일
you can also use inpaintn available on the fex
the result may slightly differ as we are not using the same approach
scan2 =
0.3000 0.4000 0.3000 0.2000 0.1599 0.2000
0.4000 0.4000 0.3000 0.4000 0.2000 0.2000
0.3000 0.3943 0.5000 0.2000 0.4000 0.4000
0.3000 0.3000 0.4000 0.2000 0.2000 0.1000
Mathieu NOE
Mathieu NOE 2024년 4월 26일
I have one more suggestion (John would have been unhappy if I had forgotten about his work !!)
scan2 =
0.3000 0.4000 0.3000 0.2000 0.1167 0.2000
0.4000 0.4000 0.3000 0.4000 0.2000 0.2000
0.3000 0.4222 0.5000 0.2000 0.4000 0.4000
0.3000 0.3000 0.4000 0.2000 0.2000 0.1000

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by