Clear Filters
Clear Filters

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

10 views (last 30 days)
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!

Accepted Answer

Voss
Voss on 25 Apr 2024
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);

More Answers (1)

Mathieu NOE
Mathieu NOE on 26 Apr 2024
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 Comments
Mathieu NOE
Mathieu NOE on 26 Apr 2024
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 on 26 Apr 2024
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

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!